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

本人是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]])

你可能感兴趣的:(python3.7下载sklearn_Python3.7sklearn包中缺失Imputer函数)