create partial dependence plot (pdp) and individual conditional expectation (ice) plots -凯发k8网页登录
create partial dependence plot (pdp) and individual conditional expectation (ice) plots
syntax
description
plotpartialdependence(
computes and plots the partial dependence between the predictor variables listed
in regressionmdl
,vars
)vars
and the responses predicted by using the regression
model regressionmdl
, which contains predictor data.
if you specify one variable in
vars
, the function creates a line plot of the partial dependence against the variable.if you specify two variables in
vars
, the function creates a surface plot of the partial dependence against the two variables.
plotpartialdependence(
computes and plots the partial dependence between the predictor variables listed
in classificationmdl
,vars
,labels
)vars
and the scores for the classes specified in
labels
by using the classification model
classificationmdl
, which contains predictor data.
if you specify one variable in
vars
, the function creates a line plot of the partial dependence against the variable for each class inlabels
.if you specify two variables in
vars
, the function creates a surface plot of the partial dependence against the two variables. you must specify one class inlabels
.
plotpartialdependence(
computes and plots the partial dependence between the predictor variables listed
in fun
,vars
,data
)vars
and the outputs returned by the custom model
fun
, using the predictor data data
.
if you specify one variable in
vars
, the function creates a line plot of the partial dependence against the variable for each column of the output returned byfun
.if you specify two variables in
vars
, the function creates a surface plot of the partial dependence against the two variables. when you specify two variables,fun
must return a column vector or you must specify which output column to use by setting theoutputcolumns
name-value argument.
plotpartialdependence(___,
uses additional options specified by one or more name-value arguments. for
example, if you specify name,value
)"conditional","absolute"
, the
plotpartialdependence
function creates a figure
including a pdp, a scatter plot of the selected predictor variable and predicted
responses or scores, and an ice plot for each observation.
examples
create partial dependence plot
train a regression tree using the carsmall
data set, and create a pdp that shows the relationship between a feature and the predicted responses in the trained regression tree.
load the carsmall
data set.
load carsmall
specify weight
, cylinders
, and horsepower
as the predictor variables (x
), and mpg
as the response variable (y
).
x = [weight,cylinders,horsepower]; y = mpg;
train a regression tree using x
and y
.
mdl = fitrtree(x,y);
view a graphical display of the trained regression tree.
view(mdl,"mode","graph")
create a pdp of the first predictor variable, weight
.
plotpartialdependence(mdl,1)
the plotted line represents averaged partial relationships between weight
(labeled as x1
) and mpg
(labeled as y
) in the trained regression tree mdl
. the x
-axis minor ticks represent the unique values in x1
.
the regression tree viewer shows that the first decision is whether x1
is smaller than 3085.5. the pdp also shows a large change near x1
= 3085.5. the tree viewer visualizes each decision at each node based on predictor variables. you can find several nodes split based on the values of x1
, but determining the dependence of y
on x1
is not easy. however, the plotpartialdependence
plots average predicted responses against x1
, so you can clearly see the partial dependence of y
on x1
.
the labels x1
and y
are the default values of the predictor names and the response name. you can modify these names by specifying the name-value arguments predictornames
and responsename
when you train mdl
using fitrtree
. you can also modify axis labels by using the and functions.
create partial dependence plot for multiple classes
train a naive bayes classification model with the fisheriris
data set, and create a pdp that shows the relationship between the predictor variable and the predicted scores (posterior probabilities) for multiple classes.
load the fisheriris
data set, which contains species (species
) and measurements (meas
) on sepal length, sepal width, petal length, and petal width for 150 iris specimens. the data set contains 50 specimens from each of three species: setosa, versicolor, and virginica.
load fisheriris
train a naive bayes classification model with species
as the response and meas
as predictors.
mdl = fitcnb(meas,species);
create a pdp of the scores predicted by mdl
for all three classes of species
against the third predictor variable x3
. specify the class labels by using the classnames
property of mdl
.
plotpartialdependence(mdl,3,mdl.classnames);
according to this model, the probability of virginica
increases with x3
. the probability of setosa
is about 0.33, from where x3
is 0 to around 2.5, and then the probability drops to almost 0.
create individual conditional expectation plots
train a gaussian process regression model using generated sample data where a response variable includes interactions between predictor variables. then, create ice plots that show the relationship between a feature and the predicted responses for each observation.
generate sample predictor data x1
and x2
.
rng("default") % for reproducibility n = 200; x1 = rand(n,1)*2-1; x2 = rand(n,1)*2-1;
generate response values that include interactions between x1
and x2
.
y = x1-2*x1.*(x2>0) 0.1*rand(n,1);
create a gaussian process regression model using [x1 x2]
and y
.
mdl = fitrgp([x1 x2],y);
create a figure including a pdp (red line) for the first predictor x1
, a scatter plot (circle markers) of x1
and predicted responses, and a set of ice plots (gray lines) by specifying conditional
as "centered"
.
plotpartialdependence(mdl,1,"conditional","centered")
when conditional
is "centered"
, plotpartialdependence
offsets plots so that all plots start from zero, which is helpful in examining the cumulative effect of the selected feature.
a pdp finds averaged relationships, so it does not reveal hidden dependencies especially when responses include interactions between features. however, the ice plots clearly show two different dependencies of responses on x1
.
use new predictor data for partial dependence plot
train an ensemble of classification models and create two pdps, one using the training data set and the other using a new data set.
load the census1994
data set, which contains us yearly salary data, categorized as <=50k
or >50k
, and several demographic variables.
load census1994
extract a subset of variables to analyze from the tables adultdata
and adulttest
.
x = adultdata(:,["age","workclass","education_num","marital_status","race", ... "sex","capital_gain","capital_loss","hours_per_week","salary"]); xnew = adulttest(:,["age","workclass","education_num","marital_status","race", ... "sex","capital_gain","capital_loss","hours_per_week","salary"]);
train an ensemble of classifiers with salary
as the response and the remaining variables as predictors by using the function fitcensemble
. for binary classification, fitcensemble
aggregates 100 classification trees using the logitboost
method.
mdl = fitcensemble(x,"salary");
inspect the class names in mdl
.
mdl.classnames
ans = 2x1 categorical
<=50k
>50k
create a partial dependence plot of the scores predicted by mdl
for the second class of salary
(>50k
) against the predictor age
using the training data.
plotpartialdependence(mdl,"age",mdl.classnames(2))
create a pdp of the scores for class >50k
against age
using new predictor data from the table xnew
.
plotpartialdependence(mdl,"age",mdl.classnames(2),xnew)
the two plots show similar shapes for the partial dependence of the predicted score of high salary
(>50k
) on age
. both plots indicate that the predicted score of high salary rises fast until the age of 30, then stays almost flat until the age of 60, and then drops fast. however, the plot based on the new data produces slightly higher scores for ages over 65.
specify model using function handle
create a pdp to analyze relationships between predictors and anomaly scores for an isolationforest
object. you cannot pass an isolationforest
object directly to the plotpartialdependence
function. instead, define a custom function that returns anomaly scores for the object, and then pass the function to plotpartialdependence
.
load the 1994 census data stored in census1994.mat
. the data set consists of demographic data from the us census bureau.
load census1994
census1994
contains the two data sets adultdata
and adulttest
.
train an isolation forest model for adulttest
. the function iforest
returns an isolationforest
object.
rng("default") % for reproducibility mdl = iforest(adulttest);
define the custom function myanomalyscores
, which returns anomaly scores computed by the isanomaly
function of isolationforest
; the custom function definition appears at .
create a pdp of the anomaly scores against the variable age
for the adulttest
data set. plotpartialdependence
accepts a custom model in the form of a function handle. the function represented by the function handle must accept predictor data and return a column vector or matrix with one row for each observation. specify the custom model as @(tbl)myanomalyscores(mdl,tbl)
so that the custom function uses the trained model mdl
and accepts predictor data.
plotpartialdependence(@(tbl)myanomalyscores(mdl,tbl),"age",adulttest) xlabel("age") ylabel("anomaly score")
custom function myanomalyscores
function scores = myanomalyscores(mdl,tbl) [~,scores] = isanomaly(mdl,tbl); end
compare importance of predictor variables
train a regression ensemble using the carsmall
data set, and create a pdp plot and ice plots for each predictor variable using a new data set, carbig
. then, compare the figures to analyze the importance of predictor variables. also, compare the results with the estimates of predictor importance returned by the function.
load the carsmall
data set.
load carsmall
specify weight
, cylinders
, horsepower
, and model_year
as the predictor variables (x
), and mpg
as the response variable (y
).
x = [weight,cylinders,horsepower,model_year]; y = mpg;
train a regression ensemble using x
and y
.
mdl = fitrensemble(x,y, ... "predictornames",["weight","cylinders","horsepower","model year"], ... "responsename","mpg");
create the importance of predictor variables by using the plotpartialdependence
and functions. the plotpartialdependence
function visualizes the relationships between a selected predictor and predicted responses. predictorimportance
summarizes the importance of a predictor with a single value.
create a figure including a pdp plot (red line) and ice plots (gray lines) for each predictor by using plotpartialdependence
and specifying "conditional","absolute"
. each figure also includes a scatter plot (circle markers) of the selected predictor and predicted responses. also, load the carbig
data set and use it as new predictor data, xnew
. when you provide xnew
, the plotpartialdependence
function uses xnew
instead of the predictor data in mdl
.
load carbig xnew = [weight,cylinders,horsepower,model_year]; figure t = tiledlayout(2,2,"tilespacing","compact"); title(t,"individual conditional expectation plots") for i = 1 : 4 nexttile plotpartialdependence(mdl,i,xnew,"conditional","absolute") title("") end
compute estimates of predictor importance by using predictorimportance
. this function sums changes in the mean squared error (mse) due to splits on every predictor, and then divides the sum by the number of branch nodes.
imp = predictorimportance(mdl); figure bar(imp) title("predictor importance estimates") ylabel("estimates") xlabel("predictors") ax = gca; ax.xticklabel = mdl.predictornames;
the variable weight
has the most impact on mpg
according to predictor importance. the pdp of weight
also shows that mpg
has high partial dependence on weight
. the variable cylinders
has the least impact on mpg
according to predictor importance. the pdp of cylinders
also shows that mpg
does not change much depending on cylinders
.
compare partial dependence of generalized additive model
train a generalized additive model (gam) with both linear and interaction terms for predictors. then, create a pdp with both linear and interaction terms and a pdp with only linear terms. specify whether to include interaction terms when creating the pdps.
load the ionosphere
data set. this data set has 34 predictors and 351 binary responses for radar returns, either bad ('b'
) or good ('g'
).
load ionosphere
train a gam using the predictors x
and class labels y
. a recommended practice is to specify the class names. specify to include the 10 most important interaction terms.
mdl = fitcgam(x,y,"classnames",{'b','g'},"interactions",10);
mdl
is a classificationgam
model object.
list the interaction terms in mdl
.
mdl.interactions
ans = 10×2
1 5
7 8
6 7
5 6
5 7
5 8
3 5
4 7
1 7
4 5
each row of interactions
represents one interaction term and contains the column indexes of the predictor variables for the interaction term.
find the most frequent predictor in the interaction terms.
mode(mdl.interactions,"all")
ans = 5
the most frequent predictor in the interaction terms is the 5th predictor (x5
). create pdps for the 5th predictor. to exclude interaction terms from the computation, specify "includeinteractions",false
for the second pdp.
plotpartialdependence(mdl,5,mdl.classnames(1)) hold on plotpartialdependence(mdl,5,mdl.classnames(1),"includeinteractions",false) grid on legend("linear and interaction terms","linear terms only") title("pdps of posterior probabilities for 5th predictor") hold off
the plot shows that the partial dependence of the scores (posterior probabilities) on x5
varies depending on whether the model includes the interaction terms, especially where x5
is between 0.2 and 0.45.
extract partial dependence estimates from plots
train a support vector machine (svm) regression model using the carsmall
data set, and create a pdp for two predictor variables. then, extract partial dependence estimates from the output of plotpartialdependence
. alternatively, you can get the partial dependence values by using the partialdependence
function.
load the carsmall
data set.
load carsmall
specify weight
, cylinders
, displacement
, and horsepower
as the predictor variables (tbl
).
tbl = table(weight,cylinders,displacement,horsepower);
construct an svm regression model using tbl
and the response variable mpg
. use a gaussian kernel function with an automatic kernel scale.
mdl = fitrsvm(tbl,mpg,"responsename","mpg", ... "categoricalpredictors","cylinders","standardize",true, ... "kernelfunction","gaussian","kernelscale","auto");
create a pdp that visualizes partial dependence of predicted responses (mpg
) on the predictor variables weight
and cylinders
. specify query points to compute the partial dependence for weight
by using the querypoints
name-value argument. you cannot specify the querypoints
value for cylinders
because it is a categorical variable. plotpartialdependence
uses all categorical values.
pt = linspace(min(weight),max(weight),50)'; ax = plotpartialdependence(mdl,["weight","cylinders"],"querypoints",{pt,[]}); view(140,30) % modify the viewing angle
the pdp shows an interaction effect between weight
and cylinders
. the partial dependence of mpg
on weight
changes depending on the value of cylinders
.
extract the estimated partial dependence of mpg
on weight
and cylinders
. the xdata
, ydata
, and zdata
values of ax.children
are x-axis values (the first selected predictor values), y-axis values (the second selected predictor values), and z-axis values (the corresponding partial dependence values), respectively.
xval = ax.children.xdata; yval = ax.children.ydata; zval = ax.children.zdata;
alternatively, you can get the partial dependence values by using the partialdependence
function.
[pd,x,y] = partialdependence(mdl,["weight","cylinders"],"querypoints",{pt,[]});
pd
contains the partial dependence values for the query points x
and y
.
if you specify conditional
as "absolute"
, plotpartialdependence
creates a figure including a pdp, a scatter plot, and a set of ice plots. ax.children(1)
and ax.children(2)
correspond to the pdp and scatter plot, respectively. the remaining elements of ax.children
correspond to the ice plots. the xdata
and ydata
values of ax.children(i)
are x-axis values (the selected predictor values) and y-axis values (the corresponding partial dependence values), respectively.
input arguments
regressionmdl
— regression model
regression model object
regression model, specified as a full or compact regression model object, as given in the following tables of supported models.
model | full or compact model object |
---|---|
generalized linear model | , |
generalized linear mixed-effect model | |
linear regression | , |
linear mixed-effect model | |
nonlinear regression | |
ensemble of regression models | , regressionbaggedensemble ,
|
generalized additive model (gam) | , |
gaussian process regression | regressiongp , |
gaussian kernel regression model using random feature expansion | |
linear regression for high-dimensional data | regressionlinear |
neural network regression model | , |
support vector machine (svm) regression | regressionsvm , compactregressionsvm |
regression tree | regressiontree , compactregressiontree |
bootstrap aggregation for ensemble of decision trees | treebagger , |
if regressionmdl
is a model object that does not contain
predictor data (for example, a compact model), you must provide the input argument
data
.
plotpartialdependence
does not support a model object trained with a sparse
matrix. when you train a model, use a full numeric matrix or table for predictor data
where rows correspond to individual observations.
classificationmdl
— classification model
classification model object
classification model, specified as a full or compact classification model object, as given in the following table of supported models.
model | full or compact model object |
---|---|
discriminant analysis classifier | , |
multiclass model for support vector machines or other classifiers | classificationecoc , compactclassificationecoc |
ensemble of learners for classification | , , |
generalized additive model (gam) | , |
gaussian kernel classification model using random feature expansion | classificationkernel |
k-nearest neighbor classifier | classificationknn |
linear classification model | classificationlinear |
multiclass naive bayes model | , compactclassificationnaivebayes |
neural network classifier | , |
support vector machine (svm) classifier for one-class and binary classification | classificationsvm , compactclassificationsvm |
binary decision tree for multiclass classification | , compactclassificationtree |
bagged ensemble of decision trees | treebagger , |
multinomial regression model |
if classificationmdl
is a model object that does not contain predictor
data (for example, a compact model), you must provide the input argument
data
.
plotpartialdependence
does not support a model object trained with a sparse matrix. when you train a model, use a full numeric matrix or table for predictor data where rows correspond to individual observations.
fun
— custom model
function handle
custom model, specified as a function handle. the function handle fun
must represent a function that accepts the predictor data data
and
returns an output in the form of a column vector or matrix. each row of the output must
correspond to each observation (row) in the predictor data.
by default, plotpartialdependence
uses all output columns of
fun
for the partial dependence computation. you can specify
which output columns to use by setting the outputcolumns
name-value
argument.
if the predictor data (data
) is in a table,
plotpartialdependence
assumes that a variable is categorical if it is a
logical vector, categorical vector, character array, string array, or cell array of
character vectors. if the predictor data is a matrix, plotpartialdependence
assumes that all predictors are continuous. to identify any other predictors as
categorical predictors, specify them by using the
categoricalpredictors
name-value argument.
data types: function_handle
vars
— predictor variables
vector of positive integers | character vector | string scalar | string array | cell array of character vectors
predictor variables, specified as a vector of positive integers, character vector, string scalar, string array, or cell array of character vectors. you can specify one or two predictor variables, as shown in the following tables.
one predictor variable
value | description |
---|---|
positive integer | index value corresponding to the column of the predictor data. |
character vector or string scalar | name of the predictor variable. the name must match the entry in the
|
two predictor variables
value | description |
---|---|
vector of two positive integers | index values corresponding to the columns of the predictor data. |
string array or cell array of character vectors | names of the predictor variables. each element in the array is the name of a predictor
variable. the names must match the entries in the
|
if you specify two predictor variables, you must specify one class in
labels
for classificationmdl
or specify one output column in outputcolumns
for a
custom model fun
.
example: ["x1","x3"]
data types: single
| double
| char
| string
| cell
labels
— class labels
categorical array | character array | logical vector | numeric vector | cell array of character vectors
class labels, specified as a categorical or character array, logical or
numeric vector, or cell array of character vectors. the values and data
types in labels
must match those of the class names in
the classnames
property of
classificationmdl
(classificationmdl.classnames
).
you can specify multiple class labels only when you specify one variable in
vars
and specifyconditional
as"none"
(default).use
partialdependence
if you want to compute the partial dependence for two variables and multiple class labels in one function call.
this argument is valid only when you specify a classification model object
classificationmdl
.
example: ["red","blue"]
example: classificationmdl.classnames([1 3])
specifies
labels
as the first and third classes in
classificationmdl
.
data types: single
| double
| logical
| char
| cell
| categorical
data
— predictor data
numeric matrix | table
predictor data, specified as a numeric matrix or table. each row of
data
corresponds to one observation, and each column
corresponds to one variable.
for both a regression model (regressionmdl
) and a classification
model (classificationmdl
), data
must be
consistent with the predictor data that trained the model, stored in either the
x
or variables
property.
if you trained the model using a numeric matrix, then
data
must be a numeric matrix. the variables that make up the columns ofdata
must have the same number and order as the predictor variables that trained the model.if you trained the model using a table (for example,
tbl
), thendata
must be a table. all predictor variables indata
must have the same variable names and data types as the names and types intbl
. however, the column order ofdata
does not need to correspond to the column order oftbl
.data
must not be sparse.
if you specify a regression or classification model that does not contain predictor
data, you must provide data
. if the model is a full model object
that contains predictor data and you specify the data
argument,
then plotpartialdependence
ignores the predictor data in the model and uses
data
only.
if you specify a custom model fun
, you must provide
data
.
data types: single
| double
| table
name-value arguments
specify optional pairs of arguments as
name1=value1,...,namen=valuen
, where name
is
the argument name and value
is the corresponding value.
name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
before r2021a, use commas to separate each name and value, and enclose
name
in quotes.
example: plotpartialdependence(mdl,vars,data,"numobservationstosample",100,"useparallel",true)
creates a pdp by using 100 sampled observations in
and executing
data
for
-loop iterations in parallel.
conditional
— plot type
"none"
(default) | "absolute"
| "centered"
plot type, specified as "none"
,
"absolute"
, or "centered"
.
value | description |
---|---|
"none" |
|
"absolute" |
to use the
|
"centered" |
to use the
|
example: "conditional","absolute"
includeinteractions
— flag to include interaction terms
true
| false
flag to include interaction terms of the generalized additive model (gam) in the partial
dependence computation, specified as true
or
false
. this argument is valid only for a gam. that is, you can
specify this argument only when regressionmdl
is
or , or classificationmdl
is or .
the default includeinteractions
value is true
if the
model contains interaction terms. the value must be false
if the
model does not contain interaction terms.
example: "includeinteractions",false
data types: logical
includeintercept
— flag to include intercept term
true
(default) | false
flag to include an intercept term of the generalized additive model (gam) in the partial
dependence computation, specified as true
or
false
. this argument is valid only for a gam. that is, you can
specify this argument only when regressionmdl
is
or , or classificationmdl
is or .
example: "includeintercept",false
data types: logical
numobservationstosample
— number of observations to sample
number of total observations (default) | positive integer
number of observations to sample, specified as a positive integer. the default value is the
number of total observations in data
or the model
(regressionmdl
or classificationmdl
). if you
specify a value larger than the number of total observations, then
plotpartialdependence
uses all observations.
plotpartialdependence
samples observations without replacement by using the
function and uses the sampled observations to compute partial
dependence.
plotpartialdependence
displays minor tick marks
at the unique values of the sampled observations.
if you specify conditional
as either
"absolute"
or "centered"
,
plotpartialdependence
creates a figure including an ice
plot for each sampled observation.
example: "numobservationstosample",100
data types: single
| double
parent
— axes in which to plot
gca
(default) | axes object
axes in which to plot, specified as an axes object. if you do not
specify the axes and if the current axes are cartesian, then
plotpartialdependence
uses the current axes
(gca
). if axes do not exist,
plotpartialdependence
plots in a new
figure.
example: "parent",ax
querypoints
— points to compute partial dependence
numeric column vector | numeric two-column matrix | cell array of two numeric column vectors
points to compute partial dependence for numeric predictors, specified as a numeric column vector, a numeric two-column matrix, or a cell array of two numeric column vectors.
if you select one predictor variable in
vars
, use a numeric column vector.if you select two predictor variables in
vars
:use a numeric two-column matrix to specify the same number of points for each predictor variable.
use a cell array of two numeric column vectors to specify a different number of points for each predictor variable.
the default value is a numeric column vector or a numeric two-column matrix, depending on the number of selected predictor variables. each column contains 100 evenly spaced points between the minimum and maximum values of the sampled observations for the corresponding predictor variable.
if conditional
is "absolute"
or "centered"
, then the software adds the predictor
data values (data
or predictor data in
regressionmdl
or
classificationmdl
) of the selected predictors
to the query points.
you cannot modify querypoints
for a categorical
variable. the plotpartialdependence
function uses all
categorical values in the selected variable.
if you select one numeric variable and one categorical variable, you
can specify querypoints
for a numeric variable by
using a cell array consisting of a numeric column vector and an empty
array.
example: "querypoints",{pt,[]}
data types: single
| double
| cell
useparallel
— flag to run in parallel
false
(default) | true
flag to run in parallel, specified as true
or
false
. if you specify "useparallel",true
, the
plotpartialdependence
function executes for
-loop
iterations by using when predicting responses or
scores for each observation and averaging them. the loop runs in parallel when you have
parallel computing toolbox™.
example: "useparallel",true
data types: logical
categoricalpredictors
— categorical predictors list for custom model
vector of positive integers | logical vector | character matrix | string array | cell array of character vectors | "all"
categorical predictors list for the custom model fun
, specified as one of the values in this table.
value | description |
---|---|
vector of positive integers | each entry in the vector is an index value indicating that the corresponding predictor is categorical. the index values are between 1 and |
logical vector | a |
character matrix | each row of the matrix is the name of a predictor variable. the names must match the variable names of the predictor data data in a table. pad the names with extra blanks so each row of the character matrix has the same length. |
string array or cell array of character vectors | each element in the array is the name of a predictor variable. the names must match the variable names of the predictor data data in a table. |
"all" | all predictors are categorical. |
by default, if the predictor data data
is in a table, plotpartialdependence
assumes that a variable is categorical if it is a logical vector, categorical vector, character array, string array, or cell array of character vectors. if the predictor data is a matrix, plotpartialdependence
assumes that all predictors are continuous. to identify any other predictors as categorical predictors, specify them by using the categoricalpredictors
name-value argument.
this argument is valid only when you specify a custom model by using fun
.
example: "categoricalpredictors","all"
data types: single
| double
| logical
| char
| string
| cell
outputcolumns
— output columns of custom model
"all"
(default) | vector of positive integers | logical vector
output columns of the custom model fun
to use for the partial dependence computation, specified as one of the values in this table.
value | description |
---|---|
vector of positive integers | each entry in the vector is an index value indicating that |
logical vector | a |
"all" | plotpartialdependence uses all output columns for the partial dependence computation. |
you can specify multiple output columns only when you specify one variable in
vars
and specifyconditional
as"none"
(default).use
partialdependence
if you want to compute the partial dependence for two variables and multiple output columns in one function call.
this argument is valid only when you specify a custom model by using
fun
.
example: "outputcolumns",[1 2]
data types: single
| double
| logical
| char
| string
output arguments
ax
— axes of the plot
axes object
axes of the plot, returned as an axes object. for details on how to modify the appearance of the axes and extract data from plots, see and extract partial dependence estimates from plots.
more about
partial dependence for regression models
partial dependence represents the relationships between
predictor variables and predicted responses in a trained regression model.
plotpartialdependence
computes the partial dependence of predicted responses
on a subset of predictor variables by marginalizing over the other variables.
consider partial dependence on a subset xs of the whole predictor variable set x = {x1, x2, …, xm}. a subset xs includes either one variable or two variables: xs = {xs1} or xs = {xs1, xs2}. let xc be the complementary set of xs in x. a predicted response f(x) depends on all variables in x:
f(x) = f(xs, xc).
the partial dependence of predicted responses on xs is defined by the expectation of predicted responses with respect to xc:
where
pc(xc)
is the marginal probability of xc, that is, . assuming that each observation is equally likely, and the dependence
between xs and
xc and the interactions of
xs and
xc in responses is not strong,
plotpartialdependence
estimates the partial dependence by using observed
predictor data as follows:
(1) |
where n is the number of observations and xi = (xis, xic) is the ith observation.
when you call the plotpartialdependence
function, you can specify a trained
model (f(·)) and select variables
(xs) by using the input arguments
regressionmdl
and vars
, respectively.
plotpartialdependence
computes the partial dependence at 100 evenly spaced
points of xs or the points that you specify by
using the querypoints
name-value argument. you can specify the number
(n) of observations to sample from given predictor data by using the
numobservationstosample
name-value argument.
individual conditional expectation for regression models
an individual conditional expectation (ice) , as an extension of partial dependence, represents the relationship between a predictor variable and the predicted responses for each observation. while partial dependence shows the averaged relationship between predictor variables and predicted responses, a set of ice plots disaggregates the averaged information and shows an individual dependence for each observation.
plotpartialdependence
creates an ice plot
for each observation. a set of ice plots is useful to investigate heterogeneities of
partial dependence originating from different observations.
plotpartialdependence
can also create ice plots with any
predictor data provided through the input argument data
. you
can use this feature to explore predicted response space.
consider an ice plot for a selected predictor variable xs with a given observation xic, where xs = {xs}, xc is the complementary set of xs in the whole variable set x, and xi = (xis, xic) is the ith observation. the ice plot corresponds to the summand of the summation in :
plotpartialdependence
plots for each observation i when you specify
conditional
as "absolute"
. if you
specify conditional
as "centered"
,
plotpartialdependence
draws all plots after removing level
effects due to different observations:
this subtraction ensures that each plot starts from zero, so that you can examine the cumulative effect of xs and the interactions between xs and xc.
partial dependence and ice for classification models
in the case of classification models,
plotpartialdependence
computes the partial dependence and
individual conditional expectation in the same way as for regression models, with
one exception: instead of using the predicted responses from the model, the function
uses the predicted scores for the classes specified in
labels
.
weighted traversal algorithm
the weighted traversal algorithm is a method to estimate partial dependence for a tree-based model. the estimated partial dependence is the weighted average of response or score values corresponding to the leaf nodes visited during the tree traversal.
let xs be a subset of the whole variable set x and xc be the complementary set of xs in x. for each xs value to compute partial dependence, the algorithm traverses a tree from the root (beginning) node down to leaf (terminal) nodes and finds the weights of leaf nodes. the traversal starts by assigning a weight value of one at the root node. if a node splits by xs, the algorithm traverses to the appropriate child node depending on the xs value. the weight of the child node becomes the same value as its parent node. if a node splits by xc, the algorithm traverses to both child nodes. the weight of each child node becomes a value of its parent node multiplied by the fraction of observations corresponding to each child node. after completing the tree traversal, the algorithm computes the weighted average by using the assigned weights.
for an ensemble of bagged trees, the estimated partial dependence is an average of the weighted averages over the individual trees.
algorithms
for both a regression model (regressionmdl
) and a classification
model (classificationmdl
),
plotpartialdependence
uses a predict
function to predict responses or scores. plotpartialdependence
chooses the proper predict
function according to the model and runs
predict
with its default settings. for details about each
predict
function, see the predict
functions in
the following two tables. if the specified model is a tree-based model (not including a
boosted ensemble of trees) and conditional
is
"none"
, then plotpartialdependence
uses the
weighted traversal algorithm instead of the predict
function. for
details, see .
regression model object
model type | full or compact regression model object | function to predict responses |
---|---|---|
bootstrap aggregation for ensemble of decision trees | ||
bootstrap aggregation for ensemble of decision trees | treebagger | |
ensemble of regression models | , regressionbaggedensemble , | |
gaussian kernel regression model using random feature expansion | ||
gaussian process regression | regressiongp , | |
generalized additive model | , | |
generalized linear mixed-effect model | ||
generalized linear model | , | |
linear mixed-effect model | ||
linear regression | , | |
linear regression for high-dimensional data | regressionlinear | |
neural network regression model | , | |
nonlinear regression | ||
regression tree | regressiontree , compactregressiontree | |
support vector machine | regressionsvm , compactregressionsvm |
classification model object
model type | full or compact classification model object | function to predict labels and scores |
---|---|---|
discriminant analysis classifier | , | |
multiclass model for support vector machines or other classifiers | classificationecoc , compactclassificationecoc | predict |
ensemble of learners for classification | , , | |
gaussian kernel classification model using random feature expansion | classificationkernel | |
generalized additive model | , | |
k-nearest neighbor model | classificationknn | |
linear classification model | classificationlinear | |
naive bayes model | , compactclassificationnaivebayes | |
neural network classifier | , | |
support vector machine for one-class and binary classification | classificationsvm , compactclassificationsvm | predict |
binary decision tree for multiclass classification | , compactclassificationtree | |
bagged ensemble of decision trees | treebagger , |
alternative functionality
partialdependence
computes partial dependence without visualization. the function can compute partial dependence for two variables and multiple classes in one function call.
references
[1] friedman, jerome. h. “greedy function approximation: a gradient boosting machine.” the annals of statistics 29, no. 5 (2001): 1189-1232.
[2] goldstein, alex, adam kapelner, justin bleich, and emil pitkin. “peeking inside the black box: visualizing statistical learning with plots of individual conditional expectation.” journal of computational and graphical statistics 24, no. 1 (january 2, 2015): 44–65.
[3] hastie, trevor, robert tibshirani, and jerome friedman. the elements of statistical learning. new york, ny: springer new york, 2001.
extended capabilities
automatic parallel support
accelerate code by automatically running computation in parallel using parallel computing toolbox™.
to run in parallel, set the useparallel
name-value argument to
true
in the call to this function.
for more general information about parallel computing, see (parallel computing toolbox).
gpu arrays
accelerate code by running on a graphics processing unit (gpu) using parallel computing toolbox™.
usage notes and limitations:
this function fully supports gpu arrays for the following regression models:
and objects
and objects
regressionsvm
andcompactregressionsvm
objects
this function supports gpu arrays with limitations for the regression and classification models described in this table.
full or compact model object limitations classificationecoc
orcompactclassificationecoc
surrogate splits are not supported for decision tree learners.
for knn learners, you cannot set the following options to the values shown:
"nsmethod","kdtree"
"distance"
,function handle"includeties",true
or surrogate splits are not supported for decision tree learners.
classificationknn
you cannot set the following options to the values shown:
"nsmethod","kdtree"
"distance"
,function handle"includeties",true
classificationsvm
orcompactclassificationsvm
one-class classification is not supported
data
cannot contain infinite valuesname-value arguments have the following limitations:
you cannot specify the
kernelfunction
name-value argument as a custom kernel function.you can specify the
solver
name-value argument as "smo
" only.you cannot specify the
outlierfraction
orshrinkageperiod
name-value argument.
or compactclassificationtree
surrogate splits are not supported for decision trees.
or surrogate splits are not supported for decision tree learners.
regressiontree
orcompactregressiontree
surrogate splits are not supported for decision trees.
this function fully supports gpu arrays for a custom function if the custom function supports gpu arrays.
for more information, see run matlab functions on a gpu (parallel computing toolbox).
version history
introduced in r2017br2023a: gpu array support for regressionsvm
and compactregressionsvm
models
starting in r2023a, plotpartialdependence
fully supports gpu arrays for
regressionsvm
and
compactregressionsvm
models.
see also
partialdependence
| lime
| shapley
| oobpermutedpredictorimportance
| | | |
打开示例
您曾对此示例进行过修改。是否要打开带有您的编辑的示例?
matlab 命令
您点击的链接对应于以下 matlab 命令:
请在 matlab 命令行窗口中直接输入以执行命令。web 浏览器不支持 matlab 命令。
select a web site
choose a web site to get translated content where available and see local events and offers. based on your location, we recommend that you select: .
you can also select a web site from the following list:
how to get best site performance
select the china site (in chinese or english) for best site performance. other mathworks country sites are not optimized for visits from your location.
americas
- (español)
- (english)
- (english)
europe
- (english)
- (english)
- (deutsch)
- (español)
- (english)
- (français)
- (english)
- (italiano)
- (english)
- (english)
- (english)
- (deutsch)
- (english)
- (english)
- switzerland
- (english)
asia pacific
- (english)
- (english)
- (english)
- 中国
- (日本語)
- (한국어)