Skip to content

Commit 5a349c8

Browse files
DOC Use DecisionBoundaryDisplay in plot_gpc_iris (scikit-learn#34509)
Co-authored-by: Stefanie Senger <91849487+StefanieSenger@users.noreply.github.com>
1 parent 75d1994 commit 5a349c8

1 file changed

Lines changed: 17 additions & 18 deletions

File tree

examples/gaussian_process/plot_gpc_iris.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,48 @@
1313
# Authors: The scikit-learn developers
1414
# SPDX-License-Identifier: BSD-3-Clause
1515

16+
import matplotlib as mpl
1617
import matplotlib.pyplot as plt
1718
import numpy as np
1819

1920
from sklearn import datasets
2021
from sklearn.gaussian_process import GaussianProcessClassifier
2122
from sklearn.gaussian_process.kernels import RBF
23+
from sklearn.inspection import DecisionBoundaryDisplay
2224

2325
# import some data to play with
2426
iris = datasets.load_iris()
2527
X = iris.data[:, :2] # we only take the first two features.
26-
y = np.array(iris.target, dtype=int)
27-
28-
h = 0.02 # step size in the mesh
28+
y = np.array(iris.target)
2929

3030
kernel = 1.0 * RBF([1.0])
3131
gpc_rbf_isotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y)
3232
kernel = 1.0 * RBF([1.0, 1.0])
3333
gpc_rbf_anisotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y)
3434

35-
# create a mesh to plot in
36-
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
37-
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
38-
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
39-
4035
titles = ["Isotropic RBF", "Anisotropic RBF"]
4136
plt.figure(figsize=(10, 5))
37+
4238
for i, clf in enumerate((gpc_rbf_isotropic, gpc_rbf_anisotropic)):
43-
# Plot the predicted probabilities. For that, we will assign a color to
44-
# each point in the mesh [x_min, m_max]x[y_min, y_max].
4539
plt.subplot(1, 2, i + 1)
4640

47-
Z = clf.predict_proba(np.c_[xx.ravel(), yy.ravel()])
41+
# Visualize the decision boundary as class regions shaded in the predicted
42+
# probability of the winning class
43+
boundary_display = DecisionBoundaryDisplay.from_estimator(
44+
clf, X, ax=plt.gca(), response_method="predict_proba", alpha=0.5
45+
)
4846

49-
# Put the result into a color plot
50-
Z = Z.reshape((xx.shape[0], xx.shape[1], 3))
51-
plt.imshow(Z, extent=(x_min, x_max, y_min, y_max), origin="lower")
47+
# Plot also the training points reusing the boundary display color scheme
48+
plt.scatter(
49+
X[:, 0],
50+
X[:, 1],
51+
c=y,
52+
cmap=mpl.colors.ListedColormap(boundary_display.target_colors_),
53+
edgecolors="k",
54+
)
5255

53-
# Plot also the training points
54-
plt.scatter(X[:, 0], X[:, 1], c=np.array(["r", "g", "b"])[y], edgecolors=(0, 0, 0))
5556
plt.xlabel("Sepal length")
5657
plt.ylabel("Sepal width")
57-
plt.xlim(xx.min(), xx.max())
58-
plt.ylim(yy.min(), yy.max())
5958
plt.xticks(())
6059
plt.yticks(())
6160
plt.title(

0 commit comments

Comments
 (0)