2
$\begingroup$

I'm trying to understand how to plot SVM hyperplane and its margins by this example: https://scikit-learn.org/stable/auto_examples/svm/plot_svm_margin.html

And I got stuck at the plotting the parallels part:

# plot the parallels to the separating hyperplane that pass through the
# support vectors (margin away from hyperplane in direction
# perpendicular to hyperplane). This is sqrt(1+a^2) away vertically in
# 2-d.
margin = 1 / np.sqrt(np.sum(clf.coef_ ** 2))
yy_down = yy - np.sqrt(1 + a ** 2) * margin
yy_up = yy + np.sqrt(1 + a ** 2) * margin

At the comment section we see:

This is sqrt(1+a^2) away vertically in 2-d.

So, my question is why np.sqrt(1 + a ** 2) * margin is the vertical distance from a hyperplane to its parallel line? How did we come up with it?

*Note: a here is just a slope of a hyperplane, and margin is a magnitude of the distance between the hyperplane and the parallel dashed line.

I also drew a picture as I can see the problem:

enter image description here

$\endgroup$

1 Answer 1

1
$\begingroup$

This boils down to trigonometry. The distance between the points in the y axis that you represent here by a red line is calculated by multiplying the distance between the two parallel lines (here represented by the margin) and the sqrt(1 + a ** 2).

You can get to this by using two elements:

  1. this equation, often used to represent a line: y = mx + c.
  2. the slope, since parallel lines have the same slope.

More information can be found in the following link:

https://www.geeksforgeeks.org/what-is-the-distance-between-two-parallel-lines/

Hope this helps.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.