Python Sklearn - RandomForest和Missing值

时间:2014-08-25 07:33:48

标签: python scikit-learn random-forest

我试图在包含缺失值的数据集上执行RandomForest。

我的数据集如下:

train_data = [['1' 'NaN' 'NaN' '0.0127034' '0.0435092']
 ['1' 'NaN' 'NaN' '0.0113187' '0.228205']
 ['1' '0.648' '0.248' '0.0142176' '0.202707']
 ..., 
 ['1' '0.357' '0.470' '0.0328121' '0.255039']
 ['1' 'NaN' 'NaN' '0.00311825' '0.0381745']
 ['1' 'NaN' 'NaN' '0.0332604' '0.2857']]

归咎于" NaN"价值,我使用:

from sklearn.preprocessing import Imputer

imp=Imputer(missing_values='NaN',strategy='mean',axis=0)
imp.fit(train_data[0::,1::])
new_train_data=imp.transform(train_data)

但我收到以下错误:

Traceback (most recent call last):
  File "./RandomForest.py", line 72, in <module>
    new_train_data=imp.transform(train_data)
  File "/home/aurore/.local/lib/python2.7/site-packages/sklearn/preprocessing    /imputation.py", line 388, in transform
    values = np.repeat(valid_statistics, n_missing)
  File "/usr/lib/python2.7/dist-packages/numpy/core/fromnumeric.py", line 343, in repeat
    return repeat(repeats, axis)
ValueError: a.shape[axis] != len(repeats)

我做到了:

new_train_data = imp.fit_transform(train_data)

然后我收到此错误:

Traceback (most recent call last):
  File "./RandomForest.py", line 82, in <module>
    forest = forest.fit(train_data[0::,1::],train_data[0::,0])
  File "/home/aurore/.local/lib/python2.7/site-packages/sklearn/ensemble/forest.py", line 224, in fit
    X, = check_arrays(X, dtype=DTYPE, sparse_format="dense")
  File "/home/aurore/.local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 283, in check_arrays
    _assert_all_finite(array)
  File "/home/aurore/.local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 43, in _assert_all_finite
    " or a value too large for %r." % X.dtype)
 ValueError: Input contains NaN, infinity or a value too large for dtype('float32').

包裹有问题吗? 有人可以帮帮我吗?这是什么意思?

1 个答案:

答案 0 :(得分:3)

您可以在列1::上训练电影,但是您会尝试将其应用于所有列。这不起作用。做

new_train_data = imp.fit_transform(train_data)