-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
[MRG] Modify svm/plot_separating_hyperplane.py example for matplotlib v2 #8369
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,37 +12,39 @@ | |
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from sklearn import svm | ||
|
||
from sklearn.datasets import make_classification | ||
# we create 40 separable points | ||
np.random.seed(0) | ||
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]] | ||
Y = [0] * 20 + [1] * 20 | ||
|
||
X, Y = make_classification(n_features=2, n_redundant=0, n_informative=1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use a |
||
n_clusters_per_class=1) | ||
# fit the model | ||
clf = svm.SVC(kernel='linear') | ||
clf.fit(X, Y) | ||
|
||
# get the separating hyperplane | ||
w = clf.coef_[0] | ||
a = -w[0] / w[1] | ||
xx = np.linspace(-5, 5) | ||
yy = a * xx - (clf.intercept_[0]) / w[1] | ||
|
||
# plot the parallels to the separating hyperplane that pass through the | ||
# support vectors | ||
b = clf.support_vectors_[0] | ||
yy_down = a * xx + (b[1] - a * b[0]) | ||
b = clf.support_vectors_[-1] | ||
yy_up = a * xx + (b[1] - a * b[0]) | ||
|
||
# plot the line, the points, and the nearest vectors to the plane | ||
plt.plot(xx, yy, 'k-') | ||
plt.plot(xx, yy_down, 'k--') | ||
plt.plot(xx, yy_up, 'k--') | ||
|
||
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1], | ||
s=80, facecolors='none') | ||
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired) | ||
plt.scatter(X[:, 0], X[:, 1], c=Y, s=50, cmap='autumn', edgecolors='k') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not change something unless there is a very good reason to. In this case, I would keep the |
||
|
||
ax = plt.gca() | ||
xlim = ax.get_xlim() | ||
ylim = ax.get_ylim() | ||
|
||
# create grid to evaluate model | ||
x = np.linspace(xlim[0], xlim[1], 30) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should really have a function for this but that's another issue #6338 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not merge yet. |
||
y = np.linspace(ylim[0], ylim[1], 30) | ||
Y, X = np.meshgrid(y, x) | ||
xy = np.vstack([X.ravel(), Y.ravel()]).T | ||
P = clf.decision_function(xy).reshape(X.shape) | ||
|
||
# plot decision boundary and margins | ||
ax.contour(X, Y, P, colors='k', | ||
levels=[-1, 0, 1], alpha=0.5, | ||
linestyles=['--', '-', '--']) | ||
|
||
ax.scatter(clf.support_vectors_[:, 0], | ||
clf.support_vectors_[:, 1], edgecolors='k', | ||
s=200, linewidth=1, facecolors='none') | ||
ax.set_xlim(xlim) | ||
ax.set_ylim(ylim) | ||
|
||
plt.axis('tight') | ||
plt.show() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should probably not change the data unless you have a very good reason to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lesteve under the Issue #8364 , @amueller point out it good to use
make_classification
for data generation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah OK I missed that, thanks.