scikit-learn中的弃用警告

时间:2019-01-09 14:26:36

标签: python scikit-learn

大家好,我正在学习机器学习,一开始代码运行良好,但是第二天,当我再次执行代码时,它开始警告我要注意数据集中丢失的数据,我不知道这是什么问题,但是有谁知道解决方案

源代码:

import numpy as np

import matplotlib.pyplot as plt

import pandas as pd

dataset = pd.read_csv('Data.csv')

x = dataset.iloc[:, :-1]

y = dataset.iloc[:, 3]


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])

这里是警告:

DeprecationWarning: Class Imputer is deprecated; Imputer was deprecated in version 0.20 and will be removed in 0.22. Import impute.SimpleImputer from sklearn instead.

6 个答案:

答案 0 :(得分:2)

SimpleImputer的工作原理几乎与旧的Imputer相似,只是导入并使用它。不再使用Imputer。

from sklearn.impute import SimpleImputer

https://scikit-learn.org/stable/modules/generated/sklearn.impute.SimpleImputer.html

答案 1 :(得分:2)

from sklearn.impute import SimpleImputer

imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean', verbose = 0)

imputer = imputer.fit(X[:, 1:3])
X[:, 1:3] = imputer.transform(X[:, 1:3])

答案 2 :(得分:1)

尝试一下。 在新的python版本中,SimpleImputer有效。

from sklearn.impute import SimpleImputer

imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean',verbose=0)

imputer = imputer.fit(X[:, 1:3])

X[:, 1:3] = imputer.transform(X[:, 1:3])

答案 3 :(得分:1)

照顾丢失的数据

from sklearn.impute import SimpleImputer

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

imputer = imputer.fit(X.iloc[:, 1:3])
X = imputer.transform(X.iloc[:, 1:3])

在第3行和第4行中使用.iloc会有所帮助!

答案 4 :(得分:0)

Imputer仍然可以使用,只需添加其余参数(详细信息和副本)并在必要时填写它们即可。

from sklearn.preprocessing import Imputer

imputer = Imputer(missing_values="NaN", strategy="mean", axis=0, verbose=0, copy="True")

imputer = imputer.fit(X[:, 1:3])

X[:, 1:3] = imputer.transform(X[:, 1:3]))

答案 5 :(得分:0)

from sklearn.impute import SimpleImputer

imputer = SimpleImputer(missing_values = np.nan, strategy = 'mean',verbose=0)

imputer = imputer.fit(X[:, 1:3])

X[:, 1:3] = imputer.transform(X[:, 1:3])