DecisionBoundaryDisplay#

class sklearn.inspection.DecisionBoundaryDisplay(*, xx0, xx1, n_classes, response, multiclass_colors=None, xlabel=None, ylabel=None)[source]#

Decisions boundary visualization.

It is recommended to use from_estimator to create a DecisionBoundaryDisplay. All parameters are stored as attributes.

Read more in the User Guide.

For a detailed example comparing the decision boundaries of multinomial and one-vs-rest logistic regression, please see Decision Boundaries of Multinomial and One-vs-Rest Logistic Regression.

Added in version 1.1.

Parameters:
xx0ndarray of shape (grid_resolution, grid_resolution)

First output of meshgrid.

xx1ndarray of shape (grid_resolution, grid_resolution)

Second output of meshgrid.

n_classesint

Expected number of unique classes or labels if response was generated by a classifier or a clusterer.

For outlier detectors, n_classes should be set to 2 by definition (inlier or outlier).

For regressors, n_classes should also be set to 2 by convention (continuous responses are displayed the same way as unthresholded binary responses).

Added in version 1.9.

responsendarray of shape (grid_resolution, grid_resolution) or (grid_resolution, grid_resolution, n_classes)

Values of the response function.

multiclass_colorslist of str or str, default=None

Specifies how to color each class when plotting all classes of multiclass problems.

Possible inputs are:

  • list: list of Matplotlib color strings, of length n_classes

  • str: name of matplotlib.colors.Colormap

  • None: ‘tab10’ colormap is used to sample colors if the number of classes is less than or equal to 10, otherwise ‘gist_rainbow’ colormap.

Single color (fading to white) colormaps will be generated from the colors in the list or colors taken from the colormap, and passed to the cmap parameter of the plot_method.

For binary problems, this is ignored and cmap or colors can be passed as kwargs instead, otherwise, the default colormap (‘viridis’) is used.

Added in version 1.7.

Changed in version 1.9: multiclass_colors is now also used when response_method="predict"

xlabelstr, default=None

Default label to place on x axis.

ylabelstr, default=None

Default label to place on y axis.

Attributes:
surface_matplotlib QuadContourSet or QuadMesh or list of such objects

If plot_method is ‘contour’ or ‘contourf’, surface_ is QuadContourSet. If plot_method is ‘pcolormesh’, surface_ is QuadMesh.

multiclass_colors_array of shape (n_classes, 4)

Colors used to plot each class in multiclass problems. Only defined when n_classes > 2.

Added in version 1.7.

ax_matplotlib Axes

Axes with decision boundary.

figure_matplotlib Figure

Figure containing the decision boundary.

See also

DecisionBoundaryDisplay.from_estimator

Plot decision boundary given an estimator.

Examples

>>> import matplotlib as mpl
>>> import matplotlib.pyplot as plt
>>> import numpy as np
>>> from sklearn.datasets import load_iris
>>> from sklearn.inspection import DecisionBoundaryDisplay
>>> from sklearn.tree import DecisionTreeClassifier
>>> iris = load_iris()
>>> feature_1, feature_2 = np.meshgrid(
...     np.linspace(iris.data[:, 0].min(), iris.data[:, 0].max()),
...     np.linspace(iris.data[:, 1].min(), iris.data[:, 1].max())
... )
>>> grid = np.vstack([feature_1.ravel(), feature_2.ravel()]).T
>>> tree = DecisionTreeClassifier().fit(iris.data[:, :2], iris.target)
>>> y_pred = np.reshape(tree.predict(grid), feature_1.shape)
>>> display = DecisionBoundaryDisplay(
...     xx0=feature_1, xx1=feature_2, n_classes=len(tree.classes_), response=y_pred
... )
>>> display.plot()
<...>
>>> display.ax_.scatter(
...     iris.data[:, 0],
...     iris.data[:, 1],
...     c=iris.target,
...     cmap=mpl.colors.ListedColormap(display.multiclass_colors_),
...     edgecolor="black"
... )
<...>
>>> plt.show()
../../_images/sklearn-inspection-DecisionBoundaryDisplay-1.png
classmethod from_estimator(estimator, X, *, grid_resolution=100, eps=1.0, plot_method='contourf', response_method='auto', class_of_interest=None, multiclass_colors=None, xlabel=None, ylabel=None, ax=None, **kwargs)[source]#

Plot decision boundary given an estimator.

Read more in the User Guide.

Parameters:
estimatorobject

Trained estimator used to plot the decision boundary.

X{array-like, sparse matrix, dataframe} of shape (n_samples, 2)

