【问题标题】:Getting an error when applying sklearn StandardScaler on Python在 Python 上应用 sklearn StandardScaler 时出错
【发布时间】:2022-02-20 06:40:29
【问题描述】:

我正在使用具有分类和数字列的 DataFrame。我正在尝试缩放数字列,因为数字非常极端。我尝试使用下面的代码,但它不断出现错误。我只指出了要应用的数字列。

from sklearn.preprocessing import StandardScaler

numerical = df.iloc[:, 6:24]
scaler = StandardScaler()
scaled_df = scaler.fit_transform(df[numerical])

错误是 ValueError:条件需要的布尔数组,而不是 float64

我不明白是什么原因造成的。我已尝试更新我正在使用的 pandas 版本,但没有修复它。

【问题讨论】:

  • 定义numerical 的行实际上将返回DataFrame。所以检查type(numerical)。如果你运行scaled_df = scaler.fit_transform(numerical),它可能会起作用。
  • 另外,如果您不想硬编码数字列的位置,您可以使用df.select_dtypes(include=[np.number])(如果您使用别名np 导入了numpy)。

标签: python pandas


【解决方案1】:

numerical 已经是一个数据帧(df 的子集),所以 df[numerical] 在这里没有意义:

scaled_df = scaler.fit_transform(numerical)

如果你只尝试:

>>> df[numerical]
...
ValueError: Boolean array expected for the condition, not float64

【讨论】:

    猜你喜欢
    • 2020-01-10
    • 2016-08-25
    • 2018-08-23
    • 2022-01-12
    • 2019-02-17
    • 2020-07-16
    • 2018-12-29
    • 2018-03-15
    • 1970-01-01
    相关资源
    最近更新 更多