sklearn: TypeError: fit() 缺少 1 个必需的位置参数: 'x'



尝试运行此内容时

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values ="NaN", strategy = "mean")
imputer = SimpleImputer.fit(X[:,1:3])
X[:,1:3] = SimpleImputer.transform(X[:,1:3])

我收到错误

类型错误: fit() 缺少 1 个必需的位置参数:"X"

但是我已经提供了x,对吧?对此的解决方案是什么?

根据这个Scikit-learn模块,正确的语法应该是:

imputer.fit(X[:,1:3])

而不是:

imputer = SimpleImputer.fit(X[:,1:3])

完全工作的代码如下所示:

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(missing_values = np.nan, strategy = "mean")
imputer = imputer.fit(X[:,1:3])
X[:,1:3] = imputer.transform(X[:,1:3])

请注意:

missing_values = np.nan

您的代码:

from sklearn.impute import SimpleImputer
# PAY ATTENTION: to NaN as np.nan
imputer = SimpleImputer(**missing_values ="NaN"**, strategy = "mean")
imputer = SimpleImputer.fit(X[:,1:3])
# PAY ATTENTION: instead of "SimpleImputer.transform" use "imputer.transform"
X[:,1:3] = **SimpleImputer**.transform(X[:,1:3])

正确的代码 :

from sklearn.impute import SimpleImputer  
imputer = SimpleImputer(missing_values=np.nan, strategy='mean',fill_value=None, verbose=0, copy=True)  
imputer = imputer.fit(X[:, 1:3])  
X[:, 1:3] = imputer.transform(X[:, 1:3])

相关内容

  • 没有找到相关文章

最新更新