Input data that should be only 2-dimensional.

grid_resolutionint, default=100

Number of grid points to use for plotting decision boundary. Higher values will make the plot look nicer but be slower to render.

epsfloat, default=1.0

Extends the minimum and maximum values of X for evaluating the response function.

plot_method{‘contourf’, ‘contour’, ‘pcolormesh’}, default=’contourf’

Plotting method to call when plotting the response. Please refer to the following matplotlib documentation for details: contourf, contour, pcolormesh.

response_method{‘auto’, ‘decision_function’, ‘predict_proba’, ‘predict’}, default=’auto’

Specifies whether to use decision_function, predict_proba or predict as the target response. If set to ‘auto’, the response method is tried in the order as listed above.

Changed in version 1.6: For multiclass problems, ‘auto’ no longer defaults to ‘predict’.

class_of_interestint, float, bool or str, default=None

The class to be plotted. For binary classifiers, if None, estimator.classes_[1] is considered the positive class. For multiclass classifiers, if None, all classes will be represented in the decision boundary plot; when response_method is predict_proba or decision_function, the class with the highest response value at each point is plotted. The color of each class can be set via multiclass_colors.

Added in version 1.4.

multiclass_colorslist of str, or str, default=None

Specifies how to color each class when plotting multiclass problems and class_of_interest is None.

Possible inputs are:

  • list: list of Matplotlib color strings, of length n_classes

  • str: name of matplotlib.colors.Colormap

  • None: ‘tab10’ colormap is used to sample colors if the number of

    classes is less than or equal to 10, otherwise ‘gist_rainbow’ colormap.

Single color (fading to white) colormaps will be generated from the colors in the list or colors taken from the colormap, and passed to the cmap parameter of the plot_method.

For binary problems, this is ignored and cmap or colors can be passed as kwargs instead, otherwise, the default colormap (‘viridis’) is used.

Added in version 1.7.

Changed in version 1.9: multiclass_colors is now also used when response_method="predict"

xlabelstr, default=None

The label used for the x-axis. If None, an attempt is made to extract a label from X if it is a dataframe, otherwise an empty string is used.

ylabelstr, default=None

The label used for the y-axis. If None, an attempt is made to extract a label from X if it is a dataframe, otherwise an empty string is used.

axMatplotlib axes, default=None

Axes object to plot on. If None, a new figure and axes is created.

**kwargsdict

Additional keyword arguments to be passed to the plot_method.

Returns:
displayDecisionBoundaryDisplay

Object that stores the result.

See also

DecisionBoundaryDisplay

Decision boundary visualization.

sklearn.metrics.ConfusionMatrixDisplay.from_estimator

Plot the confusion matrix given an estimator, the data, and the label.

sklearn.metrics.ConfusionMatrixDisplay.from_predictions

Plot the confusion matrix given the true and predicted labels.

Examples

>>> import matplotlib as mpl
>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import load_iris
>>> from sklearn.linear_model import LogisticRegression
>>> from sklearn.inspection import DecisionBoundaryDisplay
>>> iris = load_iris()
>>> X = iris.data[:, :2]
>>> classifier = LogisticRegression().fit(X, iris.target)
>>> disp = DecisionBoundaryDisplay.from_estimator(
...     classifier, X, response_method="predict",
...     xlabel=iris.feature_names[0], ylabel=iris.feature_names[1],
...     alpha=0.5,
... )
>>> cmap = mpl.colors.ListedColormap(disp.multiclass_colors_)
>>> disp.ax_.scatter(X[:, 0], X[:, 1], c=iris.target, edgecolor="k", cmap=cmap)
<...>
>>> plt.show()
../../_images/sklearn-inspection-DecisionBoundaryDisplay-2.png
plot(plot_method='contourf', ax=None, xlabel=None, ylabel=None, **kwargs)[source]#

Plot visualization.

Parameters:
plot_method{‘contourf’, ‘contour’, ‘pcolormesh’}, default=’contourf’

Plotting method to call when plotting the response. Please refer to the following matplotlib documentation for details: contourf, contour, pcolormesh.

axMatplotlib axes, default=None

Axes object to plot on. If None, a new figure and axes is created.

xlabelstr, default=None

Overwrite the x-axis label.

ylabelstr, default=None

Overwrite the y-axis label.

**kwargsdict

Additional keyword arguments to be passed to the plot_method. For binary problems, cmap or colors can be set here to specify the colormap or colors, otherwise the default colormap (‘viridis’) is used.

Returns:
display: DecisionBoundaryDisplay

Object that stores computed values.