成功解决(机器学习填补数值型缺失值时报错):TypeError: init() got an unexpected keyword argument ‘axis’

成功解决(机器学习填补数值型缺失值时报错):TypeError: init() got an unexpected keyword argument ‘axis’




文章目录

  • 报错问题
  • 报错原因
  • 解决方法


报错问题

我的代码

from sklearn.impute import SimpleImputer
import numpy as np

def si():
    sip = SimpleImputer(missing_values=np.nan, strategy='mean', axis=0)
    data = sip.fit_transform([[1, 2],
                             [np.nan, 3],
                             [7, 6]]
                            )
    print(data)


si()

报错内容

Traceback (most recent call last):
  File "E:/Python/1.py", line 68, in <module>
    si()
  File "E:/Python/1.py", line 62, in si
    si = SimpleImputer(missing_values='Nan',strategy='mean',axis=0)
TypeError: __init__() got an unexpected keyword argument 'axis'

成功解决(机器学习填补数值型缺失值时报错):TypeError: init() got an unexpected keyword argument ‘axis’_第1张图片



报错原因

报错原因:0.20新版功能:SimpleImputer取代了以前的sklearn.preprocessing.Imputer;而SimpleImputer参数中没有axis

SimpleImputer类参数如下:

SimpleImputer(missing_values=np.nan,strategy="mean",fill_value=None,verbose=0,copy=True,add_indicator=False)


解决方法

去掉代码中的axis=0参数,修改后代码如下:

from sklearn.impute import SimpleImputer
import numpy as np

def si():
    sip = SimpleImputer(missing_values=np.nan, strategy='mean')
    data = sip.fit_transform([[1, 2],
                             [np.nan, 3],
                             [7, 6]]
                            )
    print(data)


si()

再次运行成功:

[[1. 2.]
 [4. 3.]
 [7. 6.]]

你可能感兴趣的:(《告别Bug》,sklearn,python,人工智能,机器学习)