2

I'm trying to figure out what is the proper format of a python list to be given as input to a svm_problem function in python. I got the following program from the web, stackoverflow.

I have the following:

from svm import *
x=[ [1,0,1],[-1,0,-1],[1,0,0]]
#x=[ [1,0,1],[-1,0,-1]]
prob = svm_problem( [1,-1],x  )
param = svm_parameter(kernel_type = LINEAR, C = 10)
m = svm_model(prob, param)
print m.predict([ 1,1, 1])

It raises an assertion error, says assert failed: assert len(x)==len(y).

But if x=[ [1,0,1],[-1,0,-1]], the program works perfectly. Am I not supposed to give a train-data problem of length more than 2?

Also I don't understand what in x=[[1,0,1],[-1,0,-1]] is a label and what is the data?

Any help is highly appreciated.

2 Answers 2

2

svm_problem() takes two parameters: the first parameter of a vector of labels, and the second is a matrix of features. You get this assertion error because you are only specifying 2 labels, [1, -1], as the first parameter in your call to svm_problem.

Example:

y = [1,-1,1,1]
x = [[1,0,1], [-1,0,-1], [1,2,3], [4,5,6]]
prob = svm_problem(y, x)
Sign up to request clarification or add additional context in comments.

1 Comment

I just figured that out. THank you very much
0

If you give 3 examples, you need to give the classes for the three inputs as well, so you need to do

prob = svm_problem( [1,1,-1],x  )

or something similar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.