当前位置: 代码迷 >> 综合 >> sklearn :ImportError: cannot import name ‘Imputer‘
  详细解决方案

sklearn :ImportError: cannot import name ‘Imputer‘

热度:85   发布时间:2023-12-15 17:35:32.0

在学习中国大学mooc中学习Python机器学习应用(礼欣、嵩天)里人体运动...程序编写遇到的问题

# 模块导入
import numpy as np
import pandas as pdfrom sklearn.preprocessing import Imputer
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report

问题1:导入Imputer出错

ImportError                               Traceback (most recent call last)
<ipython-input-55-67f8b87066ad> in <module>()3 import pandas as pd4 
----> 5 from sklearn.preprocessing import Imputer6 from sklearn.model_selection import train_test_split7 from sklearn.metrics import classification_reportImportError: cannot import name 'Imputer'

原因是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 

问题2:

ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
Imputer.(missing_values='NaN', strategy='mean')

需要改为

impute.SimpleImputer(missing_values=np.nan, strategy='mean')

 

课程其他问题:

SystemError: new style getargs format but argument is not a tuple[与putpixel(xy, value)相关]
https://blog.csdn.net/weixin_43584807/article/details/104209292

  相关解决方案