python - 使用 cl.fit() 在 python 中进行 SVM() 分类器训练

标签 python svm feature-detection

我正在制作一个程序,使用以下代码在 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.

从输出中可以看出,特征和标签的总数等于 1722。为什么显示此错误:

ValueError: Found array with 0 feature(s) (shape=(1722, 0)) while a minimum of 1 is required.

最佳答案

看起来你的特征数组不正确,它应该有一个类似这样的形式 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 的数组,来自文档“fit(X,y) X : {array-like, sparse matrix}, shape = [n_samples , n_features]”。您的错误消息告诉您没有提供任何功能,这就是为什么我怀疑您只是在提供数组而不是数组 [n_samples,n_features] 作为 X 参数。

关于python - 使用 cl.fit() 在 python 中进行 SVM() 分类器训练,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35112431/

相关文章:

python - 如何在 Python 中使用 SVM 进行词性标注?

javascript - 检测 ES6 导入兼容性

python - 如何从 "i view X"打开使用 "SMI"系统创建的 *.idf 眼动仪数据

python - 使用多相机的 3D 点投影

python - 使用索引在 Pandas 中查找两个系列之间的交集

python - 如何发现数据集中的哪些特征具有预测性?

c++ - 矩形不在检测到的眼睛对周围

python - 将数据拆分为单列

python - Scikit 学习定义 max-iter

cv::ml::SVM::trainAuto 的 OpenCV Python 绑定(bind)