简体   繁体   English

python中的数组形状错误

[英]Bad array shape in python

I am trying to implement SVM for a dataset I found online.The features_test,features_train,labels_test,labels_train are python lists of tuples.I did the below to convert it into numpy arrays.But clf.fit is giving me the below error. 我正在尝试为在网上找到的数据集实现SVM.featurestest,features_train,labels_test,labels_train是元组的python列表。我做了以下操作将其转换为numpy数组。但是clf.fit给了我以下错误。

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)

Code is as below 代码如下

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

Even from the error itself it is easy to notice that your labels matrix is two-dimensional, while it should be 1D vector. 即使从错误本身来看,也很容易注意到标签矩阵是二维的,而它应该是一维向量。 It should contain on i'th position - label of i'th example. 它应包含在第i个位置-第i个示例的标签。 In your case it looks like each sample has 9 labels, which is not supported by sklearn SVM. 在您的情况下,看起来每个样本都有9个标签,sklearn SVM不支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM