2016-04-01 40 views
0

我想實現我在線上找到的數據集的SVM。features_test,features_train,labels_test,labels_train是pyple列表的tuples.I做了下面的將它轉換成numpy arrays.But clf.fit給我下面的錯誤。Python中的陣列形狀不好

File "ebola.py", line 47, in <module> 
clf.fit(features_train_numpy,labels_train_numpy) 
File "/usr/lib64/python2.7/site-packages/sklearn/svm/base.py", line 151, in fit 
y = self._validate_targets(y) 
File "/usr/lib64/python2.7/site-packages/sklearn/svm/base.py", line 514, in _validate_targets 
y_ = column_or_1d(y, warn=True) 
File "/usr/lib64/python2.7/site-packages/sklearn/utils/validation.py", line 551, in column_or_1d 
raise ValueError("bad input shape {0}".format(shape)) 
ValueError: bad input shape (2923, 9) 

代碼是如下

features_train_numpy = np.asarray(features_train) 
labels_train_numpy= np.asarray(labels_train) 
features_test_numpy = np.asarray(features_test) 
labels_test_numpy= np.asarray(labels_test) 
from sklearn.svm import SVC 
temp = 100 
clf=SVC(C=temp,kernel="rbf") 
clf.fit(features_train_numpy,labels_train_numpy)` 
+1

每個陣列的形狀是什麼?通過'print XXX.shape'? – lejlot

+0

此代碼是給我下面的錯誤 回溯(最近通話最後一個): 文件「ebola.py」 58行,在 打印np.ndarray.shape(features_train_numpy) 類型錯誤:「getset_descriptor」對象不是可調用 該代碼是這個 features_train_numpy = np.asarray(features_train) labels_train_numpy = np.asarray(labels_train) features_test_numpy = np.asarray(features_test) labels_test_numpy = np.asarray(labels_test) 打印np.ndarray.shape( features_train_numpy) – Anjan

+0

對不起,我的評論風格,首先,我在這裏是新的,第二,我給了cpdes的4個空格和評論b中的錯誤它沒有工作 – Anjan

回答

0

即使從錯誤本身很容易注意到你的標籤矩陣是二維的,而應該是一維向量。它應該包含第i個位置 - 第i個示例的標籤。在你的情況下,它看起來像每個樣本有9個標籤,sklearn支持SVM不支持。