Solved – How to calculate the margin in SVM light

classificationmachine learningmax-marginsvm

I'm using Support Vector Machine in a project. The library chosen is SVM light of Joachims: http://svmlight.joachims.org/

I have the need to calculate the margin. Namely, given a training set of data I have to calculate the margin of the better hyperplane found. But I do not see a direct way to do this in svm light. So I'll ask you to know how to do it.
The data should be linearly separable and in this case I expect a positive margin, but there is also the remote possibility that in some case the data arent't linearly separable and in this case I expect a negative margin. (I use hard margin)
Is there a possibility to do that in this library?

Best Answer

For the linearly separable case (using linear kernel), there is a script on SVM Light site's FAQ that can give you the weight vector (w) from the svm_model output file, http://www.cs.cornell.edu/people/tj/svm%5Flight/svm_light_faq.html (look for the question "How can I get the weight vector of the hyperplane for a linear SVM?").

With the weight vector you can calculate the margin by doing one over its norm: $\frac{1}{\lVert w \rVert}$

Related Question