hihell
hihell

Reputation: 946

sklearn svc with linear kernel provide strange support vectors

code:

from sklearn import svm

def testsvm():
    t_d = [[1,1], [1,-1], [-1,-1], [-1, 1], [-0.0001, 0.0001], [0,0], [-1, -1.0001]]
    y = [1,-1,1,1, 1, 1, -1]

    clf = svm.SVC(kernel='linear', C=1)
    clf.fit(t_d, y)

    print clf.support_vectors_
    print clf

and the result is:

[[ 1.     -1.    ]  
 [-1.     -1.0001]  
 [-1.     -1.    ]  
 [ 0.      0. ]]

but as far as I concern, the support vectors should be:

[[1,1]
 [0,0]
 [-1,-1]
 [-1.     -1.0001]]

shouldn't the [1,-1] leave out if the margins are parallel to each other?

is there anything wrong with my code? if so, please indicate me. many many thanks in advance

Upvotes: 1

Views: 768

Answers (1)

lejlot
lejlot

Reputation: 66805

You are using too small C, and simply you are not getting a "hard margin SVM" you are thinking about, but instead, a "soft" version, which has more support vectors (not exactly on the "margin boundaries").

Set clf = svm.SVC(kernel='linear', C=100000000) and everything should be fine

[[ 1.     -1.    ]
 [-1.     -1.0001]
 [-1.     -1.    ]]

you can use code from http://scikit-learn.org/stable/auto_examples/svm/plot_svm_margin.html to investigate the exact location of your separating hyperplane and margin size.

Upvotes: 2

Related Questions