8000 Fix plot_svm_margin example plots · Pull Request #8051 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

Fix plot_svm_margin example plots #8051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from Dec 16, 2016
Merged

Fix plot_svm_margin example plots #8051

merged 4 commits into from Dec 16, 2016

Conversation

ghost
Copy link
@ghost ghost commented Dec 14, 2016

Reference Issue

The dashed lines which are "margin" away from the separating hyperplane do not pass through the support vectors when they are supposed to.

What does this implement/fix? Explain your changes.

The dashed lines are calculated by shifting of the separating hyperplane to margin distance away by adding and subtracting margin*sqrt(1+(slope of separating hyperplane)^2) in the y-direction. This follows from the fact that the absolute value of the difference of intercepts of two parallel lines is the (distance apart) * sqrt(1+slope^2), where the distance apart is measured perpendicular to the lines.

Any other comments?

None.

@tguillemot
Copy link
Contributor

Thanks @jligo.
Can you add the figure before and after the correction on the discussion ?

margin = 1 / np.sqrt(np.sum(clf.coef_ ** 2))
yy_down = yy + a * margin
yy_up = yy - a * margin
yy_down = yy - np.sqrt(1+a**2) * margin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pep8 : Add space before/after + and **

@amueller
Copy link
Member

Wouldn't it be easier to just add or subtract coef_ from all points on the line? But yeah this is ok, too...

@ghost
Copy link
Author
ghost commented Dec 14, 2016

There are two figures generated (order: original, revised). PDF's are a bit higher quality.

Figure 1: original_1.pdf,revised_1.pdf
original_1
revised_1

Figure 2: original_2.pdf,revised_2.pdf
original_2
revised_2

@amueller
Copy link
Member

yeah that makes a lot more sense.

Btw, you can also directly embed PNGs into github comments which makes them much easier to see.

@ghost
Copy link
Author
ghost commented Dec 14, 2016

You just have to shift x and y so that |y shift - slope * x shift | = margin * sqrt(1+slope^2) = 1/w[1] where w is coef_.

So, we could replace np.sqrt(1 + a ** 2) * margin with 1/w[1], but I'm not sure which one is clearer for an example (since the margin doesn't come into play explicitly in the code at all, then). Or introduce a set of variables xx_down and xx_up, and shift both x and y (but the shifts won't just be by coef_).

@ghost
Copy link
Author
ghost commented Dec 14, 2016

Another alternative is the approach in this example (which is basically the same example, without colors), but you're relying on the support vectors to appear in some order (which I don't think is guaranteed for different data realizations).

@amueller
Copy link
Member

I think this is fine.

Copy link
Member
@jnothman jnothman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@agramfort
Copy link
Member

LGTM but travis complains

@tguillemot
Copy link
Contributor

Thx @jligo.
Travis is happy now.

@amueller @raghavrv @jnothman Can you merge please ?

@amueller amueller merged commit 726c8d9 into scikit-learn:master Dec 16, 2016
@ghost ghost deleted the svmexample branch December 16, 2016 20:58
sergeyf pushed a commit to sergeyf/scikit-learn that referenced this pull request Feb 28, 2017
* Corrected shift of separating hyperplane that passes through the support vectors

* Fixed signs on yy_down and yy_up

* PEP 8 spacing correction

* Fixed spaces at end of comment for Travis
@Przemo10 Przemo10 mentioned this pull request Mar 17, 2017
Sundrique pushed a commit to Sundrique/scikit-learn that referenced this pull request Jun 14, 2017
* Corrected shift of separating hyperplane that passes through the support vectors

* Fixed signs on yy_down and yy_up

* PEP 8 spacing correction

* Fixed spaces at end of comment for Travis
NelleV pushed a commit to NelleV/scikit-learn that referenced this pull request Aug 11, 2017
* Corrected shift of separating hyperplane that passes through the support vectors

* Fixed signs on yy_down and yy_up

* PEP 8 spacing correction

* Fixed spaces at end of comment for Travis
paulha pushed a commit to paulha/scikit-learn that referenced this pull request Aug 19, 2017
* Corrected shift of separating hyperplane that passes through the support vectors

* Fixed signs on yy_down and yy_up

* PEP 8 spacing correction

* Fixed spaces at end of comment for Travis
maskani-moh pushed a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017
* Corrected shift of separating hyperplane that passes through the support vectors

* Fixed signs on yy_down and yy_up

* PEP 8 spacing correction

* Fixed spaces at end of comment for Travis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0