fitcensemble
statistics: Mdl = fitcensemble (X, Y)
statistics: Mdl = fitcensemble (…, name, value)
Fit an ensemble of decision trees for classification.
Mdl = fitcensemble (X, Y) grows 100 boosted
decision trees on the NxP predictor matrix X and the class
labels Y, LogitBoost for two classes and AdaBoostM2 for more, and
returns a ClassificationEnsemble. With 'Method' set to
'Bag', or with a boosting method that resamples, it returns a
ClassificationBaggedEnsemble.
Y holds a class label per row of X, as a numeric or logical vector, a categorical, string or character array, or a cell array of character vectors. A row missing a predictor or a class is left out.
The boosting methods, y being +1 for the first class and -1 for the
second, d the observation weights, which start at W times the
total cost of misclassifying each observation’s class, and eta the
learning rate:
'AdaBoostM1'eta * log ((1 - e) / e) / 2. The weights are
then multiplied by exp (-weight * y .* h).'AdaBoostM2''RUSBoost'round (r * m) rows,
m being the size of the smallest class and r the class’s
element of 'RatioToSmallest', drawn in proportion to d and
with replacement only when the class holds fewer rows. Its pseudo-loss
e over all the observations, the weight of each spread evenly over
the classes other than its own, gives its weight as for AdaBoostM1, the
weights are then multiplied by the mean over those classes of
exp (-weight * (1 + h_true - h_k)), and the scores are as for
AdaBoostM2. A perfect tree is kept as for AdaBoostM2.'LPBoost''MarginPrecision' above it, the new tree is not kept and the fit
stops. Otherwise the program gives the learner weights that maximise the
smallest margin, and as its dual the next weights d. The scores are
as for TotalBoost. Where the program has several solutions, MATLAB and
this package may choose different ones: the learner weights, and the
weights d with the trees grown on them, then differ, and so may the
number of trees kept. MATLAB R2024a keeps a tree only when that gap is also
above 0.01, so a 'MarginPrecision' below 0.01 acts there as 0.01;
here it is taken as given.'TotalBoost''MarginPrecision'. Otherwise d takes one quadratic step
towards the least relative entropy to the starting weights, every edge held
at most the smallest edge less 'MarginPrecision', and the learner
weights are those that maximise the smallest margin, a linear program solved
by GLPK. A tree scores each class with twice its probability less one.
Where several learner weights maximise that margin equally, MATLAB and this
package may choose different ones, and the scores then differ.'GentleBoost'exp (-eta * y .* h).'LogitBoost'(y01 - p) ./ (p .* (1 - p)) with the
weights d .* p .* (1 - p); its prediction times eta / 2 is
added to the score f, and p = 1 ./ (1 + exp (-f)). The 'Subspace' method fits each learner, a nearest neighbour or
discriminant classifier, on NPredToSample predictors drawn at random
without replacement, or on every combination of that many with
'NumLearningCycles' set to 'AllPredictorCombinations', and
scores each observation with the plain average of the learners’ class
probabilities. UsePredForLearner records the predictors of each
learner; every combination is taken in the order of nchoosek, which
MATLAB R2024a reverses for some subset sizes, changing the order of the
learners but not the scores. MATLAB takes observation weights here and
passes them to the learners; the learners in this package take none, so
weights that are not uniform are refused.
A boosting method resamples when 'Resample' is 'on' or
'FResample' or 'Replace' is given; RUSBoost and Subspace
cannot. Each learner is then grown on ceil (FResample * N) rows,
drawn with replacement in proportion to d, each draw weighing the
same, or without replacement uniformly, each row keeping its weight in
d. Its error is taken on those rows with those weights, and only
they are reweighted, rescaled to the weight they carried over their draws.
AdaBoostM2 then keeps one weight per observation, spread evenly over the
classes other than its own, as RUSBoost does, and LogitBoost advances the
score of the rows drawn only. The ensemble is a
ClassificationBaggedEnsemble, which records the rows each learner
drew and estimates the out-of-bag error. These rules reproduce MATLAB
R2024a’s fits on its own draws, except LogitBoost with replacement, whose
reweighting in MATLAB was not identified; here it follows the others.
A boosting method whose tree classifies the data without error, zero error
for AdaBoostM1 or zero pseudo-loss for AdaBoostM2, keeps that tree with the
weight an error of eps gives and stops; one whose error is greater
than 0.5 is not kept and stops. MATLAB discards a perfect tree, so a fit
whose first tree is perfect is empty there. GentleBoost applies the
learning rate to the fit itself, as the other boosting methods do; MATLAB
scales only the learner weights, growing the same trees whatever the
rate.
Name-Value arguments:
| Name | Value | |
|---|---|---|
'Method' | 'AdaBoostM1',
'AdaBoostM2', 'RUSBoost', 'GentleBoost',
'LogitBoost', 'LPBoost', 'TotalBoost',
'Bag' or 'Subspace'. The default is
'LogitBoost' for two classes and 'AdaBoostM2' for more. | |
'NumLearningCycles' | A positive integer, the number
of learners to grow, or for Subspace 'AllPredictorCombinations'.
The default is 100. | |
'NPredToSample' | A positive integer less than the number of predictors, the predictors each Subspace learner is fitted on. The default is 1. Subspace only. | |
'Learners' | For Subspace 'knn' (default),
'discriminant', or a template from templateKNN or
templateDiscriminant. Otherwise 'tree' (default) or a
template from templateTree, whose options override the defaults: for
boosting MaxNumSplits 10, MinParentSize 2 and
MinLeafSize 1, the regression trees of GentleBoost and LogitBoost
taking MinParentSize 10; for Bag unlimited splits,
MinParentSize 2, MinLeafSize 1 and
NumVariablesToSample ceil (sqrt (P)). | |
'LearnRate' | A number greater than 0 and no greater than 1. The default is 1. Boosting only. | |
'RatioToSmallest' | A nonnegative number, or a vector with one per class, the size of each class’s sample relative to the smallest class. The default is 1 for every class. RUSBoost only. | |
'MarginPrecision' | A number from 0 to 1, how far above the linear program LPBoost needs the smallest edge to stay, and how far below the smallest edge TotalBoost holds every edge. The default is 0.01. LPBoost and TotalBoost only. | |
'CategoricalPredictors' | The predictors whose values
are levels, as indices, as a logical vector with one element per
predictor, or as 'all'. Every tree splits them into sets of
levels, as fitctree does. Not with 'Subspace'. | |
'FResample' | The share of the observations each learner draws, greater than 0 and no greater than 1. The default is 1. Given with a boosting method, the ensemble resamples. | |
'Replace' | 'on' (default) or 'off',
whether the learners draw with replacement. Given with a boosting method,
the ensemble resamples. | |
'Resample' | 'off' (default) or
'on', whether a boosting method resamples. Bag always does. | |
'NPrint' | 'off' (default) or a positive
integer n, to print a line after every n trees. | |
'ClassNames' | The classes to fit, in the order their scores are laid out; by default they are sorted. | |
'Cost' | A KxK matrix of misclassification
costs. The default is 1 - eye (K). | |
'Prior' | 'empirical' (default),
'uniform', or a vector with one element per class. | |
'Weights' | A nonnegative vector with one weight per observation. The default is uniform. | |
'PredictorNames' | A cell array of character vectors naming the columns of X. | |
'ResponseName' | The name of the response variable. | |
'ScoreTransform' | A transform applied to the
returned scores. The default is 'none'. |
Source Code: fitcensemble
'CrossVal' set to 'on', 'KFold',
'Holdout', 'Leaveout' or 'CVPartition', only one of
them, fits the ensemble and cross-validates it as crossval does,
returning a ClassificationPartitionedEnsemble.
The method 'RobustBoost', binning and hyperparameter optimization
are not implemented, and an option asking for one of them is refused.
'CategoricalPredictors' cannot be used with 'Subspace', its
nearest neighbour and discriminant learners taking no categorical
predictors here; MATLAB passes them to its nearest neighbour learners and
drops them, with a warning, from its discriminant ones.
See also: ClassificationEnsemble, ClassificationBaggedEnsemble, CompactClassificationEnsemble, templateTree, TreeBagger
Source Code: fitcensemble
Boost decision stumps to tell versicolor from virginica, and see how the training error falls as the stumps accumulate.
load fisheriris
X = meas(51:150,:);
Y = species(51:150);
Mdl = fitcensemble (X, Y, 'Method', 'AdaBoostM1', ...
'NumLearningCycles', 20, ...
'Learners', templateTree ('MaxNumSplits', 1));
err = loss (Mdl, X, Y, 'Mode', 'cumulative');
plot (err);
xlabel ('Number of stumps');
ylabel ('Training error');
A bagged ensemble of trees classifies all three species, and scores each flower with the average of its trees' class probabilities.
load fisheriris
rng (42);
Mdl = fitcensemble (meas, species, 'Method', 'Bag', ...
'NumLearningCycles', 30);
[label, scores] = predict (Mdl, [5.0, 3.4, 1.5, 0.2; 6.7, 3.0, 5.2, 2.3])
label =
2x1 cell array
{'setosa' }
{'virginica'}
scores =
1 0 0
0 0 1