Repository navigation
Conversation
Member
|
The linked issue is not open for contribution, please read the full description #33980 (comment). In addition, please take a look at https://scikit-learn.org/stable/developers/contributing.html#automated-contributions-policy. With that in mind I'm closing this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Single-file edit to
examples/svm/plot_svm_margin.py. The refactor replaces the manual mesh+decision_function+contourfblock (lines ~70-81 of the current file) with a call toDecisionBoundaryDisplay.from_estimator.Add
from sklearn.inspection import DecisionBoundaryDisplayto the imports.Inside the per-
(name, penalty)loop, restructure to obtain anAxesobject soDecisionBoundaryDisplaycan draw onto the same figure as the margin lines and scatter:in place of
plt.figure(fignum, figsize=(4, 3)); plt.clf().Replace the explicit
YY, XX = np.meshgrid(yy, xx); xy = np.vstack(...).T; Z = clf.decision_function(xy).reshape(...); plt.contourf(XX, YY, Z, cmap=plt.get_cmap("RdBu"), alpha=0.5, ...)block with:Note:
from_estimatorchooses a sensible mesh extent fromX. The current example hard-codesx_min=-4.8, x_max=4.2, y_min=-6, y_max=6. Preserve the visual range by callingax.set_xlim(x_min, x_max)/ax.set_ylim(y_min, y_max)after the display, matching what the file already does.Convert remaining
plt.*calls in this loop iteration to theax.*form (ax.plot,ax.scatter,ax.set_xticks([]),ax.set_yticks([])) so the margin lines, support vectors, and data points still render on top of the decision region. The twoplt.plot(xx, yy_down, "k--")andplt.plot(xx, yy_up, "k--")calls becomeax.plot(xx, yy_down, "k--")andax.plot(xx, yy_up, "k--")respectively; the twoplt.scattercalls becomeax.scatter.Leave
plt.show()at the end of the file untouched.PR title:
DOC Use DecisionBoundaryDisplay in plot_svm_margin examplePR body (concise, factual):
Why this matters
Issue #33980 (DOC refactor examples to use
DecisionBoundaryDisplay) is a follow-up to #33557 and tracks five examples that still draw decision boundaries with manual contour math instead of the publicsklearn.inspection.DecisionBoundaryDisplayhelper. One subtask is already merged (#33952 ->plot_rbf_parameters.py). The remaining four are explicitly listed as unchecked checkbox items:plot_svm_margin.py,plot_kmeans_digits.py,plot_gpc_iris.py,plot_forest_iris.py. This plan coversplot_svm_margin.py.examples/svm/plot_svm_margin.pycurrently:xx = np.linspace(-5, 5),yy,yy_down,yy_upfrom the linear classifier'scoef_andintercept_to draw the separating hyperplane and margin lines,(YY, XX)and callsclf.decision_function(xy)to produceZ,plt.contourf(XX, YY, Z, ...)to fill the decision regions.The manual
decision_functionmesh andcontourfcall is exactly the patternDecisionBoundaryDisplay.from_estimator(..., response_method="decision_function", plot_method="contourf")exists to replace. The margin lines themselves (yy_down,yy_up) are a special feature of this example, not generic boundary plotting, so they stay - the refactor is targeted at the colored region behind them.Testing
python examples/svm/plot_svm_margin.pyruns to completion without exceptions and shows two figures (unreg and reg) with the same hyperplane, margin lines, support-vector circles, and colored decision background as the currentmainoutput.mainand the branch side-by-side. The filledRdBubackground must look indistinguishable (the underlying decision function value is identical; only the path that draws it changed).sphinx-build doc -b html doc/_build(or the project'smake htmlrecipe under the same environment) completes without new warnings or sphinx-gallery rendering errors for the SVM examples gallery page.grep -n "meshgrid\|decision_function" examples/svm/plot_svm_margin.pyafter the edit returns no matches inside the loop (the manual computation is gone).grep -n "DecisionBoundaryDisplay" examples/svm/plot_svm_margin.pyreturns one match for the import and one for the call site.ruff check examples/svm/plot_svm_margin.py(the repo's configured linter viapyproject.toml) - no new findings.Fixes #33980
AI was used for assistance.