我正在使用以下代码制作一个程序来训练python中的SVM(支持向量机)分类器: `
print ("Fetching saved features and corresponding labels from disk")
features, labels = load_features_labels(1)
print("Features and labels successfully loaded")
print(np.array(labels))
print(len(np.array(features)))
print(len(np.array(labels)))
clf = LinearSVC()
print("Fitting classifier")
clf.fit(np.array(features), np.array(labels))
print("Classifier fitting completed")
print("Persisting classifier model")
pickle.dump(clf, open("clf.p", "wb"))
print("Model persistence completed")
但是我得到了这个输出:
Fetching saved features and corresponding labels from disk
Features and labels successfully loaded
[1 1 1 ..., 0 0 0]
1722
1722
Fitting classifier
Traceback (most recent call last):
File "train.py", line 20, in <module>
clf.fit(np.array(features), np.array(labels))
File "/home/ws2/anaconda2/lib/python2.7/site-packages/sklearn/svm/classes.py", line 205, in fit
dtype=np.float64, order="C")
File "/home/ws2/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 510, in check_X_y
ensure_min_features, warn_on_dtype, estimator)
File "/home/ws2/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 415, in check_array
context))
ValueError: Found array with 0 feature(s) (shape=(1722, 0)) while a minimum of 1 is required.
从输出中可以看出,要素和标签的总数相等,为i-e 1722.为什么显示此错误:
ValueError: Found array with 0 feature(s) (shape=(1722, 0)) while a minimum of 1 is required.
答案 0 :(得分:1)
看起来你的features数组不正确,它应该有一个类似于这个np.array([[1,2,3],[2,2,2],[1,1,1]])
的表单,更像是一个数组数组。我怀疑你可能有这样的事情np.array([1,2,3,2,2,2,1,1,1])
?
基本上fit
方法需要一个n_samples和n_features数组,来自文档&#34; fit(X,y)X:{array-like,sparse matrix},shape = [n_samples ,n_features] &#34;。您的错误消息告诉您没有提供任何功能,这就是为什么我怀疑而不是数组[n_samples,n_features]作为X参数,您只是在为数组提供数据。