2016-09-15 78 views
0

我的代码是很简单,但它总是弹出这样的警告:折旧警告当我使用sklearn imputer

DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and will 
raise ValueError in 0.19. Reshape your data either using X.reshape(-1, 1) if 
your data has a single feature or X.reshape(1, -1) if it contains a single sample. 
(DeprecationWarning) 

我不知道为什么它,即使我在添加s.reshape(-1,1)不起作用圆括号fit_transforms

的代码如下:

import pandas as pd 

s = pd.Series([1,2,3,np.nan,5,np.nan,7,8]) 
imp = Imputer(missing_values='NAN', strategy='mean', axis=0) 
x = pd.Series(imp.fit_transform(s).tolist()[0]) 
x 
+0

请尝试导入您正在使用的所有库。例如,presumba'pd'是熊猫,但不确定每个人都会猜到。 –

+0

那么你是否会收到警告或错误?如果出现警告,代码仍然应该执行,所以如果没有,您可能会遇到其他问题 – UnholySheep

回答

0

除了警告,您的片段并没有被我由于缺少import小号解释和错误(ufunc: isnan not supported)一旦我得到了一些进口到位。

此代码运行没有警告或错误,但:

In [39]: import pandas as pd 

In [40]: import numpy as np 

In [41]: from sklearn import preprocessing 

In [42]: s = pd.Series([1,2,3,np.nan,5,np.nan,7,8]) 

In [43]: imp = preprocessing.Imputer(strategy='mean', axis=0) 

In [44]: x = pd.Series(imp.fit_transform(s.values.reshape(1, -1)).tolist()[0]) 

In [45]: x 
Out[45]: 
0 1.0 
1 2.0 
2 3.0 
3 5.0 
4 7.0 
5 8.0 
dtype: float64 

注意以下几点:

  1. import小号

  2. preprocessing.Imputer(strategy='mean', axis=0)省去您NAN规范(应该是NaN,但由于NaN是默认设置,因此您可以将其删除)。

  3. x = pd.Series(imp.fit_transform(s.values.reshape(1, -1)).tolist()[0])从1d阵列重塑为2d阵列。

您的警告是关于第3点 - 此功能需要2d矩阵,而不是1d向量。

+0

非常感谢您的回答!对不起,我没有粘贴完整的导入语句....但为什么我应该删除NaN规范?我删除它,然后它的作品! –

+0

如果你看[文档](http://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.Imputer.html),你可以告诉你写了'NAN',它应该在那里'NaN'。由于这是默认值,您可以将其删除。 –

+0

啊,我明白了!谢谢! –