-
-
Notifications
You must be signed in to change notification settings - Fork 25.9k
DOC Merge plot_svm_margin.py and plot_separating_hyperplane.py into plot_svm_hyperplane_margin.py #31045
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
base: main
Are you sure you want to change the base?
DOC Merge plot_svm_margin.py and plot_separating_hyperplane.py into plot_svm_hyperplane_margin.py #31045
Changes from all commits
30bf31a
b5018ef
4211cca
cbcffba
3116f1f
8c1fa62
4216bec
499fc1b
7aa4078
b50fefa
902a787
d29225d
9fb3c50
2de5c47
49ee55e
2ef1d5f
53bd994
2257e4f
a730252
bd74817
8b1eb56
762644d
8612d2d
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 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -404,13 +404,14 @@ Tips on Practical Use | |||||||||||||
|
||||||||||||||
* **Setting C**: ``C`` is ``1`` by default and it's a reasonable default | ||||||||||||||
choice. If you have a lot of noisy observations you should decrease it: | ||||||||||||||
decreasing C corresponds to more regularization. | ||||||||||||||
decreasing C corresponds to more regularization (see example below). | ||||||||||||||
|
||||||||||||||
:class:`LinearSVC` and :class:`LinearSVR` are less sensitive to ``C`` when | ||||||||||||||
it becomes large, and prediction results stop improving after a certain | ||||||||||||||
threshold. Meanwhile, larger ``C`` values will take more time to train, | ||||||||||||||
sometimes up to 10 times longer, as shown in [#3]_. | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
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.
Suggested change
Let's also leave this unchanged. |
||||||||||||||
* Support Vector Machine algorithms are not scale invariant, so **it | ||||||||||||||
is highly recommended to scale your data**. For example, scale each | ||||||||||||||
attribute on the input vector X to [0,1] or [-1,+1], or standardize it | ||||||||||||||
|
@@ -468,6 +469,9 @@ Tips on Practical Use | |||||||||||||
The ``C`` value that yields a "null" model (all weights equal to zero) can | ||||||||||||||
be calculated using :func:`l1_min_c`. | ||||||||||||||
|
||||||||||||||
.. rubric:: Examples | ||||||||||||||
|
||||||||||||||
* :ref:`sphx_glr_auto_examples_svm_plot_svm_hyperplane_margin.py` | ||||||||||||||
Comment on lines
+472
to
+474
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. It would be great to add the reference to the example in the text instead, including a description of what the example is about. |
||||||||||||||
|
||||||||||||||
.. _svm_kernels: | ||||||||||||||
|
||||||||||||||
|
@@ -632,7 +636,11 @@ indicates a perfect prediction. But problems are usually not always perfectly | |||||||||||||
separable with a hyperplane, so we allow some samples to be at a distance :math:`\zeta_i` from | ||||||||||||||
their correct margin boundary. The penalty term `C` controls the strength of | ||||||||||||||
this penalty, and as a result, acts as an inverse regularization parameter | ||||||||||||||
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.
Suggested change
|
||||||||||||||
(see note below). | ||||||||||||||
(see the figure below). Also please refer to the note below. | ||||||||||||||
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.
Suggested change
I would suggest to simply finish the last sentence with a colon and then show the plot directly. |
||||||||||||||
|
||||||||||||||
.. figure:: ../auto_examples/svm/images/sphx_glr_plot_svm_hyperplane_margin_001.png | ||||||||||||||
:align: center | ||||||||||||||
:scale: 75 | ||||||||||||||
Comment on lines
+641
to
+643
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.
Suggested change
If we add a target line, the plot serves as a link to the example. I think it should be like this. |
||||||||||||||
|
||||||||||||||
The dual problem to the primal is | ||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
""" | ||
========================================================================= | ||
SVM: Effect of Regularization (C) on Maximum Margin Separating Hyperplane | ||
========================================================================= | ||
|
||
This script demonstrates the concept of maximum margin separating hyperplane | ||
in a two-class separable dataset using a Support Vector Machine (SVM) | ||
with a linear kernel and how different values of `C` influence margin width. | ||
Comment on lines
+6
to
+8
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. I would suggest to add a short note on what a margin is to make this more beginner-friendly. |
||
|
||
- **Small C (e.g., 0.05)**: | ||
- Allows some misclassifications, resulting in wider margin. | ||
- **Moderate C (e.g., 1)**: | ||
- Balances classification accuracy and margin width. | ||
- **Large C (e.g., 1000)**: | ||
- Prioritizes classifying all points correctly, leading to narrower margin. | ||
|
||
""" | ||
|
||
# Authors: The scikit-learn developers | ||
# SPDX-License-Identifier: BSD-3-Clause | ||
|
||
# %% | ||
import matplotlib.pyplot as plt | ||
|
||
from sklearn import svm | ||
from sklearn.datasets import make_blobs | ||
from sklearn.inspection import DecisionBoundaryDisplay | ||
|
||
# %% | ||
# Create 40 separable points | ||
X, y = make_blobs(n_samples=40, centers=2, cluster_std=1.5, random_state=6) | ||
|
||
# %% | ||
# Define different values of C to observe its effect on the margin | ||
C_values = [0.05, 1, 1000] | ||
|
||
# %% | ||
# Visualize | ||
plt.figure(figsize=(12, 4)) | ||
for i, C_val in enumerate(C_values, 1): | ||
clf = svm.SVC(kernel="linear", C=C_val) | ||
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. I think we should add a random state here, so this example looks the same with every run. |
||
clf.fit(X, y) | ||
y_pred = clf.predict(X) | ||
misclassified = y_pred != y | ||
|
||
plt.subplot(1, 3, i) | ||
plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired, 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. Please add axis labels. Something simple like "Feature 1" and "Feature 2" would be enough. 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. This is resolved. (Just the "resolved" button doesn't show here.) |
||
# misclassified samples | ||
plt.scatter( | ||
X[misclassified, 0], | ||
X[misclassified, 1], | ||
facecolors="none", | ||
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. Using the same color (edgecolors="k") for correctly classified and for misclassified points, doesn't make them stand out as expected. Could you fix this please? |
||
s=80, | ||
linewidths=1.5, | ||
label="Misclassified", | ||
) | ||
|
||
# plot the decision function | ||
ax = plt.gca() | ||
DecisionBoundaryDisplay.from_estimator( | ||
clf, | ||
X, | ||
plot_method="contour", | ||
colors="k", | ||
levels=[-1, 0, 1], | ||
alpha=0.5, | ||
linestyles=["--", "-", "--"], | ||
ax=ax, | ||
) | ||
|
||
# plot support vectors | ||
ax.scatter( | ||
clf.support_vectors_[:, 0], | ||
clf.support_vectors_[:, 1], | ||
s=120, | ||
linewidth=1.5, | ||
facecolors="none", | ||
edgecolors="r", | ||
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. What do you think about having a different color for miss-classified samples? 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. This is resolved. (Just the "resolved" button doesn't show here.) |
||
label="Support Vectors", | ||
) | ||
|
||
plt.title(f"SVM Decision Boundary (C={C_val})") | ||
plt.xlabel("Feature 1") | ||
plt.ylabel("Feature 2") | ||
plt.legend() | ||
|
||
plt.tight_layout() | ||
plt.show() | ||
|
||
# %% [markdown] | ||
# - **Small `C` (e.g., 0.01, 0.05)**: | ||
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. Would be nice to have the representation of |
||
# - Use when: | ||
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. The |
||
# - You expect noisy or overlapping data. | ||
# - You can tolerate some misclassification in training. | ||
# - Your priority is better generalization on unseen data. | ||
# - Note: | ||
# - May underfit if the margin is too lenient. | ||
# - **Moderate `C` (e.g., 1)**: | ||
# - Use when: | ||
# - You're unsure about noise levels. | ||
# - You want good balance between margin width and classification accuracy. | ||
# - **Large `C` (e.g., 1000)**: | ||
# - Use when: | ||
# - The data is clean and linearly separable. | ||
# - You want to avoid any training misclassification. | ||
# - Note: | ||
# - May overfit noisy data by trying to classify all samples correctly. | ||
Comment on lines
+107
to
+108
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 rendered as part of the |
This file was deleted.
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.
I think it's better to leave this unchanged. We add links in suitable spots, but these quasi-links just occupy the reader's attention without adding real value.