DecisionBoundaryDisplay#
- class sklearn.inspection.DecisionBoundaryDisplay(*, xx0, xx1, response, multiclass_colors=None, xlabel=None, ylabel=None)[source]#
Decisions boundary visualization.
It is recommended to use
from_estimator
to create aDecisionBoundaryDisplay
. 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
.- 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 problem. Ignored for binary problems and multiclass problems when plotting a single prediction value per point. Possible inputs are:
list: list of Matplotlib color strings, of length
n_classes
str: name of
matplotlib.colors.Colormap
None: ‘viridis’ colormap is used to sample colors
Single color colormaps will be generated from the colors in the list or colors taken from the colormap and passed to the
cmap
parameter of theplot_method
.Added in version 1.7.
- xlabelstr, default=None
Default label to place on x axis.
- ylabelstr, default=None
Default label to place on y axis.
- Attributes:
- surface_matplotlib
QuadContourSet
orQuadMesh
or list of such objects If
plot_method
is ‘contour’ or ‘contourf’,surface_
isQuadContourSet
. Ifplot_method
is ‘pcolormesh’,surface_
isQuadMesh
.- multiclass_colors_array of shape (n_classes, 4)
Colors used to plot each class in multiclass problems. Only defined when
color_of_interest
is None.Added in version 1.7.
- ax_matplotlib Axes
Axes with decision boundary.
- figure_matplotlib Figure
Figure containing the decision boundary.
- surface_matplotlib
See also
DecisionBoundaryDisplay.from_estimator
Plot decision boundary given an estimator.
Examples
>>> 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, response=y_pred ... ) >>> display.plot() <...> >>> display.ax_.scatter( ... iris.data[:, 0], iris.data[:, 1], c=iris.target, edgecolor="black" ... ) <...> >>> plt.show()
- 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 when
response_method
is ‘predict_proba’ or ‘decision_function’. If None,estimator.classes_[1]
is considered the positive class for binary classifiers. For multiclass classifiers, if None, all classes will be represented in the decision boundary plot; the class with the highest response value at each point is plotted. The color of each class can be set viamulticlass_colors
.Added in version 1.4.
- multiclass_colorslist of str, or str, default=None
Specifies how to color each class when plotting multiclass ‘predict_proba’ or ‘decision_function’ and
class_of_interest
is None. Ignored in all other cases.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 colormaps will be generated from the colors in the list or colors taken from the colormap, and passed to the
cmap
parameter of theplot_method
.Added in version 1.7.
- xlabelstr, default=None
The label used for the x-axis. If
None
, an attempt is made to extract a label fromX
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 fromX
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:
- display
DecisionBoundaryDisplay
Object that stores the result.
- display
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.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, ... ) >>> disp.ax_.scatter(X[:, 0], X[:, 1], c=iris.target, edgecolor="k") <...> >>> plt.show()
- 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
.
- Returns:
- display:
DecisionBoundaryDisplay
Object that stores computed values.
- display:
Gallery examples#

Linear and Quadratic Discriminant Analysis with covariance ellipsoid

Plot the decision boundaries of a VotingClassifier

Decision Boundaries of Multinomial and One-vs-Rest Logistic Regression

One-Class SVM versus One-Class SVM using Stochastic Gradient Descent

Class Likelihood Ratios to measure classification performance

Comparing Nearest Neighbors with and without Neighborhood Components Analysis

Plot different SVM classifiers in the iris dataset

Plot classification boundaries with different SVM Kernels

Plot the decision surface of decision trees trained on the iris dataset