首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python sklearn /titanic教程在上一个特性标度上失败

Python sklearn /titanic教程在上一个特性标度上失败
EN

Stack Overflow用户
提问于 2016-12-15 19:09:36
回答 1查看 176关注 0票数 0

我正在学习本教程:http://ahmedbesbes.com/how-to-score-08134-in-titanic-kaggle-challenge.html

它没有问题,直到我到达中间部分的最后一部分:

正如您所看到的,功能的范围是不同的间隔。让我们在单位间隔内对它们进行规范化。除了提交文件所需的PassengerId之外,所有这些

代码语言:javascript
复制
In [48]:
>>> def scale_all_features():

>>>     global combined

>>>     features = list(combined.columns)
>>>     features.remove('PassengerId')
>>>     combined[features] = combined[features].apply(lambda x: x/x.max(), axis=0)

>>>     print 'Features scaled successfully !'

In [49]:
>>> scale_all_features()

功能缩放成功!

尽管在我的python脚本中逐字输入了它:

代码语言:javascript
复制
#Cell 48
GreatDivide.split()
def scale_all_features():

    global combined

    features = list(combined.columns)
    features.remove('PassengerId')
    combined[features] = combined[features].apply(lambda x: x/x.max(), axis=0)

    print 'Features scaled successfully !'

#Cell 49
GreatDivide.split()
scale_all_features()

它总是给我一个错误:

代码语言:javascript
复制
--------------------------------------------------48--------------------------------------------------
--------------------------------------------------49--------------------------------------------------
Traceback (most recent call last):
  File "KaggleTitanic[2-FE]--[01].py", line 350, in <module>
    scale_all_features()
  File "KaggleTitanic[2-FE]--[01].py", line 332, in scale_all_features
    combined[features] = combined[features].apply(lambda x: x/x.max(), axis=0)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4061, in apply
    return self._apply_standard(f, axis, reduce=reduce)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4157, in _apply_standard
    results[i] = func(v)
  File "KaggleTitanic[2-FE]--[01].py", line 332, in <lambda>
    combined[features] = combined[features].apply(lambda x: x/x.max(), axis=0)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py", line 651, in wrapper
    return left._constructor(wrap_results(na_op(lvalues, rvalues)),
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/ops.py", line 592, in na_op
    result[mask] = op(x[mask], y)
TypeError: ("unsupported operand type(s) for /: 'str' and 'str'", u'occurred at index Ticket')

这里有什么问题?前面的49个部分都没有问题,所以如果我得到了一个错误,到现在它就会显示出来,对吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-15 19:37:29

您可以帮助确保数学转换只发生在具有以下内容的数字列上。

代码语言:javascript
复制
numeric_cols = combined.columns[combined.dtypes != 'object']
combined.loc[:, numeric_cols] = combined[numeric_cols] / combined[numeric_cols].max()

没有必要使用该应用函数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41171613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档