python opencv SVM列车错误

时间:2018-02-22 13:06:09

标签: python opencv svm

我想构建一个非常简单的SVM分类器。 所以我写了代码:

import cv2
import numpy as np

# Generate data and labels
trainData = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
trainLabels = np.array([0, 1, 0, 1], dtype=np.float32)

# Create SVM
svm = cv2.ml.SVM_create()
svm.setType(cv2.ml.SVM_C_SVC)
svm.setKernel(cv2.ml.SVM_RBF)
svm.setC(2)
svm.setGamma(2)

# Train : error occurs here.
svm.train(trainData, cv2.ml.ROW_SAMPLE, trainLabels)

# Predict : I don't know if it is appropriate for printing output.
print(svm.predict(np.array([0, 1])))

但是,运行时会发生此错误。

File "C:/.../.py", line 13, in <module>
svm.train(trainData, cv2.ml.ROW_SAMPLE, trainLabels)
cv2.error: C:\ci\opencv_1512688052760\work\modules\ml\src\svm.cpp:1624: error: (-5) in the case of classification problem the responses must be categorical; either specify varType when creating TrainData, or pass integer responses in function cv::ml::SVMImpl::train

我在这里提到:https://www.learnopencv.com/handwritten-digits-classification-an-opencv-c-python-tutorial/

问题是什么?

0 个答案:

没有答案