PartialDependenceDisplay
- class sklearn.inspection.PartialDependenceDisplay(pd_results, *, features, feature_names, target_idx, deciles, kind='average', subsample=1000, random_state=None, is_categorical
Partial Dependence Plot (PDP) and Individual Conditional Expectation (ICE).
It is recommended to use
from_estimator to create a
PartialDependenceDisplay. All parameters are stored
as attributes.
For general information regarding scikit-learn visualization tools, see
the Visualization Guide.
For guidance on interpreting these plots, refer to the
Inspection Guide.
For an example on how to use this class, see the following example: Advanced Plotting With Partial Dependence.
Results of partial_dependence for
features.
Indices of features for a given plot. A tuple of one integer will plot a partial dependence curve of one feature. A tuple of two integers will plot a two-way partial dependence curve as a contour plot.
Feature names corresponding to the indices in features.
>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from sklearn.inspection import PartialDependenceDisplay
>>> from
Partial Dependence and Individual Conditional Expectation Plots
In a multiclass setting, specifies the class for which the PDPs should be computed. Note that for binary classification, the positive class (index 1) is always used.
In a multioutput setting, specifies the task for which the PDPs should be computed.
Ignored in binary classification or classical regression settings.
Deciles for feature indices in features.
Whether to plot the partial dependence averaged across all the samples in the dataset or one line per sample or both.
kind='average' results in the traditional PD plot;
kind='individual' results in the ICE plot;
kind='both' results in plotting both the ICE and PD on the same
plot.
A list of such strings can be provided to specify kind on a per-plot
basis. The length of the list should be the same as the number of
interaction requested in features.
Sampling for ICE curves when kind is ‘individual’ or ‘both’.
If float, should be between 0.0 and 1.0 and represent the proportion
of the dataset to be used to plot ICE curves. If int, represents the
maximum absolute number of samples to use.
Note that the full dataset is still used to calculate partial
dependence when kind='both'.
Controls the randomness of the selected samples when subsamples is not
None. See Glossary for details.
Whether each target feature in features is categorical or not.
The list should be same size as features. If None, all features
are assumed to be continuous.
If ax is an axes or None, the bounding_ax_ is the axes where the
grid of partial dependence plots are drawn. If ax is a list of axes
or a numpy array of axes, bounding_ax_ is None.
If ax is an axes or None, axes_[i, j] is the axes on the i-th row
and j-th column. If ax is a list of axes, axes_[i] is the i-th item
in ax. Elements that are None correspond to a nonexisting axes in
that position.
If ax is an axes or None, lines_[i, j] is the partial dependence
curve on the i-th row and j-th column. If ax is a list of axes,
lines_[i] is the partial dependence curve corresponding to the i-th
item in ax. Elements that are None correspond to a nonexisting axes
or an axes that does not include a line plot.
If ax is an axes or None, vlines_[i, j] is the line collection
representing the x axis deciles of the i-th row and j-th column. If
ax is a list of axes, vlines_[i] corresponds to the i-th item in
ax. Elements that are None correspond to a nonexisting axes or an
axes that does not include a PDP plot.
If ax is an axes or None, vlines_[i, j] is the line collection
representing the y axis deciles of the i-th row and j-th column. If
ax is a list of axes, vlines_[i] corresponds to the i-th item in
ax. Elements that are None correspond to a nonexisting axes or an
axes that does not include a 2-way plot.
If ax is an axes or None, contours_[i, j] is the partial dependence
plot on the i-th row and j-th column. If ax is a list of axes,
contours_[i] is the partial dependence plot corresponding to the i-th
item in ax. Elements that are None correspond to a nonexisting axes
or an axes that does not include a contour plot.
If ax is an axes or None, bars_[i, j] is the partial dependence bar
plot on the i-th row and j-th column (for a categorical feature).
If ax is a list of axes, bars_[i] is the partial dependence bar
plot corresponding to the i-th item in ax. Elements that are None
correspond to a nonexisting axes or an axes that does not include a
bar plot.
If ax is an axes or None, heatmaps_[i, j] is the partial dependence
heatmap on the i-th row and j-th column (for a pair of categorical
features) . If ax is a list of axes, heatmaps_[i] is the partial
dependence heatmap corresponding to the i-th item in ax. Elements
that are None correspond to a nonexisting axes or an axes that does not
include a heatmap.
Figure containing partial dependence plots.
Partial dependence (PD) and individual conditional expectation (ICE) plots.
Partial dependence plots, individual conditional expectation plots, or an
overlay of both can be plotted by setting the kind parameter.
This method generates one plot for each entry in features. The plots
are arranged in a grid with n_cols columns. For one-way partial
dependence plots, the deciles of the feature values are shown on the
x-axis. For two-way plots, the deciles are shown on both axes and PDPs
are contour plots.
For general information regarding scikit-learn visualization tools, see
the Visualization Guide.
For guidance on interpreting these plots, refer to the
Inspection Guide.
For an example on how to use this class method, see Partial Dependence and Individual Conditional Expectation Plots.
A fitted estimator object implementing predict, predict_proba, or decision_function. Multioutput-multiclass classifiers are not supported.
X is used to generate a grid of values for the target
features (where the partial dependence will be evaluated), and
also to generate values for the complement features when the
method is .
>>> import matplotlib.pyplot as plt
>>> from sklearn.datasets import make_friedman1
>>> from sklearn.ensemble import GradientBoostingRegressor
>>> from sklearn.inspection import PartialDependenceDisplay
>>> X, y = make_friedman1
Plot partial dependence plots.
and a grid of partial dependence plots will be drawn within
these bounds. The n_cols parameter controls the number of
columns in the grid.
plots will be drawn directly into these axes.
None, a figure and a bounding axes is created and treatedas the single axes case.
The maximum number of columns in the grid plot. Only active when
ax is a single axes or None.
Dict with keywords passed to the matplotlib.pyplot.plot call.
For one-way partial dependence plots.
Dictionary with keywords passed to the matplotlib.pyplot.plot call.
For ICE lines in the one-way partial dependence plots.
The key value pairs defined in ice_lines_kw takes priority over
line_kw.
Dictionary with keywords passed to the matplotlib.pyplot.plot call.
For partial dependence in one-way partial dependence plots.
The key value pairs defined in pd_line_kw takes priority over
line_kw.
Dict with keywords passed to the matplotlib.pyplot.contourf
call for two-way partial dependence plots.
Dict with keywords passed to the matplotlib.pyplot.bar
call for one-way categorical partial dependence plots.
Dict with keywords passed to the matplotlib.pyplot.imshow
call for two-way categorical partial dependence plots.
Global min and max average predictions, such that all plots will have the
same scale and y limits. pdp_lim[1] is the global min and max for single
partial dependence curves. pdp_lim[2] is the global min and max for
two-way partial dependence curves. If None (default), the limit will be
inferred from the global minimum and maximum of all predictions.
If True, the ICE and PD lines will start at the origin of the
y-axis. By default, no centering is done.
PartialDependenceDisplayReturns a PartialDependenceDisplay
object that contains the partial dependence plots.
init'recursion''brute''brute'The target features for which to create the PDPs.
If features[i] is an integer or a string, a one-way PDP is created;
if features[i] is a tuple, a two-way PDP is created (only supported
with kind='average'). Each tuple must be of size 2.
If any entry is a string, then it must be in feature_names.
Sample weights are used to calculate weighted means when averaging the
model output. If None, then samples are equally weighted. If
sample_weight is not None, then method will be set to 'brute'.
Note that sample_weight is ignored for kind='individual'.
Indicates the categorical features.
None: no feature will be considered categorical;
boolean array-like: boolean mask of shape (n_features,)
indicating which features are categorical. Thus, this array has
the same shape has X.shape[1];
integer or string array-like: integer indices or strings indicating categorical features.
Name of each feature; feature_names[i] holds the name of the feature
with index i.
By default, the name of the feature corresponds to their numerical
index for NumPy array and their column name for pandas dataframe.
In a multiclass setting, specifies the class for which the PDPs should be computed. Note that for binary classification, the positive class (index 1) is always used.
In a multioutput setting, specifies the task for which the PDPs should be computed.
Ignored in binary classification or classical regression settings.
Specifies whether to use predict_proba or
decision_function as the target response. For regressors
this parameter is ignored and the response is always the output of
predict. By default, predict_proba is tried first
and we revert to decision_function if it doesn’t exist. If
method is 'recursion', the response is always the output of
decision_function.
The maximum number of columns in the grid plot. Only active when ax
is a single axis or None.
The number of equally spaced points on the axes of the plots, for each
target feature.
This parameter is overridden by custom_values if that parameter is set.
The lower and upper percentile used to create the extreme values
for the PDP axes. Must be in [0, 1].
This parameter is overridden by custom_values if that parameter is set.
A dictionary mapping the index of an element of features to an
array of values where the partial dependence should be calculated
for that feature. Setting a range of values for a feature overrides
grid_resolution and percentiles.
The method used to calculate the averaged predictions:
'recursion' is only supported for some tree-based estimators
(namely
GradientBoostingClassifier,
GradientBoostingRegressor,
HistGradientBoostingClassifier,
HistGradientBoostingRegressor,
DecisionTreeRegressor,
RandomForestRegressor
but is more efficient in terms of speed.
With this method, the target response of a
classifier is always the decision function, not the predicted
probabilities. Since the 'recursion' method implicitly computes
the average of the ICEs by design, it is not compatible with ICE and
thus kind must be 'average'.
'brute' is supported for any estimator, but is more
computationally intensive.
'auto': the 'recursion' is used for estimators that support it,
and 'brute' is used otherwise. If sample_weight is not None,
then 'brute' is used regardless of the estimator.
Please see this note for
differences between the 'brute' and 'recursion' method.
The number of CPUs to use to compute the partial dependences.
Computation is parallelized over features specified by the features
parameter.
None means 1 unless in a joblib.parallel_backend context.
-1 means using all processors. See Glossary
for more details.
Verbose output during PD computations.
Dict with keywords passed to the matplotlib.pyplot.plot call.
For one-way partial dependence plots. It can be used to define common
properties for both ice_lines_kw and pdp_line_kw.
Dictionary with keywords passed to the matplotlib.pyplot.plot call.
For ICE lines in the one-way partial dependence plots.
The key value pairs defined in ice_lines_kw takes priority over
line_kw.
Dictionary with keywords passed to the matplotlib.pyplot.plot call.
For partial dependence in one-way partial dependence plots.
The key value pairs defined in pd_line_kw takes priority over
line_kw.
Dict with keywords passed to the matplotlib.pyplot.contourf call.
For two-way partial dependence plots.
If a single axis is passed in, it is treated as a bounding axes
and a grid of partial dependence plots will be drawn within
these bounds. The n_cols parameter controls the number of
columns in the grid.
If an array-like of axes are passed in, the partial dependence plots will be drawn directly into these axes.
If None, a figure and a bounding axes is created and treated
as the single axes case.
Whether to plot the partial dependence averaged across all the samples in the dataset or one line per sample or both.
kind='average' results in the traditional PD plot;
kind='individual' results in the ICE plot.
Note that the fast method='recursion' option is only available for
kind='average' and sample_weights=None. Computing individual
dependencies and doing weighted averages requires using the slower
method='brute'.
If True, the ICE and PD lines will start at the origin of the
y-axis. By default, no centering is done.
Sampling for ICE curves when kind is ‘individual’ or ‘both’.
If float, should be between 0.0 and 1.0 and represent the proportion
of the dataset to be used to plot ICE curves. If int, represents the
absolute number samples to use.
Note that the full dataset is still used to calculate averaged partial
dependence when kind='both'.
Controls the randomness of the selected samples when subsamples is not
None and kind is either 'both' or 'individual'.
See Glossary for details.
PartialDependenceDisplay