【问题标题】:While normalizing the data in python, getting this error:Error evaluating factor: TypeError: list indices must be integers or slices, not str在 python 中对数据进行规范化时,出现此错误:错误评估因子:TypeError: list indices must be integers or slices, not str
【发布时间】:2019-07-23 05:03:54
【问题描述】:

我的数据:

absent_data.head()

My data

在它顺利运行之前,这是我的代码:

cnames = ['Transportation expense', 'Distance from Residence to Work',
   'Service time', 'Age', 'Work load Average/day ', 'Hit target',
   'Son', 'Pet', 'Weight','Body mass index',
   'Absenteeism time in hours']

#Nomalization
for i in cnames:
    print(i)
    absent_data[i] = (absent_data[i] - min(absent_data[i]))/(max(absent_data[i]) - min(absent_data[i]))

现在出现这个错误:

TypeError Traceback(最近调用 最后)在() 2 for i 在 cnames 中: 3 打印(一) ----> 4 缺席数据[i] = (缺席数据[i] - min(缺席数据[i]))/(max(缺席数据[i]) - min(缺席数据[i]))

TypeError: 'numpy.float64' 对象不可调用

以前也是浮动的。

【问题讨论】:

  • 什么是absent_data
  • @Ev.Kounis 这是我的数据集。
  • 例如,您在执行absent_data['Transportation expense'] 时会发生什么?因为这就是你正在做的......
  • for i in cnames 改为for i in range(len(cnames)):
  • 什么是type(absent_data)

标签: python normalization


【解决方案1】:

我猜你正在为minmax 分配一个浮点值,而minmax 函数被覆盖。重新启动您的笔记本并删除覆盖部分,然后您将获得预期的结果。

否则,直接使用sklearn.preprocessing.MinMaxScaler,而不是自己实现。

Normalize columns of pandas data frame

【讨论】:

  • 相同的代码在另一个数据集上工作正常,我也有浮点值。
  • 你的笔记本里有min = ...max = ...这样的行吗?这将覆盖函数并导致is not callable 错误。
  • 我的笔记本中有 min=.. max=... ,这是在规范化代码之后。这就是它第一次运行良好并且再次运行整个代码后出现错误的原因。是的,我会使用 sklearn 库来完成。非常感谢。
猜你喜欢
  • 2022-11-16
  • 2019-09-28
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2019-07-23
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
相关资源
最近更新 更多