|
13 | 13 | # Authors: The scikit-learn developers |
14 | 14 | # SPDX-License-Identifier: BSD-3-Clause |
15 | 15 |
|
| 16 | +import matplotlib as mpl |
16 | 17 | import matplotlib.pyplot as plt |
17 | 18 | import numpy as np |
18 | 19 |
|
19 | 20 | from sklearn import datasets |
20 | 21 | from sklearn.gaussian_process import GaussianProcessClassifier |
21 | 22 | from sklearn.gaussian_process.kernels import RBF |
| 23 | +from sklearn.inspection import DecisionBoundaryDisplay |
22 | 24 |
|
23 | 25 | # import some data to play with |
24 | 26 | iris = datasets.load_iris() |
25 | 27 | 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) |
29 | 29 |
|
30 | 30 | kernel = 1.0 * RBF([1.0]) |
31 | 31 | gpc_rbf_isotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y) |
32 | 32 | kernel = 1.0 * RBF([1.0, 1.0]) |
33 | 33 | gpc_rbf_anisotropic = GaussianProcessClassifier(kernel=kernel).fit(X, y) |
34 | 34 |
|
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 | | - |
40 | 35 | titles = ["Isotropic RBF", "Anisotropic RBF"] |
41 | 36 | plt.figure(figsize=(10, 5)) |
| 37 | + |
42 | 38 | 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]. |
45 | 39 | plt.subplot(1, 2, i + 1) |
46 | 40 |
|
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 | + ) |
48 | 46 |
|
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 | + ) |
52 | 55 |
|
53 | | - # Plot also the training points |
54 | | - plt.scatter(X[:, 0], X[:, 1], c=np.array(["r", "g", "b"])[y], edgecolors=(0, 0, 0)) |
55 | 56 | plt.xlabel("Sepal length") |
56 | 57 | plt.ylabel("Sepal width") |
57 | | - plt.xlim(xx.min(), xx.max()) |
58 | | - plt.ylim(yy.min(), yy.max()) |
59 | 58 | plt.xticks(()) |
60 | 59 | plt.yticks(()) |
61 | 60 | plt.title( |
|
0 commit comments