使用python中的imputer处理NaN

时间:2015-08-19 03:09:15

标签: python machine-learning scikit-learn nan

我正在尝试使用python清理我的数据集。我正在使用scikit的imputer来学习它。我的数据集是一个包含大量“NULL”值的csv文件。在使用panda库的from_csv导入数据并将该数据帧转换为矩阵时,我的数据如下所示:

mainController

然而,现在当我尝试使用imputer时,它会给我以下错误:

[1 '2013-04-04 08:32:15' 12 187 nan nan 219 10404 4 4.0 1 2.2 0.0149 5.03
 26 170.74 0 23246 1 0 4 0 1 1 nan nan 1 nan nan nan nan nan nan 0.0 0.0
 nan nan nan nan 0.0 1.0 nan nan nan nan nan nan nan 0.0 0.0 nan 0 nan 0]

以下是我的代码片段

Traceback (most recent call last):
  File "myRandomForesy.py", line 27, in <module>
    temp[i] = imp.transform(temp[i])
  File "/Users/Sherlock/anaconda/lib/python2.7/site-packages/sklearn/preprocessing/imputation.py", line 331, in transform
    self.axis)
  File "/Users/Sherlock/anaconda/lib/python2.7/site-packages/sklearn/preprocessing/imputation.py", line 252, in _dense_fit
    mask = _get_mask(X, missing_values)
  File "/Users/Sherlock/anaconda/lib/python2.7/site-packages/sklearn/preprocessing/imputation.py", line 30, in _get_mask
    if value_to_mask == "NaN" or np.isnan(value_to_mask):
TypeError: Not implemented for this type

1 个答案:

答案 0 :(得分:0)

即使这已经很老了,但只要是遇到同样问题的人,您都可以替换

imp中的“ nan” = Imputer(missing_values =“ nan”,strategy ='mean',axis = 1)

使用

np.nan

因为数据中的nan值是实际的nan值,而不是字符串