Skip to content

DOC Use DecisionBoundaryDisplay in plot_gpc_iris example - #34059

Closed
mvanhorn wants to merge 1 commit into
scikit-learn:mainfrom
mvanhorn:fix/33980-sklearn-decision-boundary-gpc-iris
Closed

mvanhorn wants to merge 1 commit into
scikit-learn:mainfrom
mvanhorn:fix/33980-sklearn-decision-boundary-gpc-iris

Conversation

@mvanhorn

Copy link
Copy Markdown

Summary

Single-file edit to examples/gaussian_process/plot_gpc_iris.py.

  1. Add from sklearn.inspection import DecisionBoundaryDisplay to the imports.

  2. Drop the explicit h, xx, yy, and np.meshgrid variables. DecisionBoundaryDisplay.from_estimator will pick a sensible grid from X.

  3. Replace the inner-loop body that calls clf.predict_proba, reshapes to RGB, and calls plt.imshow with:

    ax = plt.subplot(1, 2, i + 1)
    DecisionBoundaryDisplay.from_estimator(
        clf,
        X,
        response_method="predict_proba",
        plot_method="contourf",
        cmap="viridis",
        alpha=0.7,
        ax=ax,
        xlabel="Sepal length",
        ylabel="Sepal width",
    )

    response_method="predict_proba" on a 3-class GPC returns a max-probability surface that from_estimator reduces to a single 2D response, drawn via contourf. This is the same pattern DOC Use DecisionBoundaryDisplay in Plot RBF Parameters example #33952 used for plot_rbf_parameters.py.

  4. Keep the training-point scatter, axes limits, ticks, and title exactly as today (convert plt.scatter/plt.xlim/plt.ylim/plt.xticks(())/plt.yticks(())/plt.title(...) to ax.scatter/ax.set_xlim etc.). The training-point colors (np.array(["r","g","b"])[y]) and the LML title format string stay verbatim.

  5. Leave plt.figure(figsize=(10, 5)), plt.tight_layout(), and plt.show() untouched.

  6. Update the example's module docstring to note that the colored region now visualizes the maximum class probability over the input plane (so the docstring stays accurate after the response_method change). One-sentence edit, no narrative restructuring.

PR title: DOC Use DecisionBoundaryDisplay in plot_gpc_iris example

PR body (concise, factual):

Towards #33980.

Refactor examples/gaussian_process/plot_gpc_iris.py to draw the GPC decision region with DecisionBoundaryDisplay.from_estimator(..., response_method="predict_proba", plot_method="contourf") instead of the manual meshgrid + predict_proba + RGB imshow block.

Note for reviewers: the previous rendering visualized the raw posterior vector as an RGB image (each pixel's color encoded [p_setosa, p_versicolor, p_virginica]). DecisionBoundaryDisplay does not natively reproduce that exact rendering. This PR converts the example to the standard single-response contourf style used by the other examples in the gallery (and by the already-refactored plot_rbf_parameters per #33952), at the cost of losing the per-pixel RGB encoding. Happy to revert that aspect if maintainers prefer to keep the RGB visualization for this example.

Docstring updated in one line to describe the new visualization.

Why this matters

Issue #33980 lists examples/gaussian_process/plot_gpc_iris.py as one of the four remaining examples that should be refactored to use DecisionBoundaryDisplay. The current implementation:

  • builds a meshgrid of step h=0.02 over the iris sepal-feature range,
  • calls clf.predict_proba(np.c_[xx.ravel(), yy.ravel()]) to get a (n_points, 3) probability array,
  • reshapes that to (n_rows, n_cols, 3) and renders it as a direct RGB image via plt.imshow(Z, extent=..., origin="lower"). Each pixel's color is the literal [p_0, p_1, p_2] posterior for that point.

This RGB-probability rendering is unusual: most sklearn examples plot a single response (argmax label, or class-1 probability) via contourf. The companion examples that #33980 wants to refactor (plot_svm_margin, plot_kmeans_digits, plot_forest_iris) all use the single-response style.

This plan refactors plot_gpc_iris.py to the single-response DecisionBoundaryDisplay form for consistency with the other examples in the gallery, and notes the trade-off in the PR body so reviewers can keep or revert.

Testing

  • python examples/gaussian_process/plot_gpc_iris.py runs to completion without exceptions and produces two subplots (Isotropic RBF, Anisotropic RBF) with the colored decision region behind the training points and the LML title preserved.
  • Visual diff: render main vs the branch. The colored background style changes (RGB-imshow -> single-response contourf) - this is expected and called out in the PR body. The scatter points, titles, and overall layout must remain visually equivalent.
  • sphinx-build doc -b html doc/_build completes without new warnings for the gaussian_process gallery page.
  • grep -n "predict_proba\|imshow" examples/gaussian_process/plot_gpc_iris.py after the edit returns no matches inside the loop body.
  • grep -n "DecisionBoundaryDisplay" examples/gaussian_process/plot_gpc_iris.py returns one import match and one call-site match.
  • ruff check examples/gaussian_process/plot_gpc_iris.py reports no new findings.

Fixes #33980

AI was used for assistance.

@jeremiedbb

Copy link
Copy Markdown
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.

@jeremiedbb jeremiedbb closed this May 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DOC refactor examples to use DecisionBoundaryDisplay

2 participants