scikit-learn.impute 未使用机器学习 A-Z 教程中的代码通过 Spyder 从 Imputer 导入
scikit-learn.impute isn't being imported from Imputer via Spyder using the code from Machine Learning A-Z tutorial
我的代码无法正常工作,因为我从机器学习 A-Z™:动手实践 Python & R In Data Science 教程课程中逐字复制。我正在使用 Python 3.7,我已经在我的环境中安装了 scikit-learn 包。它不起作用,我试图寻找一个有 sklearn 的包,尽管它似乎没有找到任何东西。它给我这个错误。
我通过 Anaconda 运行 我的环境。
ImportError: cannot import name 'Imputer' from 'sklearn.preprocessing' (C:\Users\vygan\.conda\envs\env\lib\site-packages\sklearn\preprocessing\__init__.py)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Data.csv')
X = pd.DataFrame(dataset.iloc[:, :-1].values)
y = pd.DataFrame(dataset.iloc[:, 3].values)
# Taking care of missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])
它从预处理永久转移到估算库,你可以这样称呼它:
from sklearn.impute import SimpleImputer
完全一样。
如果不行的话,你应该用pip卸载它,然后重新安装
第一次可能安装不正确
它不再有轴,但您可以像这样使用 pandas 数据框 header 轻松处理它:
si=SimpleImputer()
si.fit([dataset["headername"]])
有一个策略参数可以让您在 "mean"、"most_frequent"、"median" 和 "constant"
之间进行选择
不过还有一个我更喜欢的imputer:
from sklearn.impute import KNNImputer
这将使用 k 个最近邻居的平均值来估算缺失值
你的代码对我来说工作正常。你有哪个 sklearn 版本?
import sklearn
sklearn.__version__
'0.21.3'
您可以通过以下方式使用 conda 升级软件包:
How to upgrade scikit-learn package in anaconda
更完整的答案:
Imputer
(https://sklearn.org/modules/generated/sklearn.preprocessing.Imputer.html`)
只能在0.19.1及以下版本中找到。
SimpleImputer
出现在最新版本,这就是你需要的。
尝试安装最新版本:
pip install -U scikit-learn # or using conda
然后使用:
from sklearn.impute import SimpleImputer
我遇到了同样的问题,因为库从 preprocessing
更改为 impute
,class 从 Imputer
更改为 SimpleImputer
。
我更改了我的代码如下:
from sklearn.impute import SimpleImputer
simp = SimpleImputer(missing_values = 'NaN', strategy = 'mean')
simp = SimpleImputer().fit(X[:, 1:3])
X[:, 1:3] = simp.transform(X[:, 1:3])
我的代码无法正常工作,因为我从机器学习 A-Z™:动手实践 Python & R In Data Science 教程课程中逐字复制。我正在使用 Python 3.7,我已经在我的环境中安装了 scikit-learn 包。它不起作用,我试图寻找一个有 sklearn 的包,尽管它似乎没有找到任何东西。它给我这个错误。
我通过 Anaconda 运行 我的环境。
ImportError: cannot import name 'Imputer' from 'sklearn.preprocessing' (C:\Users\vygan\.conda\envs\env\lib\site-packages\sklearn\preprocessing\__init__.py)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# Importing the dataset
dataset = pd.read_csv('Data.csv')
X = pd.DataFrame(dataset.iloc[:, :-1].values)
y = pd.DataFrame(dataset.iloc[:, 3].values)
# Taking care of missing data
from sklearn.preprocessing import Imputer
imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis = 0)
imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])
它从预处理永久转移到估算库,你可以这样称呼它:
from sklearn.impute import SimpleImputer
完全一样。 如果不行的话,你应该用pip卸载它,然后重新安装 第一次可能安装不正确
它不再有轴,但您可以像这样使用 pandas 数据框 header 轻松处理它:
si=SimpleImputer()
si.fit([dataset["headername"]])
有一个策略参数可以让您在 "mean"、"most_frequent"、"median" 和 "constant"
之间进行选择不过还有一个我更喜欢的imputer:
from sklearn.impute import KNNImputer
这将使用 k 个最近邻居的平均值来估算缺失值
你的代码对我来说工作正常。你有哪个 sklearn 版本?
import sklearn
sklearn.__version__
'0.21.3'
您可以通过以下方式使用 conda 升级软件包:
How to upgrade scikit-learn package in anaconda
更完整的答案:
Imputer
(https://sklearn.org/modules/generated/sklearn.preprocessing.Imputer.html`)
只能在0.19.1及以下版本中找到。
SimpleImputer
出现在最新版本,这就是你需要的。
尝试安装最新版本:
pip install -U scikit-learn # or using conda
然后使用:
from sklearn.impute import SimpleImputer
我遇到了同样的问题,因为库从 preprocessing
更改为 impute
,class 从 Imputer
更改为 SimpleImputer
。
我更改了我的代码如下:
from sklearn.impute import SimpleImputer
simp = SimpleImputer(missing_values = 'NaN', strategy = 'mean')
simp = SimpleImputer().fit(X[:, 1:3])
X[:, 1:3] = simp.transform(X[:, 1:3])