python3.7下载sklearn_Python3.7sklearn包中缺失Imputer函数

在Python3.7的sklearn库中,Imputer函数已从preprocessing模块移至impute模块,变为SimpleImputer。错误提示表明尝试从preprocessing导入Imputer失败。解决方案是使用`from sklearn.impute import SimpleImputer`来代替。此外,sklearn还新增了KNNImputer和IterativeImputer等填充缺失值的方法。
摘要由CSDN通过智能技术生成

本人是win10下安装的anaconda3,相关版本如下:

然而在使用sklearn中的Imputer函数时,会出现报错:

>>> import numpy as np

>>> import sklearn

>>> from sklearn import preprocessing

>>> from sklearn.preprocessing import Imputer

Traceback (most recent call last):

File "", line 1, in ImportError: cannot import name 'Imputer' from 'sklearn.preprocessing' (E:\python\lib\site-packages\sklearn\preprocessing\__init__.py)

利用dir()查看包内的函数,发现没有Imputer:

>>> dir(sklearn.preprocessing)

['Binarizer', 'FunctionTransformer', 'KBinsDiscretizer', 'KernelCenterer', 'LabelBinarizer', 'LabelEncoder', 'MaxAbsScaler', 'MinMaxScaler', 'MultiLabelBinarizer', 'Normalizer', 'OneHotEncoder', 'OrdinalEncoder', 'PolynomialFeatures', 'PowerTransformer', 'QuantileTransformer', 'RobustScaler', 'StandardScaler', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_csr_polynomial_expansion', '_data', '_discretization', '_encoders', '_function_transformer', '_label', 'add_dummy_feature', 'binarize', 'label_binarize', 'maxabs_scale', 'minmax_scale', 'normalize', 'power_transform', 'quantile_transform', 'robust_scale', 'scale']

原因:0.22版本sklearn,imputer不在preprocessing里了,而是在sklearn.impute里,除了SimpleImputer外,还增加了KNNImputer。另外还有IterativeImputer用法,可以参考这个 https://scikit-learn.org/stable/modules/impute.html#impute 以及 https://scikit-learn.org/stable/modules/preprocessing.html#preprocessing

from sklearn.impute import SimpleImputer

imp = SimpleImputer()

imp.fit([[1, 2],

[np.nan, 3],

[7, 6]])

imp.transform([[np.nan, 2],

[6, np.nan]])

array([[4. , 2. ],

[6. , 3.66666667]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值