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

时间:2019-01-19 11:23:50

标签: python scikit-learn

尝试运行此

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])

我遇到错误

  

TypeError:fit()缺少1个必需的位置参数:“ X”

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

3 个答案:

答案 0 :(得分:2)

根据this Scikit-learn module,正确的语法应为:

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

代替:

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

答案 1 :(得分:1)

完全工作的代码如下:

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

答案 2 :(得分:0)

您的代码:

def add_to_db(d):
    report  = Report(**d)
    db.session.add(report)
    db.session.commit()

正确的代码:

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])