Solved – How does one interpret SVM feature weights

feature selectionpythonscikit learnsvm

I am trying to interpret the variable weights given by fitting a linear SVM.

(I'm using scikit-learn):

from sklearn import svm

svm = svm.SVC(kernel='linear')

svm.fit(features, labels)
svm.coef_

I cannot find anything in the documentation that specifically states how these weights are calculated or interpreted.

Does the sign of the weight have anything to do with class?

Best Answer

For a general kernel it is difficult to interpret the SVM weights, however for the linear SVM there actually is a useful interpretation:

1) Recall that in linear SVM, the result is a hyperplane that separates the classes as best as possible. The weights represent this hyperplane, by giving you the coordinates of a vector which is orthogonal to the hyperplane - these are the coefficients given by svm.coef_. Let's call this vector w.

2) What can we do with this vector? It's direction gives us the predicted class, so if you take the dot product of any point with the vector, you can tell on which side it is: if the dot product is positive, it belongs to the positive class, if it is negative it belongs to the negative class.

3) Finally, you can even learn something about the importance of each feature. This is my own interpretation so convince yourself first. Let's say the svm would find only one feature useful for separating the data, then the hyperplane would be orthogonal to that axis. So, you could say that the absolute size of the coefficient relative to the other ones gives an indication of how important the feature was for the separation. For example if only the first coordinate is used for separation, w will be of the form (x,0) where x is some non zero number and then |x|>0.