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

使用cl.fit()在python中进行SVM()分类器训练,python,svm,feature-detection,Python,Svm,Feature Detection,我正在制作一个程序,使用以下代码在python中训练SVM(支持向量机)分类器: ` 但我得到的结果是: 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.p

我正在制作一个程序,使用以下代码在python中训练SVM(支持向量机)分类器: `

但我得到的结果是:

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.

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

您可以打印
功能。shape
以确认我的假设,或者打印功能,就像您对标签所做的那样,让我们看看这个变量看起来如何解决了这个问题,我基本上是在应用HOG进行特征提取,cells_per_block=(5,5)被改为(2,2),这是有效的,但无论如何,我有1722张图片,我正在训练,我不也应该有1722个特征吗?是吗?据我所知,特征和样本是两码事。如果你有1722张图片,你将有1722张样本,但这并不一定意味着你将从中提取1722个特征。我做了一个快速搜索来说明这一点,我在样本矩阵及其特征上找到的第一张图像,“example1…exampleN”将是你的图片,大小为1722,但从中找到/提取的特征,矩阵中的列可以是任何值()
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.
ValueError: Found array with 0 feature(s) (shape=(1722, 0)) while a minimum of 1 is required.