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/ensemble/plot_forest_iris.py.Add
from sklearn.inspection import DecisionBoundaryDisplayto the imports.Capture the current axes object at the start of each subplot iteration so
from_estimatorcan be passedax=ax:in place of the existing
plt.subplot(3, 4, plot_idx)call (the function already returns anAxes- just assign it).Replace the
if isinstance(model, DecisionTreeClassifier): Z = ...; cs = plt.contourf(xx, yy, Z, cmap=cmap)branch with:The
meshgrid/x_min/x_max/y_min/y_maxcomputation a few lines above the branch becomes dead for the DecisionTree case but is still consumed by theelse(alpha-blend) branch and by the coarse-gridxx_coarser/yy_coarserscatter, so keep it.Leave the
elsebranch (alpha-blend overmodel.estimators_) completely unchanged - both thefor tree in model.estimators_:loop and its per-treeplt.contourfcalls. Add a single short comment above theelsebranch noting that this branch intentionally does not useDecisionBoundaryDisplaybecause the pedagogical point is the per-tree blend.Leave the coarser-grid
plt.scatteroverlay, the training-point scatter, theplt.titleandplt.suptitle, andplt.tight_layout()/plt.show()untouched.After step 3, convert the surrounding
plt.scatter(...)andplt.title(...)calls inside the loop iteration to theax.*form so the axes object created in step 2 receives them. The contourf via DecisionBoundaryDisplay is already routed toax; the scatter and title need the explicitax.*to match.PR title:
DOC Use DecisionBoundaryDisplay for single-tree case in plot_forest_iris examplePR body (concise, factual):
Why this matters
Issue #33980 lists
examples/ensemble/plot_forest_iris.pyas one of the four remaining examples that should be refactored to useDecisionBoundaryDisplay. The current file is 176 lines and contains TWO decision-region drawing paths inside the inner loop:Single DecisionTree path (lines ~126-129): standard
meshgrid+model.predict(np.c_[xx.ravel(), yy.ravel()])+plt.contourf. This is the direct analog of thefrom_estimatorhelper and is straightforward to refactor.Ensemble alpha-blend path (lines ~130-139): iterates over
model.estimators_and callstree.predict(...)for each individual tree in the forest/ExtraTrees/AdaBoost ensemble, drawing onecontourfper tree withalpha=1/n_estimators. This visualizes how individual trees vote and is a deliberate pedagogical choice of the example.There is also a
xx_coarser, yy_coarsermeshgrid +model.predict+plt.scatteroverlay below the contourf, showing predictions on a coarse grid.The clean refactor is to convert path (1) to
DecisionBoundaryDisplay.from_estimatorand leave path (2) and (3) untouched. Path (2) cannot be expressed byfrom_estimatorwithout losing the per-tree alpha-blend semantics; trying to do so would break the example's purpose.Testing
python examples/ensemble/plot_forest_iris.pyruns to completion without exceptions and produces the 3x4 grid of subplots with all four classifiers (DecisionTree, RandomForest, ExtraTrees, AdaBoost) showing decision regions, coarse-grid sample predictions, and training points.mainvs the branch.cmap=plt.cm.RdYlBu.mainsince they use the unchanged alpha-blend path.sphinx-build doc -b html doc/_buildcompletes without new warnings for the ensemble gallery page.grep -n "DecisionBoundaryDisplay" examples/ensemble/plot_forest_iris.pyreturns one import match and one call-site match (inside theisinstance(model, DecisionTreeClassifier)branch).grep -n "for tree in model.estimators_" examples/ensemble/plot_forest_iris.pystill returns the existing per-tree alpha-blend loop (proves the ensemble path was preserved).ruff check examples/ensemble/plot_forest_iris.pyreports no new findings.Fixes #33980
AI was used for assistance.