sklearn :ImportError: cannot import name ‘Imputer‘

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

# 模块导入
import numpy as np
import pandas as pd

from 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)
 in ()
      3 import pandas as pd
      4 
----> 5 from sklearn.preprocessing import Imputer
      6 from sklearn.model_selection import train_test_split
      7 from sklearn.metrics import classification_report

ImportError: 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

你可能感兴趣的:(Python,sklearn,机器学习)