成功解决(机器学习填补数值型缺失值时报错):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'

在这里插入图片描述



报错原因

报错原因: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.]]

猜你喜欢

转载自blog.csdn.net/yuan2019035055/article/details/125298469