Lijin Durairaj
Lijin Durairaj

Reputation: 5232

How to calculate score while using SVM?

I am new to machine learning, I am a bit confused by the documentation of the sklearn on how to get the score while using sklearn.svm.SVC.

This is my code

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.30)

for _c in [0.4,0.6,0.8,1.0,1.2,1.4]:
    svm=SVC(C=_c,kernel='linear')
    svm.fit(x_train,y_train)
    result=svm.predict(x_test)
    print('C value is {} and score is {}'.format(_c,svm.score(x_test,y_test)))

This is the output

C value is 0.4 and score is 0.0091324200913242
C value is 0.6 and score is 0.0091324200913242
C value is 0.8 and score is 0.0091324200913242
C value is 1.0 and score is 0.0091324200913242
C value is 1.2 and score is 0.0091324200913242
C value is 1.4 and score is 0.0091324200913242

I see all the score are same, my question how to determine the best score of my model?

  1. should I pass the predicted value to svm.score y value i.e.

    result=svm.predict(x_test)
    svm.score(x_test,result))
    
  2. should I pass the x_test and y_test value i.e.

    svm.score(x_test,y_test))
    

Upvotes: 0

Views: 5060

Answers (1)

PV8
PV8

Reputation: 6260

To your question:

  1. It is wrong, you cannot compare features xwith your target y
  2. Same mistake as in 1.

you have to use:

for _c in [0.4,0.6,0.8,1.0,1.2,1.4]:
    svm=SVC(C=_c,kernel='linear')
    svm.fit(x_train,y_train)
    result=svm.predict(x_test)
    print('C value is {} and score is {}'.format(_c,svm.score(y_test,result)))

This will compare your original target values y_test with your predicted values result . That is the idea of testing, you test your prediction against original values to see how good/bad your prediction is.

Upvotes: 1

Related Questions