Predict labels using ensemble of classification models
labels = predict(Mdl,X)
labels = predict(Mdl,X,Name,Value)
[labels,score] = predict(___)
[ also returns a matrix of classification scores (
the likelihood that a label comes from a particular class, using any
of the input arguments in the previous syntaxes. For each observation
X, the predicted class label corresponds to
the maximum score among all classes.
Predictor data to be classified, specified as a numeric matrix or table.
Each row of
Specify optional comma-separated pairs of
Name is the argument
Value is the corresponding
Name must appear
inside single quotes (
You can specify several name and value pair
arguments in any order as
Indices of weak learners
A logical matrix of size
Vector of classification labels.
A matrix with one row per observation and one column per class.
For each observation and each class, the score generated by each tree
is the probability of this observation originating from this class
computed as the fraction of observations of this class in a tree leaf.
Load Fisher's iris data set. Determine the sample size.
load fisheriris N = size(meas,1);
Partition the data into training and test sets. Hold out 10% of the data for testing.
rng(1); % For reproducibility cvp = cvpartition(N,'Holdout',0.1); idxTrn = training(cvp); % Training set indices idxTest = test(cvp); % Test set indices
Store the training data in a table.
tblTrn = array2table(meas(idxTrn,:)); tblTrn.Y = species(idxTrn);
Boost 100 classification trees using AdaBoostM2 and the training set.
Mdl = fitensemble(tblTrn,'Y','AdaBoostM2',100,'tree');
Predict labels for the test set. You trained model using a table of data, but you can predict labels using a matrix.
labels = predict(Mdl,meas(idxTest,:));
Construct a confusion matrix for the test set.
ans = 3×1 cell array 'setosa' 'versicolor' 'virginica' ans = 8 0 0 0 2 1 0 0 4
Mdl misclassifies one versicolor iris as virginica in the test set.
For ensembles, a classification score represents the confidence of a classification into a class. The higher the score, the higher the confidence.
Different ensemble algorithms have different definitions for their scores. Furthermore, the range of scores depends on ensemble type. For example:
AdaBoostM1 scores range from –∞
Bag scores range from
This function fully supports tall arrays. For more information, see Tall Arrays (MATLAB).
Usage notes and limitations:
predict generates reference C code.
predict within a function that you
declare (that is, you cannot call
predict at the
This table contains input-and-output-argument notes and limitations.
|Argument||Notes and Limitations|
|Name-value pair arguments names|
Name-value pair argument names must be compile-time constants.
For example, to specify that predicted labels be an aggregation of
learners 1 through 5 only, include
|Returned as the same data type as |
For more code generation notes and limitations on
see Extended Capabilities.