2016-09-15 73 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

啊,我明白了!謝謝! –