Ask Your Question

Python SVM Function svm.predict(testData) Returns Tuple instead of Numpy Array

asked 2017-05-19 19:49:59 -0500

kuntoro gravatar image

updated 2017-05-19 20:57:21 -0500


I have searched through this forum about this problem and I found no result. I wish that this question has never been asked before. I am very sorry if it has.

I test machine learning sample code from and it returns an error:

cv2.error: C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\ml\  src\svm.cpp:1618: error: (-5) in the case of classification problem the response  s must be categorical; either specify varType when creating TrainData, or pass i  nteger responses in function cv::ml::SVMImpl::train

I tried to fixed it by changing the response type from float to int, from

responses = np.float32(np.repeat(np.arange(10),250)[:,np.newaxis])


responses = np.repeat(np.arange(10),250)[:,np.newaxis]

However, the outcome of the classification is still not shown properly.

After some investigation, I found a solution. This function call

result = svm.predict(testData)

does not return an array of result but it returns a tuple instead. I believe this is the output of print(result)

(0.0, array([[ 0.],
       [ 0.],
       [ 0.],
       [ 9.],
       [ 9.],
       [ 9.]], dtype=float32))

So, instead of writing

mask = result==responses

the solution that worked for me is

mask = result[1] == responses

I don't know, but maybe the tutorial needs update. My question is, what value is the first element of the tuple which returned by svm.predict(testData) ? I have tried to find documentation but unfortunately I couldn't find any information about this function.

Thank you very much for your kind help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-05-19 23:15:18 -0500

berak gravatar image

well , you're right in both cases, and the tutorial sure needs an update !

for a classification, the responses need to be integer, indeed. and the weird tuple from the prediction maybe gets easier to understand, if we look at the c++ signature . a single float value is returned, if you predict on a single feature (which is probably the most common case). if you predict on several features (all 250 at once in the demo), you'd pass an empty result Mat to hold the 250 results in c++, while in python a tuple of both the single value and the responses array is returned (yea, clumsy).

edit flag offensive delete link more


made a pr here, would you care to make an issue for it ?

berak gravatar imageberak ( 2017-05-20 01:14:14 -0500 )edit

I'm not so familiar with the environment. However, very well, I'll try to post there.

kuntoro gravatar imagekuntoro ( 2017-05-24 21:54:48 -0500 )edit

Question Tools

1 follower


Asked: 2017-05-19 19:49:59 -0500

Seen: 1,373 times

Last updated: May 19 '17