MathWorks Machine Translation
The automated translation of this page is provided by a general purpose third party translator tool.
MathWorks does not warrant, and disclaims all liability for, the accuracy, suitability, or fitness for purpose of the translation.
Fit ensemble of learners for classification
Mdl = fitcensemble(Tbl,ResponseVarName)
Mdl = fitcensemble(Tbl,formula)
Mdl = fitcensemble(Tbl,Y)
Mdl = fitcensemble(X,Y)
Mdl = fitcensemble(___,Name,Value)
returns
the trained classification ensemble model object (Mdl
= fitcensemble(Tbl
,ResponseVarName
)Mdl
)
that contains the results of boosting 100 classification trees and
the predictor and response data in the table Tbl
. ResponseVarName
is
the name of the response variable in Tbl
. By default, fitcensemble
uses
LogitBoost for binary classification and AdaBoostM2 for multiclass
classification.
applies Mdl
= fitcensemble(Tbl
,formula
)formula
to
fit the model to the predictor and response data in the table Tbl
. formula
is
an explanatory model of the response and a subset of predictor variables
in Tbl
used to fit Mdl
. For
example, 'Y~X1+X2+X3'
fits the response variable Tbl.Y
as
a function of the predictor variables Tbl.X1
, Tbl.X2
,
and Tbl.X3
.
uses
additional options specified by one or more Mdl
= fitcensemble(___,Name,Value
)Name,Value
pair
arguments and any of the input arguments in the previous syntaxes.
For example, you can specify the number of learning cycles, the ensembleaggregation
method, or to implement 10fold crossvalidation.
Create a predictive classification ensemble using all available predictor variables in the data. Then, train another ensemble using fewer predictors. Compare the insample predictive accuracies of the ensembles.
Load the census1994
data set.
load census1994
Train an ensemble of classification models using the entire data set and default options.
Mdl1 = fitcensemble(adultdata,'salary')
Mdl1 = classreg.learning.classif.ClassificationEnsemble PredictorNames: {1x14 cell} ResponseName: 'salary' CategoricalPredictors: [2 4 6 7 8 9 10 14] ClassNames: [<=50K >50K] ScoreTransform: 'none' NumObservations: 32561 NumTrained: 100 Method: 'LogitBoost' LearnerNames: {'Tree'} ReasonForTermination: 'Terminated normally after completing the requested number of training cycles.' FitInfo: [100x1 double] FitInfoDescription: {2x1 cell} Properties, Methods
Mdl
is a ClassificationEnsemble
model. Some notable characteristics of Mdl
are:
Because two classes are represented in the data, LogitBoost is the ensembleaggregation algorithm.
Because the ensembleaggregation method is a boosting algorithm, classification trees that allow a maximum of 10 splits compose the ensemble.
One hundred trees compose the ensemble.
Use the classification ensemble to predict the labels of a random set of five observations from the data. Compare the predicted labels with their true values.
rng(1) % For reproducibility [pX,pIdx] = datasample(adultdata,5); label = predict(Mdl1,pX); table(label,adultdata.salary(pIdx),'VariableNames',{'Predicted','Truth'})
ans=5×2 table
Predicted Truth
_________ _____
<=50K <=50K
<=50K <=50K
<=50K <=50K
<=50K <=50K
<=50K <=50K
Train a new ensemble using age
and education
only.
Mdl2 = fitcensemble(adultdata,'salary ~ age + education');
Compare the resubstitution losses between Mdl1
and Mdl2
.
rsLoss1 = resubLoss(Mdl1)
rsLoss1 = 0.1070
rsLoss2 = resubLoss(Mdl2)
rsLoss2 = 0.2037
The insample misclassification rate for the ensemble that uses all predictors is lower.
Estimate the generalization error of ensemble of boosted classification trees.
Load the ionosphere
data set.
load ionosphere
Crossvalidate an ensemble of classification trees using AdaBoostM1 and 10fold crossvalidation. Specify that each tree should be split a maximum of five times using a decision tree template.
rng(5); % For reproducibility t = templateTree('MaxNumSplits',5); Mdl = fitcensemble(X,Y,'Method','AdaBoostM1','Learners',t,'CrossVal','on');
Mdl
is a ClassificationPartitionedEnsemble
model.
Plot the cumulative, 10fold crossvalidated, misclassification rate. Display the estimated generalization error of the ensemble.
kflc = kfoldLoss(Mdl,'Mode','cumulative'); figure; plot(kflc); ylabel('10fold Misclassification rate'); xlabel('Learning cycle');
estGenError = kflc(end)
estGenError = 0.0712
kfoldLoss
returns the generalization error by default. However, plotting the cumulative loss allows you to monitor how the loss changes as weak learners accumulate in the ensemble.
The ensemble achieves a misclassification rate of around 0.06 after accumulating about 50 weak learners. Then, the misclassification rate increase slightly as more weak learners enter the ensemble.
If you are satisfied with the generalization error of the ensemble, then, to create a predictive model, train the ensemble again using all of the settings except crossvalidation. However, it is good practice to tune hyperparameters, such as the maximum number of decision splits per tree and the number of learning cycles.
This example shows how to optimize hyperparameters automatically using fitcensemble
. The example uses the ionosphere
data.
Load the data.
load ionosphere
Find hyperparameters that minimize fivefold crossvalidation loss by using automatic hyperparameter optimization.
For reproducibility, set the random seed and use the 'expectedimprovementplus'
acquisition function.
rng default Mdl = fitcensemble(X,Y,'OptimizeHyperparameters','auto',... 'HyperparameterOptimizationOptions',struct('AcquisitionFunctionName',... 'expectedimprovementplus'))
===================================================================================================================================  Iter  Eval  Objective  Objective  BestSoFar  BestSoFar  Method  NumLearningC LearnRate  MinLeafSize    result   runtime  (observed)  (estim.)   ycles    ===================================================================================================================================  1  Best  0.10256  6.9509  0.10256  0.10256  RUSBoost  11  0.010199  17   2  Best  0.062678  9.2751  0.062678  0.064264  LogitBoost  206  0.96537  33   3  Accept  0.099715  8.7593  0.062678  0.062688  AdaBoostM1  130  0.0072814  2   4  Accept  0.065527  1.5974  0.062678  0.062681  Bag  25    5   5  Accept  0.065527  7.4355  0.062678  0.062695  LogitBoost  178  0.52008  40   6  Accept  0.068376  6.2119  0.062678  0.062693  GentleBoost  146  0.46233  8   7  Accept  0.076923  18.49  0.062678  0.063613  GentleBoost  456  0.0018323  3   8  Accept  0.068376  19.9  0.062678  0.063878  LogitBoost  479  0.036176  7   9  Accept  0.068376  11.775  0.062678  0.065468  LogitBoost  277  0.99964  42   10  Accept  0.17379  0.58809  0.062678  0.064692  LogitBoost  11  0.0012008  1   11  Accept  0.065527  4.8851  0.062678  0.064854  Bag  100    1   12  Accept  0.076923  0.97125  0.062678  0.062571  GentleBoost  23  0.0096328  2   13  Accept  0.082621  0.76955  0.062678  0.064919  GentleBoost  18  0.0078878  61   14  Accept  0.065527  24.547  0.062678  0.06557  Bag  499    7   15  Accept  0.079772  14.329  0.062678  0.064962  GentleBoost  359  0.080649  1   16  Accept  0.35897  0.52584  0.062678  0.062491  Bag  10    171   17  Accept  0.35897  0.87997  0.062678  0.062483  AdaBoostM1  14  0.0029975  174   18  Accept  0.10826  31.777  0.062678  0.062484  RUSBoost  498  0.35355  1   19  Accept  0.64103  1.2122  0.062678  0.062469  RUSBoost  20  0.11564  175   20  Accept  0.091168  11.845  0.062678  0.062474  RUSBoost  187  0.0010337  5  ===================================================================================================================================  Iter  Eval  Objective  Objective  BestSoFar  BestSoFar  Method  NumLearningC LearnRate  MinLeafSize    result   runtime  (observed)  (estim.)   ycles    ===================================================================================================================================  21  Accept  0.076923  12.647  0.062678  0.062473  GentleBoost  322  0.020651  174   22  Accept  0.065527  4.1482  0.062678  0.062473  AdaBoostM1  63  0.95202  1   23  Accept  0.17379  6.6579  0.062678  0.062356  LogitBoost  166  0.0011034  175   24  Accept  0.17379  0.87522  0.062678  0.062611  LogitBoost  20  0.0011381  15   25  Accept  0.062678  5.0428  0.062678  0.062619  LogitBoost  125  0.9709  4   26  Accept  0.11681  0.68826  0.062678  0.062621  RUSBoost  10  0.93628  6   27  Accept  0.082621  0.88836  0.062678  0.062716  GentleBoost  19  0.94744  75   28  Accept  0.065527  5.3467  0.062678  0.064168  LogitBoost  131  0.99024  9   29  Accept  0.068376  24.065  0.062678  0.064255  Bag  489    2   30  Accept  0.076923  18.562  0.062678  0.063851  GentleBoost  458  0.0010094  16  __________________________________________________________ Optimization completed. MaxObjectiveEvaluations of 30 reached. Total function evaluations: 30 Total elapsed time: 301.149 seconds. Total objective function evaluation time: 261.6473 Best observed feasible point: Method NumLearningCycles LearnRate MinLeafSize __________ _________________ _________ ___________ LogitBoost 206 0.96537 33 Observed objective function value = 0.062678 Estimated objective function value = 0.063851 Function evaluation time = 9.2751 Best estimated feasible point (according to models): Method NumLearningCycles LearnRate MinLeafSize __________ _________________ _________ ___________ LogitBoost 277 0.99964 42 Estimated objective function value = 0.063851 Estimated function evaluation time = 11.8227
Mdl = classreg.learning.classif.ClassificationEnsemble ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' NumObservations: 351 HyperparameterOptimizationResults: [1×1 BayesianOptimization] NumTrained: 277 Method: 'LogitBoost' LearnerNames: {'Tree'} ReasonForTermination: 'Terminated normally after completing the requested number of training cycles.' FitInfo: [277×1 double] FitInfoDescription: {2×1 cell} Properties, Methods
The optimization searched over the methods for binary classification, over NumLearningCycles
, over the LearnRate
for applicable methods, and over the tree learner MinLeafSize
. The output is the ensemble classifier with the minimum estimated crossvalidation loss.
One way to create an ensemble of boosted classification trees that has satisfactory predictive performance is by tuning the decision treecomplexity level using crossvalidation. While searching for an optimal complexity level, tune the learning rate to minimize the number of learning cycles.
Load the ionosphere
data set.
load ionosphere
To search for the optimal treecomplexity level:
Crossvalidate a set of ensembles. Exponentially increase the treecomplexity level for subsequent ensembles from decision stump (one split) to at most n  1 splits. n is the sample size. Also, vary the learning rate for each ensemble between 0.1 to 1.
Estimate the crossvalidated misclassification rate of each ensemble.
For treecomplexity level , , compare the cumulative, crossvalidated misclassification rate of the ensembles by plotting them against number of learning cycles. Plot separate curves for each learning rate on the same figure.
Choose the curve that achieves the minimal misclassification rate, and note the corresponding learning cycle and learning rate.
Crossvalidate a deep classification tree and a stump. These classification trees serve as benchmarks.
rng(1); % For reproducibility MdlDeep = fitctree(X,Y,'CrossVal','on','MergeLeaves','off',... 'MinParentSize',1); MdlStump = fitctree(X,Y,'MaxNumSplits',1,'CrossVal','on');
Crossvalidate an ensemble of 150 boosted classification trees using 5fold crossvalidation. Using a tree template, vary the maximum number of splits using the values in the sequence . m is such that is no greater than n  1. For each variant, adjust the learning rate using each value in the set {0.1, 0.25, 0.5, 1};
n = size(X,1); m = floor(log(n  1)/log(3)); learnRate = [0.1 0.25 0.5 1]; numLR = numel(learnRate); maxNumSplits = 3.^(0:m); numMNS = numel(maxNumSplits); numTrees = 150; Mdl = cell(numMNS,numLR); for k = 1:numLR; for j = 1:numMNS; t = templateTree('MaxNumSplits',maxNumSplits(j)); Mdl{j,k} = fitcensemble(X,Y,'NumLearningCycles',numTrees,... 'Learners',t,'KFold',5,'LearnRate',learnRate(k)); end; end;
Estimate the cumulative, crossvalidated misclassification rate for each ensemble and the classification trees serving as benchmarks.
kflAll = @(x)kfoldLoss(x,'Mode','cumulative'); errorCell = cellfun(kflAll,Mdl,'Uniform',false); error = reshape(cell2mat(errorCell),[numTrees numel(maxNumSplits) numel(learnRate)]); errorDeep = kfoldLoss(MdlDeep); errorStump = kfoldLoss(MdlStump);
Plot how the crossvalidated misclassification rate behaves as the number of trees in the ensemble increases. Plot the curves with respect to learning rate on the same plot, and plot separate plots for varying treecomplexity levels. Choose a subset of tree complexity levels to plot.
mnsPlot = [1 round(numel(maxNumSplits)/2) numel(maxNumSplits)]; figure; for k = 1:3; subplot(2,2,k); plot(squeeze(error(:,mnsPlot(k),:)),'LineWidth',2); axis tight; hold on; h = gca; plot(h.XLim,[errorDeep errorDeep],'.b','LineWidth',2); plot(h.XLim,[errorStump errorStump],'.r','LineWidth',2); plot(h.XLim,min(min(error(:,mnsPlot(k),:))).*[1 1],'k'); h.YLim = [0 0.2]; xlabel 'Number of trees'; ylabel 'Crossvalidated misclass. rate'; title(sprintf('MaxNumSplits = %0.3g', maxNumSplits(mnsPlot(k)))); hold off; end; hL = legend([cellstr(num2str(learnRate','Learning Rate = %0.2f'));... 'Deep Tree';'Stump';'Min. misclass. rate']); hL.Position(1) = 0.6;
Each curve contains a minimum crossvalidated misclassification rate occurring at the optimal number of trees in the ensemble.
Identify the maximum number of splits, number of trees, and learning rate that yields the lowest misclassification rate overall.
[minErr,minErrIdxLin] = min(error(:)); [idxNumTrees,idxMNS,idxLR] = ind2sub(size(error),minErrIdxLin); fprintf('\nMin. misclass. rate = %0.5f',minErr) fprintf('\nOptimal Parameter Values:\nNum. Trees = %d',idxNumTrees); fprintf('\nMaxNumSplits = %d\nLearning Rate = %0.2f\n',... maxNumSplits(idxMNS),learnRate(idxLR))
Min. misclass. rate = 0.05413 Optimal Parameter Values: Num. Trees = 47 MaxNumSplits = 3 Learning Rate = 0.25
Create a predictive ensemble based on the optimal hyperparameters and the entire training set.
tFinal = templateTree('MaxNumSplits',maxNumSplits(idxMNS)); MdlFinal = fitcensemble(X,Y,'NumLearningCycles',idxNumTrees,... 'Learners',tFinal,'LearnRate',learnRate(idxLR))
MdlFinal = classreg.learning.classif.ClassificationEnsemble ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' NumObservations: 351 NumTrained: 47 Method: 'LogitBoost' LearnerNames: {'Tree'} ReasonForTermination: 'Terminated normally after completing the requested number of training cycles.' FitInfo: [47×1 double] FitInfoDescription: {2×1 cell}
MdlFinal
is a ClassificationEnsemble
. To predict whether a radar return is good given predictor data, pass the predictor data and MdlFinal
to predict
.
Tbl
— Sample dataSample data used to train the model, specified as a table. Each
row of Tbl
corresponds to one observation, and
each column corresponds to one predictor variable. Tbl
can
contain one additional column for the response variable. Multicolumn
variables and cell arrays other than cell arrays of character vectors
are not allowed.
If Tbl
contains the response variable
and you want to use all remaining variables as predictors, then specify
the response variable using ResponseVarName
.
If Tbl
contains the response
variable, and you want to use a subset of the remaining variables
only as predictors, then specify a formula using formula
.
If Tbl
does not contain the response
variable, then specify the response data using Y
.
The length of response variable and the number of rows of Tbl
must
be equal.
To save memory and execution time, supply X
and Y
instead
of Tbl
.
Data Types: table
ResponseVarName
— Response variable nameTbl
Response variable name, specified as the name of the response
variable in Tbl
.
You must specify ResponseVarName
as a character vector or string scalar.
For example, if Tbl.Y
is the response variable, then
specify ResponseVarName
as 'Y'
.
Otherwise, fitcensemble
treats all columns of
Tbl
as predictor variables.
The response variable must be a categorical, character, or string array, logical or numeric vector, or cell array of character vectors. If the response variable is a character array, then each element must correspond to one row of the array.
For classification, you can specify the order of the classes
using the ClassNames
namevalue pair argument.
Otherwise, fitcensemble
determines the class order,
and stores it in the Mdl.ClassNames
.
Data Types: char
 string
formula
— Explanatory model of response and subset of predictor variablesExplanatory model of the response and a subset of the predictor variables, specified as a
character vector or string scalar in the form of 'Y~X1+X2+X3'
. In
this form, Y
represents the response variable, and
X1
, X2
, and X3
represent
the predictor variables. The variables must be variable names in Tbl
(Tbl.Properties.VariableNames
).
To specify a subset of variables in Tbl
as
predictors for training the model, use a formula. If you specify a
formula, then the software does not use any variables in Tbl
that
do not appear in formula
.
Data Types: char
 string
X
— Predictor dataPredictor data, specified as numeric matrix.
Each row corresponds to one observation, and each column corresponds to one predictor variable.
The length of Y
and the number of rows
of X
must be equal.
To specify the names of the predictors in the order of their
appearance in X
, use the PredictorNames
namevalue
pair argument.
Data Types: single
 double
Y
— Response dataResponse data, specified as a categorical, character, or string array, logical or numeric
vector, or cell array of character vectors. Each entry in
Y
is the response to or label for the observation in
the corresponding row of X
or Tbl
.
The length of Y
and the number of rows of
X
or Tbl
must be equal. If the
response variable is a character array, then each element must correspond to
one row of the array.
You can specify the order of the classes using the ClassNames
namevalue
pair argument. Otherwise, fitcensemble
determines
the class order, and stores it in the Mdl.ClassNames
.
Data Types: categorical
 char
 string
 logical
 single
 double
 cell
Specify optional
commaseparated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as Name1,Value1,...,NameN,ValueN
.
'CrossVal','on','LearnRate',0.05
specifies to implement
10fold crossvalidation and to use 0.05
as the learning
rate.You cannot use any crossvalidation namevalue pair argument along with the
'OptimizeHyperparameters'
namevalue pair argument. You can modify
the crossvalidation for 'OptimizeHyperparameters'
only by using the
'HyperparameterOptimizationOptions'
namevalue pair
argument.
'Method'
— Ensembleaggregation method'AdaBoostM1'
 'LogitBoost'
 'GentleBoost'
 'RobustBoost'
 'LPBoost'
 'TotalBoost'
 'RUSBoost'
 'Subspace'
 'Bag'
 'AdaBoostM2'
Ensembleaggregation method, specified as the comma separated pair
consisting of 'Method'
and a value such as
'RUSBoost'
.
Value  Description  Classification Problem Support  Additional Requirements 

'AdaBoostM1'  Adaptive boosting  Binary only   
'AdaBoostM2'  Adaptive boosting  Multiclass only   
'Bag'  Bootstrap aggregating (e.g., random forest)  Binary and multiclass   
'GentleBoost'  Gentle adaptive boosting  Binary only   
'LogitBoost'  Adaptive logistic regression  Binary only   
'LPBoost'  Linear programming boosting  Binary and multiclass  Optimization Toolbox™ license 
'RobustBoost'  Robust boosting  Binary only  Optimization Toolbox license 
'RUSBoost'  Random undersampling boosting  Binary and multiclass   
'Subspace'  Random subspace  Binary and multiclass   
'TotalBoost'  Totally corrective boosting  Binary and multiclass  Optimization Toolbox license 
For binary classification problems, the default is
'LogitBoost'
. Otherwise, the default is
'AdaBoostM2'
.
Example: 'Method','Bag'
'NumLearningCycles'
— Number of ensemble learning cycles100
(default)  positive integer  'AllPredictorCombinations'
Number of ensemble learning cycles, specified as a positive integer or
'AllPredictorCombinations'
.
If you specify a positive integer, then, at every learning
cycle, the software trains one weak learner for every template
object in Learners
. Consequently, the
software trains
NumLearningCycles*numel(Learners)
learners.
If you specify 'AllPredictorCombinations'
,
then set Method
to
'Subspace'
and specify one learner only
for Learners
. With these settings, the
software trains learners for all possible combinations of
predictors taken NPredToSample
at a time.
Consequently, the software trains nchoosek
(size(X,2),NPredToSample)
leaners.
The software composes the ensemble using all trained learners and
stores them in Mdl.Trained
.
For more details, see Tips.
Example: 'NumLearningCycles',500
Data Types: single
 double
 char
 string
'Learners'
— Weak learners to use in ensembleWeak learners to use in the ensemble, specified as a weaklearner name, weaklearner template object, or cell vector of weaklearner template objects.
Weak Learner  WeakLearner Name  Template Object Creation Function  Method
Settings 

Discriminant analysis  'discriminant'  templateDiscriminant  Recommended for 'Subspace' 
k nearest neighbors  'knn'  templateKNN  For 'Subspace' only 
Decision tree  'tree'  templateTree  All methods except
'Subspace' 
The defaults are:
'knn'
if 'Method'
is 'Subspace'
'tree'
if 'Method'
is 'Bag'
templateTree('MaxNumSplits',10)
if
'Method'
is any boosting
method
fitcensemble
sets the maximum number of splits
for decision tree weak learners to 10
when both of
these conditions occur:
You do not set a value for the
'MaxNumSplits'
property of the
decision tree weak learners.
You use a boosting algorithm as the ensembleaggregation
method for fitcensemble
.
For example, if you specify
'Learners',templateTree()
and
'Method','AdaBoostM1'
, then
fitcensemble
sets the maximum number of splits
of the decision tree weak learners to 10
.
Otherwise, fitcensemble
defers to the
corresponding template object to choose any required default values for
the 'Learners'
namevalue pair argument.
For details on the number of learners to train, see
NumLearningCycles
and Tips.
Example: For an ensemble composed of two types of classification trees,
supply {t1 t2}
, where t1
and
t2
are classification tree
templates.
'NPrint'
— Printout frequency'off'
(default)  positive integerPrintout frequency, specified as the commaseparated pair consisting
of 'NPrint'
and a positive integer or 'off'
.
To track the number of weak learners or folds that
fitcensemble
trained so far, specify a positive integer. That
is, if you specify the positive integer m:
Without also specifying any crossvalidation option
(for example, CrossVal
), then fitcensemble
displays
a message to the command line every time it completes training m weak
learners.
And a crossvalidation option, then fitcensemble
displays
a message to the command line every time it finishes training m folds.
If you specify 'off'
, then fitcensemble
does
not display a message when it completes training weak learners.
When training an ensemble of many weak learners on a large data
set, specify a positive integer for NPrint
.
Example: 'NPrint',5
Data Types: single
 double
 char
 string
'CategoricalPredictors'
— Categorical predictors list'all'
Categorical predictors
list, specified as the commaseparated pair consisting of
'CategoricalPredictors'
and one of the values in this table.
Value  Description 

Vector of positive integers  An entry in the vector is the index value corresponding to the column of the
predictor data (X or Tbl ) that contains a
categorical variable. 
Logical vector  A true entry means that the corresponding column of predictor
data (X or Tbl ) is a categorical
variable. 
Character matrix  Each row of the matrix is the name of a predictor variable. The names must match
the entries in PredictorNames . 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 entries in PredictorNames . 
'all'  All predictors are categorical. 
Specification of CategoricalPredictors
is
appropriate if:
'Learners'
is 'tree'
.
'Learners'
is 'knn'
when all predictors are
categorical.
By default, if the
predictor data is in a table (Tbl
), fitcensemble
assumes that a variable is categorical if it contains logical values, categorical values, a
string array, or a cell array of character vectors. If the predictor data is a matrix
(X
), fitcensemble
assumes all predictors are
continuous. To identify any categorical predictors when the data is a matrix, use the 'CategoricalPredictors'
namevalue pair
argument.
Example: 'CategoricalPredictors','all'
Data Types: single
 double
 logical
 char
 string
 cell
'PredictorNames'
— Predictor variable namesPredictor variable names, specified as the commaseparated pair consisting of
'PredictorNames'
and a string array of unique names or cell array
of unique character vectors. The functionality of 'PredictorNames'
depends on the way you supply the training data.
If you supply X
and Y
, then you
can use 'PredictorNames'
to give the predictor variables
in X
names.
The order of the names in PredictorNames
must correspond to the column order of X
.
That is, PredictorNames{1}
is the name of
X(:,1)
,
PredictorNames{2}
is the name of
X(:,2)
, and so on. Also,
size(X,2)
and
numel(PredictorNames)
must be
equal.
By default, PredictorNames
is
{'x1','x2',...}
.
If you supply Tbl
, then you can use
'PredictorNames'
to choose which predictor variables
to use in training. That is, fitcensemble
uses only the
predictor variables in PredictorNames
and the response
variable in training.
PredictorNames
must be a subset of
Tbl.Properties.VariableNames
and cannot
include the name of the response variable.
By default, PredictorNames
contains the
names of all predictor variables.
It is a good practice to specify the predictors for training
using either 'PredictorNames'
or
formula
only.
Example: 'PredictorNames',{'SepalLength','SepalWidth','PetalLength','PetalWidth'}
Data Types: string
 cell
'ResponseName'
— Response variable name'Y'
(default)  character vector  string scalarResponse variable name, specified as the commaseparated pair consisting of
'ResponseName'
and a character vector or string scalar.
If you supply Y
, then you can
use 'ResponseName'
to specify a name for the response
variable.
If you supply ResponseVarName
or formula
,
then you cannot use 'ResponseName'
.
Example: 'ResponseName','response'
Data Types: char
 string
'CrossVal'
— Crossvalidation flag'off'
(default)  'on'
Crossvalidation flag, specified as the commaseparated pair
consisting of 'Crossval'
and 'on'
or 'off'
.
If you specify 'on'
, then the software implements
10fold crossvalidation.
To override this crossvalidation setting, use one of these
namevalue pair arguments: CVPartition
, Holdout
, KFold
,
or Leaveout
. To create a crossvalidated model,
you can use one crossvalidation namevalue pair argument at a time
only.
Alternatively, crossvalidate later by passing Mdl
to crossval
or crossval
.
Example: 'Crossval','on'
'CVPartition'
— Crossvalidation partition[]
(default)  cvpartition
partition objectCrossvalidation partition, specified as the commaseparated pair consisting of
'CVPartition'
and a cvpartition
partition
object as created by cvpartition
. The partition object
specifies the type of crossvalidation and the indexing for the training and validation
sets.
To create a crossvalidated model, you can use one of these four namevalue pair arguments
only: 'CVPartition'
, 'Holdout'
,
'KFold'
, or 'Leaveout'
.
Example: Suppose you create a random partition for 5fold crossvalidation on 500
observations by using cvp = cvpartition(500,'KFold',5)
. Then, you can
specify the crossvalidated model by using
'CVPartition',cvp
.
'Holdout'
— Fraction of data for holdout validationFraction of the data used for holdout validation, specified as the commaseparated pair
consisting of 'Holdout'
and a
scalar value in the range (0,1). If you specify
'Holdout',
,
then the software completes these steps: p
Randomly select and reserve
%
of the data as validation data, and train the
model using the rest of the data.p
*100
Store the compact, trained model in the
Trained
property of the
crossvalidated model.
To create a crossvalidated model, you can use one of these
four namevalue pair arguments only: CVPartition
, Holdout
, KFold
,
or Leaveout
.
Example: 'Holdout',0.1
Data Types: double
 single
'KFold'
— Number of folds10
(default)  positive integer value greater than 1Number of folds to use in a crossvalidated model, specified as the commaseparated pair
consisting of 'KFold'
and a positive integer value greater than 1. If
you specify 'KFold',k
, then the software completes
these steps.
Randomly partition the data into k sets.
For each set, reserve the set as validation data, and train the model using the other k – 1 sets.
Store the k
compact, trained models in the
cells of a k
by1 cell vector in the
Trained
property of the crossvalidated model.
To create a crossvalidated model, you can use one of these
four namevalue pair arguments only: CVPartition
, Holdout
, KFold
,
or Leaveout
.
Example: 'KFold',5
Data Types: single
 double
'Leaveout'
— Leaveoneout crossvalidation flag'off'
(default)  'on'
Leaveoneout crossvalidation flag, specified as the commaseparated pair consisting of
'Leaveout'
and 'on'
or
'off'
. If you specify 'Leaveout','on'
, then,
for each of the n observations (where n is the
number of observations excluding missing observations, specified in the
NumObservations
property of the model), the software completes
these steps:
Reserve the observation as validation data, and train the model using the other n – 1 observations.
Store the n compact, trained models in the cells of an
nby1 cell vector in the Trained
property of the crossvalidated model.
To create a crossvalidated model, you can use one of these
four namevalue pair arguments only: CVPartition
, Holdout
, KFold
,
or Leaveout
.
Example: 'Leaveout','on'
'ClassNames'
— Names of classes to use for trainingNames of classes to use for training, specified as the commaseparated pair consisting of
'ClassNames'
and a categorical, character, or string array, a
logical or numeric vector, or a cell array of character vectors.
ClassNames
must have the same data type as
Y
.
If ClassNames
is a character array, then each element must correspond to
one row of the array.
Use ClassNames
to:
Order the classes during training.
Specify the order of any input or output argument
dimension that corresponds to the class order. For example, use ClassNames
to
specify the order of the dimensions of Cost
or
the column order of classification scores returned by predict
.
Select a subset of classes for training. For example,
suppose that the set of all distinct class names in Y
is {'a','b','c'}
.
To train the model using observations from classes 'a'
and 'c'
only,
specify 'ClassNames',{'a','c'}
.
The default value for ClassNames
is the set of all distinct class names in
Y
.
Example: 'ClassNames',{'b','g'}
Data Types: categorical
 char
 string
 logical
 single
 double
 cell
'Cost'
— Misclassification costMisclassification cost, specified as the commaseparated pair
consisting of 'Cost'
and a square matrix or structure.
If you specify:
The square matrix Cost
, then Cost(i,j)
is
the cost of classifying a point into class j
if
its true class is i
. That is, the rows correspond
to the true class and the columns correspond to the predicted class.
To specify the class order for the corresponding rows and columns
of Cost
, also specify the ClassNames
namevalue
pair argument.
The structure S
, then it must have
two fields:
S.ClassNames
, which contains the
class names as a variable of the same data type as Y
S.ClassificationCosts
, which contains
the cost matrix with rows and columns ordered as in S.ClassNames
The default is ones(
, where K
) 
eye(K
)K
is
the number of distinct classes.
fitcensemble
uses Cost
to
adjust the prior class probabilities specified in Prior
.
Then, fitcensemble
uses the adjusted prior probabilities
for training and resets the cost matrix to its default.
Example: 'Cost',[0 1 2 ; 1 0 2; 2 2 0]
Data Types: double
 single
 struct
'Prior'
— Prior probabilities'empirical'
(default)  'uniform'
 numeric vector  structure arrayPrior probabilities for each class, specified as the commaseparated
pair consisting of 'Prior'
and a value in this
table.
Value  Description 

'empirical'  The class prior probabilities are the class relative frequencies
in Y . 
'uniform'  All class prior probabilities are equal to 1/K, where K is the number of classes. 
numeric vector  Each element is a class prior probability. Order the elements according to
Mdl.ClassNames or specify the order using the
ClassNames namevalue pair argument. The
software normalizes the elements such that they sum to
1 . 
structure array  A structure

fitcensemble
normalizes
the prior probabilities in Prior
to sum to 1.
Example: struct('ClassNames',{{'setosa','versicolor','virginica'}},'ClassProbs',1:3)
Data Types: char
 string
 double
 single
 struct
'ScoreTransform'
— Score transformation'none'
(default)  'doublelogit'
 'invlogit'
 'ismax'
 'logit'
 function handle  ...Score transformation, specified as the commaseparated pair consisting of
'ScoreTransform'
and a character vector, string scalar, or
function handle.
This table summarizes the available character vectors and string scalars.
Value  Description 

'doublelogit'  1/(1 + e^{–2x}) 
'invlogit'  log(x / (1–x)) 
'ismax'  Sets the score for the class with the largest score to 1 , and sets the
scores for all other classes to 0 
'logit'  1/(1 + e^{–x}) 
'none' or 'identity'  x (no transformation) 
'sign'  –1 for x < 0 0 for x = 0 1 for x > 0 
'symmetric'  2x – 1 
'symmetricismax'  Sets the score for the class with the largest score to 1 ,
and sets the scores for all other classes to –1 
'symmetriclogit'  2/(1 + e^{–x}) – 1 
For a MATLAB^{®} function or a function you define, use its function handle for score transform. The function handle must accept a matrix (the original scores) and return a matrix of the same size (the transformed scores).
Example: 'ScoreTransform','logit'
Data Types: char
 string
 function_handle
'Weights'
— Observation weightsTbl
Observation weights, specified as the commaseparated pair consisting
of 'Weights'
and a numeric vector of positive values
or name of a variable in Tbl
. The software weighs
the observations in each row of X
or Tbl
with
the corresponding value in Weights
. The size of Weights
must
equal the number of rows of X
or Tbl
.
If you specify the input data as a table Tbl
, then
Weights
can be the name of a variable in Tbl
that contains a numeric vector. In this case, you must specify
Weights
as a character vector or string scalar. For example, if
the weights vector W
is stored as Tbl.W
, then
specify it as 'W'
. Otherwise, the software treats all columns of
Tbl
, including W
, as predictors or the
response when training the model.
The software normalizes Weights
to sum up
to the value of the prior probability in the respective class.
By default, Weights
is ones(
,
where n
,1)n
is the number of observations in X
or Tbl
.
Data Types: double
 single
 char
 string
'FResample'
— Fraction of training set to resample1
(default)  positive scalar in (0,1]'Replace'
— Flag indicating to sample with replacement'on'
(default)  'off'
Flag indicating sampling with replacement, specified as the
commaseparated pair consisting of 'Replace'
and 'off'
or 'on'
.
For 'on'
, the software samples
the training observations with replacement.
For 'off'
, the software samples
the training observations without replacement. If you set Resample
to 'on'
,
then the software samples training observations assuming uniform weights.
If you also specify a boosting method, then the software boosts by
reweighting observations.
Unless you set Method
to 'bag'
or
set Resample
to 'on'
, Replace
has
no effect.
Example: 'Replace','off'
'Resample'
— Flag indicating to resample'off'
 'on'
Flag indicating to resample, specified as the commaseparated
pair consisting of 'Resample'
and 'off'
or 'on'
.
If Method
is any boosting method,
then:
'Resample','on'
specifies to sample
training observations using updated weights as the multinomial sampling
probabilities.
'Resample','off'
specifies to reweight
observations at every learning iteration. This setting is the default.
If Method
is 'bag'
,
then 'Resample'
must be 'on'
.
The software resamples a fraction of the training observations (see FResample
)
with or without replacement (see Replace
).
'LearnRate'
— Learning rate for shrinkage1
(default)  numeric scalar in (0,1]Learning rate for shrinkage, specified as the commaseparated pair consisting of a numeric scalar in the interval (0,1].
To train an ensemble using shrinkage, set LearnRate
to
a value less than 1
, for example, 0.1
is
a popular choice. Training an ensemble using shrinkage requires more
learning iterations, but often achieves better accuracy.
Example: 'LearnRate',0.1
Data Types: single
 double
'LearnRate'
— Learning rate for shrinkage1
(default)  numeric scalar in (0,1]Learning rate for shrinkage, specified as the commaseparated pair consisting of a numeric scalar in the interval (0,1].
To train an ensemble using shrinkage, set LearnRate
to
a value less than 1
, for example, 0.1
is
a popular choice. Training an ensemble using shrinkage requires more
learning iterations, but often achieves better accuracy.
Example: 'LearnRate',0.1
Data Types: single
 double
'RatioToSmallest'
— Sampling proportion with respect to lowestrepresented classSampling proportion with respect to the lowestrepresented class,
specified as the commaseparated pair consisting of 'RatioToSmallest'
and
a numeric scalar or numeric vector of positive values with length
equal to the number of distinct classes in the training data.
Suppose that there are K
classes
in the training data and the lowestrepresented class has m
observations
in the training data.
If you specify the positive numeric scalar s
,
then fitcensemble
samples
observations
from each class, that is, it uses the same sampling proportion for
each class. For more details, see Algorithms.s
*m
If you specify the numeric vector [
,
then s1
,s2
,...,sK
]fitcensemble
samples
observations
from class si
*m
i
, i
=
1,...,K. The elements of RatioToSmallest
correspond
to the order of the class names specified using ClassNames
(see Tips).
The default value is ones(
,
which specifies to sample K
,1)m
observations
from each class.
Example: 'RatioToSmallest',[2,1]
Data Types: single
 double
'MarginPrecision'
— Margin precision to control convergence speed0.1
(default)  numeric scalar in [0,1]Margin precision to control convergence speed, specified as
the commaseparated pair consisting of 'MarginPrecision'
and
a numeric scalar in the interval [0,1]. MarginPrecision
affects
the number of boosting iterations required for convergence.
To train an ensemble using many learners, specify a small value
for MarginPrecision
. For training using a few learners,
specify a large value.
Example: 'MarginPrecision',0.5
Data Types: single
 double
'RobustErrorGoal'
— Target classification error0.1
(default)  nonnegative numeric scalarTarget classification error, specified as the commaseparated
pair consisting of 'RobustErrorGoal'
and a nonnegative
numeric scalar. The upper bound on possible values depends on the
values of RobustMarginSigma
and RobustMaxMargin
.
However, the upper bound cannot exceed 1
.
For a particular training set, usually there is an optimal range
for RobustErrorGoal
. If you set it too low or too
high, then the software can produce a model with poor classification
accuracy. Try crossvalidating to search for the appropriate value.
Example: 'RobustErrorGoal',0.05
Data Types: single
 double
'RobustMarginSigma'
— Classification margin distribution spread0.1
(default)  positive numeric scalarClassification margin distribution spread over the training
data, specified as the commaseparated pair consisting of 'RobustMarginSigma'
and
a positive numeric scalar. Before specifying RobustMarginSigma
,
consult the literature on RobustBoost
, for example, [19].
Example: 'RobustMarginSigma',0.5
Data Types: single
 double
'RobustMaxMargin'
— Maximal classification margin0
(default)  nonnegative numeric scalarMaximal classification margin in the training data, specified
as the commaseparated pair consisting of 'RobustMaxMargin'
and
a nonnegative numeric scalar. The software minimizes the number of
observations in the training data having classification margins below RobustMaxMargin
.
Example: 'RobustMaxMargin',1
Data Types: single
 double
'NPredToSample'
— Number of predictors to sample1
(default)  positive integerNumber of predictors to sample for each random subspace learner,
specified as the commaseparated pair consisting of 'NPredToSample'
and
a positive integer in the interval 1,...,p, where p is
the number of predictor variables (size(X,2)
or size(Tbl,2)
).
Data Types: single
 double
'OptimizeHyperparameters'
— Parameters to optimize'none'
(default)  'auto'
 'all'
 string array or cell array of eligible parameter names  vector of optimizableVariable
objectsParameters to optimize, specified as the commaseparated pair
consisting of 'OptimizeHyperparameters'
and one of
the following:
'none'
— Do not optimize.
'auto'
— Use
{'Method','NumLearningCycles','LearnRate'}
along with the default parameters for the specified
Learners
:
Learners
=
'tree'
(default) —
{'MinLeafSize'}
Learners
=
'discriminant'
—
{'Delta','Gamma'}
Learners
=
'knn'
—
{'Distance','NumNeighbors'}
For hyperparameter optimization,
Learners
must be a single argument,
not a string array or cell array.
'all'
— Optimize all eligible
parameters.
String array or cell array of eligible parameter names
Vector of optimizableVariable
objects,
typically the output of hyperparameters
The optimization attempts to minimize the crossvalidation loss
(error) for fitcensemble
by varying the parameters.
For information about crossvalidation loss (albeit in a different
context), see Classification Loss. To control the
crossvalidation type and other aspects of the optimization, use the
HyperparameterOptimizationOptions
namevalue
pair.
'OptimizeHyperparameters'
values override any values you set using
other namevalue pair arguments. For example, setting
'OptimizeHyperparameters'
to 'auto'
causes the
'auto'
values to apply.
The eligible parameters for fitcensemble
are:
Method
— Depends on the number of
classes.
Two classes — Eligible methods are
'Bag'
,
'GentleBoost'
,
'LogitBoost'
,
'AdaBoostM1'
, and
'RUSBoost'
.
Three or more classes — Eligible methods are
'Bag'
,
'AdaBoostM2'
, and
'RUSBoost'
.
NumLearningCycles
—
fitcensemble
searches among positive
integers, by default logscaled with range
[10,500]
.
LearnRate
—
fitcensemble
searches among positive
reals, by default logscaled with range
[1e3,1]
.
The eligible hyperparameters for the chosen
Learners
:
Learners  Eligible
Hyperparameters Bold = Used By Default  Default Range 

'discriminant'  Delta  Logscaled in the range
[1e6,1e3] 
DiscrimType  'linear' ,
'quadratic' ,
'diagLinear' ,
'diagQuadratic' ,
'pseudoLinear' , and
'pseudoQuadratic'  
Gamma  Real values in
[0,1]  
'knn'  Distance  'cityblock' ,
'chebychev' ,
'correlation' ,
'cosine' ,
'euclidean' ,
'hamming' ,
'jaccard' ,
'mahalanobis' ,
'minkowski' ,
'seuclidean' , and
'spearman' 
DistanceWeight  'equal' ,
'inverse' , and
'squaredinverse'  
Exponent  Positive values in
[0.5,3]  
NumNeighbors  Positive integer values logscaled in the
range [1,
max(2,round(NumObservations/2))]  
Standardize  'true' and
'false'  
'tree'  MaxNumSplits  Integers logscaled in the range
[1,max(2,NumObservations1)] 
MinLeafSize  Integers logscaled in the range
[1,max(2,floor(NumObservations/2))]  
NumVariablesToSample  Integers in the range
[1,max(2,NumPredictors)]  
SplitCriterion  'gdi' ,
'deviance' , and
'twoing' 
Alternatively, use hyperparameters
with your chosen Learners
.
load fisheriris % hyperparameters requires data and learner params = hyperparameters('fitcensemble',meas,species,'Tree');
To see the eligible and default hyperparameters, examine
params
.
Set nondefault parameters by passing a vector of
optimizableVariable
objects that have nondefault
values. For example,
load fisheriris params = hyperparameters('fitcensemble',meas,species,'Tree'); params(4).Range = [1,30];
Pass params
as the value of
OptimizeHyperparameters
.
By default, iterative display appears at the command line, and
plots appear according to the number of hyperparameters in the optimization. For the
optimization and plots, the objective function is log(1 + crossvalidation loss) for regression and the misclassification rate for classification. To control
the iterative display, set the Verbose
field of the
'HyperparameterOptimizationOptions'
namevalue pair argument. To
control the plots, set the ShowPlots
field of the
'HyperparameterOptimizationOptions'
namevalue pair argument.
For an example, see Optimize Classification Ensemble.
Example: 'auto'
'HyperparameterOptimizationOptions'
— Options for optimizationOptions for optimization, specified as the commaseparated pair consisting of
'HyperparameterOptimizationOptions'
and a structure. This
argument modifies the effect of the OptimizeHyperparameters
namevalue pair argument. All fields in the structure are optional.
Field Name  Values  Default 

Optimizer 
 'bayesopt' 
AcquisitionFunctionName 
For details, see the
 'expectedimprovementpersecondplus' 
MaxObjectiveEvaluations  Maximum number of objective function evaluations.  30 for 'bayesopt' or 'randomsearch' , and the entire grid for 'gridsearch' 
MaxTime  Time limit, specified as a positive real. The time limit is in seconds, as measured by  Inf 
NumGridDivisions  For 'gridsearch' , the number of values in each dimension. The value can be
a vector of positive integers giving the number of
values for each dimension, or a scalar that
applies to all dimensions. This field is ignored
for categorical variables.  10 
ShowPlots  Logical value indicating whether to show plots. If true , this field plots
the best objective function value against the
iteration number. If there are one or two
optimization parameters, and if
Optimizer is
'bayesopt' , then
ShowPlots also plots a model of
the objective function against the
parameters.  true 
SaveIntermediateResults  Logical value indicating whether to save results when Optimizer is
'bayesopt' . If
true , this field overwrites a
workspace variable named
'BayesoptResults' at each
iteration. The variable is a BayesianOptimization object.  false 
Verbose  Display to the command line.
For details, see the
 1 
UseParallel  Logical value indicating whether to run Bayesian optimization in parallel, which requires Parallel Computing Toolbox™. For details, see Parallel Bayesian Optimization.  false 
Repartition  Logical value indicating whether to repartition the crossvalidation at every iteration. If
 false 
Use no more than one of the following three field names.  
CVPartition  A cvpartition object, as created by cvpartition .  'Kfold',5 if you do not specify any
crossvalidation field 
Holdout  A scalar in the range (0,1) representing the holdout fraction.  
Kfold  An integer greater than 1. 
Example: 'HyperparameterOptimizationOptions',struct('MaxObjectiveEvaluations',60)
Data Types: struct
Mdl
— Trained classification ensemble modelClassificationBaggedEnsemble
model object  ClassificationEnsemble
model object  ClassificationPartitionedEnsemble
crossvalidated
model objectTrained ensemble model, returned as one of the model objects in this table.
Model Object  Specify Any CrossValidation Options?  Method Setting  Resample Setting 

ClassificationBaggedEnsemble  No  'Bag'  'on' 
ClassificationEnsemble  No  Any ensembleaggregation method for classification  'off' 
ClassificationPartitionedEnsemble  Yes  Any classification ensembleaggregation method  'off' or 'on' 
The namevalue pair arguments
that control crossvalidation are CrossVal
, Holdout
, KFold
, Leaveout
,
and CVPartition
.
To reference properties of Mdl
, use dot notation.
For example, to access or display the cell vector of weak learner
model objects for an ensemble that has not been crossvalidated, enter Mdl.Trained
at
the command line.
NumLearningCycles
can vary from
a few dozen to a few thousand. Usually, an ensemble with good predictive
power requires from a few hundred to a few thousand weak learners.
However, you do not have to train an ensemble for that many cycles
at once. You can start by growing a few dozen learners, inspect the
ensemble performance and then, if necessary, train more weak learners
using resume
for classification problems.
Ensemble performance depends on the ensemble setting and the setting of the weak learners. That is, if you specify weak learners with default parameters, then the ensemble can perform poorly. Therefore, like ensemble settings, it is good practice to adjust the parameters of the weak learners using templates, and to choose values that minimize generalization error.
If you specify to resample using Resample
,
then it is good practice to resample to entire data set. That is,
use the default setting of 1
for FResample
.
If the ensembleaggregation method (Method
)
is 'bag'
and:
The misclassification cost (Cost
)
is highly imbalanced, then, for inbag samples, the software oversamples
unique observations from the class that has a large penalty.
The class prior probabilities (Prior
)
are highly skewed, the software oversamples unique observations from
the class that has a large prior probability.
For smaller sample sizes, these combinations can result
in a low relative frequency of outofbag observations from the class
that has a large penalty or prior probability. Consequently, the
estimated outofbag error is highly variable and it can be difficult
to interpret. To avoid large estimated outofbag error variances,
particularly for small sample sizes, set a more balanced misclassification
cost matrix using Cost
or a less skewed prior
probability vector using Prior
.
Because the order of some input and output arguments
correspond to the distinct classes in the training data, it is good
practice to specify the class order using the ClassNames
namevalue
pair argument.
To determine the class order quickly, remove all
observations from the training data that are unclassified (that is,
have a missing label), obtain and display an array of all the distinct
classes, and then specify the array for ClassNames
.
For example, suppose the response variable (Y
)
is a cell array of labels. This code specifies the class order in
the variable classNames
.
Ycat = categorical(Y); classNames = categories(Ycat)
categorical
assigns <undefined>
to
unclassified observations and categories
excludes <undefined>
from
its output. Therefore, if you use this code for cell arrays of labels
or similar code for categorical arrays, then you do not have to remove
observations with missing labels to obtain a list of the distinct
classes.To specify that the class order from lowestrepresented
label to mostrepresented, then quickly determine the class order
(as in the previous bullet), but arrange the classes in the list by
frequency before passing the list to ClassNames
.
Following from the previous example, this code specifies the class
order from lowest to mostrepresented in classNamesLH
.
Ycat = categorical(Y); classNames = categories(Ycat); freq = countcats(Ycat); [~,idx] = sort(freq); classNamesLH = classNames(idx);
After training a model, you can generate C/C++ code that predicts labels for new data. Generating C/C++ code requires MATLAB Coder™. For details, see Introduction to Code Generation.
For details of ensembleaggregation algorithms, see Ensemble Algorithms.
If you specify Method
to be a
boosting algorithm and Learners
to be decision
trees, then the software grows shallow decision trees by default.
You can adjust tree depth by specifying the MaxNumSplits
, MinLeafSize
,
and MinParentSize
namevalue pair arguments using templateTree
.
fitcensemble
generates inbag
samples by oversampling classes with large misclassification costs
and undersampling classes with small misclassification costs. Consequently,
outofbag samples have fewer observations from classes with large
misclassification costs and more observations from classes with small
misclassification costs. If you train a classification ensemble using
a small data set and a highly skewed cost matrix, then the number
of outofbag observations per class can be low. Therefore, the estimated
outofbag error can have a large variance and can be difficult to
interpret. The same phenomenon can occur for classes with large prior
probabilities.
For the RUSBoost ensembleaggregation method (Method
),
the namevalue pair argument RatioToSmallest
specifies
the sampling proportion for each class with respect to the lowestrepresented
class. For example, suppose that there are two classes in the training
data: A and B. A have
100 observations and B have 10 observations. and
that the lowestrepresented class has m
observations
in the training data.
If you set 'RatioToSmallest',2
,
then
= s
*m
2*10
= 20
.
Consequently, fitcensemble
trains every learner
using 20 observations from class A and 20 observations
from class B. If you set 'RatioToSmallest',[2
2]
, then you obtain the same result.
If you set 'RatioToSmallest',[2,1]
,
then
= s1
*m
2*10
= 20
and
= s2
*m
1*10
= 10
.
Consequently, fitcensemble
trains every learner
using 20 observations from class A and 10 observations
from class B.
For dualcore systems and above, fitcensemble
parallelizes
training using Intel^{®} Threading Building Blocks (TBB). For details
on Intel TBB, see https://software.intel.com/enus/inteltbb.
[1] Breiman, L. “Bagging Predictors.” Machine Learning. Vol. 26, pp. 123–140, 1996.
[2] Breiman, L. “Random Forests.” Machine Learning. Vol. 45, pp. 5–32, 2001.
[3] Freund, Y. “A more robust boosting algorithm.” arXiv:0905.2138v1, 2009.
[4] Freund, Y. and R. E. Schapire. “A DecisionTheoretic Generalization of OnLine Learning and an Application to Boosting.” J. of Computer and System Sciences, Vol. 55, pp. 119–139, 1997.
[5] Friedman, J. “Greedy function approximation: A gradient boosting machine.” Annals of Statistics, Vol. 29, No. 5, pp. 1189–1232, 2001.
[6] Friedman, J., T. Hastie, and R. Tibshirani. “Additive logistic regression: A statistical view of boosting.” Annals of Statistics, Vol. 28, No. 2, pp. 337–407, 2000.
[7] Hastie, T., R. Tibshirani, and J. Friedman. The Elements of Statistical Learning section edition, Springer, New York, 2008.
[8] Ho, T. K. “The random subspace method for constructing decision forests.” IEEE Transactions on Pattern Analysis and Machine Intelligence, Vol. 20, No. 8, pp. 832–844, 1998.
[9] Schapire, R. E., Y. Freund, P. Bartlett, and W.S. Lee. “Boosting the margin: A new explanation for the effectiveness of voting methods.” Annals of Statistics, Vol. 26, No. 5, pp. 1651–1686, 1998.
[10] Seiffert, C., T. Khoshgoftaar, J. Hulse, and A. Napolitano. “RUSBoost: Improving clasification performance when training data is skewed.” 19th International Conference on Pattern Recognition, pp. 1–4, 2008.
[11] Warmuth, M., J. Liao, and G. Ratsch. “Totally corrective boosting algorithms that maximize the margin.” Proc. 23rd Int’l. Conf. on Machine Learning, ACM, New York, pp. 1001–1008, 2006.
To run in parallel, set the 'UseParallel'
option to true
.
To perform parallel hyperparameter optimization, use the 'HyperparameterOptions', struct('UseParallel',true)
namevalue pair argument in the call to this function.
For more information on parallel hyperparameter optimization, see Parallel Bayesian Optimization.
ClassificationBaggedEnsemble
 ClassificationEnsemble
 ClassificationPartitionedEnsemble
 predict
 templateDiscriminant
 templateKNN
 templateTree
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
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: .
Select web siteYou can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.