Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

Hi,

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 http://docs.opencv.org/3.2.0/dd/d3b/tutorial_py_svm_opencv.html and the result yields 0% accuracy.

After some investigation, 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.

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

Hi,

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 http://docs.opencv.org/3.2.0/dd/d3b/tutorial_py_svm_opencv.html and the result yields 0% accuracy.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])

to

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

After that, the outcome of the classification is still not shown properly.

After some investigation, 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.

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

Hi,

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 http://docs.opencv.org/3.2.0/dd/d3b/tutorial_py_svm_opencv.html 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. int, from

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

to

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

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

After some investigation, this 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.