Python Sklearn – RandomForest和Missing值

前端之家收集整理的这篇文章主要介绍了Python Sklearn – RandomForest和Missing值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在包含缺失值的数据集上执行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 ::上训练imputer,但之后您尝试将其应用于所有列.这不起作用.做
new_train_data = imp.fit_transform(train_data)
原文链接:https://www.f2er.com/python/446477.html

猜你在找的Python相关文章