pythonpython-3.xscikit-learn

ImportError: cannnot import name 'Imputer' from 'sklearn.preprocessing'


Trying to import Imputer from sklearn.preprocessing,

from sklearn.preprocessing import Imputer

Shows

ImportError: cannot import name 'Imputer' from 'sklearn.preprocessing' (/home/codeknight13/anaconda3/lib/python3.7/site-packages/sklearn/preprocessing/__init_\_.py)"

Solution

  • from sklearn.preprocessing import Imputer was deprecated with scikit-learn v0.20.4 and removed as of v0.22.2. See the sklean changelog.

    from sklearn.impute import SimpleImputer
    import numpy as np
    
    imputer = SimpleImputer(missing_values=np.nan, strategy='mean')
    

    pip install scikit-learn==0.20.4 or conda install scikit-learn=0.20.4 are not a good options because scikit-learn==0.20.4 is more than 3 years out of date.