Sklearn.impute输入包含无穷大或对于dtype('float64')而言太大的值

时间:2019-07-24 21:20:15

标签: python-3.x pandas numpy scikit-learn

我想用sklearn np.nan的平均值填充SimpleImputer值,但是引发了valueError

from sklearn.impute import SimpleImputer

imputer = SimpleImputer(strategy='mean')
imputer.fit(house_numeric)

2 个答案:

答案 0 :(得分:0)

我尝试了您的代码并出现错误:

ValueError: Expected 2D array, got 1D array instead:
array=[ 1.  2.  3.  4. nan  7.  8.  9.].
Reshape your data either using array.reshape(-1, 1) if your data has a single 
feature or array.reshape(1, -1) if it contains a single sample.

如果您的错误相同,请尝试重塑您的house_numeric,以便:

house_numeric = house_numeric.reshape(-1,1)

答案 1 :(得分:0)

我认为上面的答案非常好。首先,重塑数据,然后重新设计。

from sklearn.preprocessing import Imputer

# Setup the Imputation transformer: imp
imp = Imputer(missing_values='NaN', strategy='mean', axis=0)