margin

Classification margins

Syntax

M = margin(ens,tbl,ResponseVarName)
M = margin(ens,tbl,Y)
M = margin(ens,X,Y)
M = margin(___Name,Value)

Description

M = margin(ens,tbl,ResponseVarName) returns the classification margin for the predictions of ens on data tbl, when the true classifications are tbl.ResponseVarName.

M = margin(ens,tbl,Y) returns the classification margin for the predictions of ens on data tbl, when the true classifications are Y.

M = margin(ens,X,Y) returns the classification margin for the predictions of ens on data X, when the true classifications are Y.

M = margin(___Name,Value) calculates margin with additional options specified by one or more Name,Value pair arguments, using any of the previous syntaxes.

Input Arguments

ens

Classification ensemble created with fitcensemble, or a compact classification ensemble created with compact.

tbl

Sample data, specified as a table. Each row of tbl corresponds to one observation, and each column corresponds to one predictor variable. tbl must contain all of the predictors used to train the model. Multi-column variables and cell arrays other than cell arrays of character vectors are not allowed.

If you trained ens using sample data contained in a table, then the input data for this method must also be in a table.

ResponseVarName

Response variable name, specified as the name of a variable in tbl.

You must specify ResponseVarName as a character vector or string scalar. For example, if the response variable Y is stored as tbl.Y, then specify it as 'Y'. Otherwise, the software treats all columns of tbl, including Y, as predictors when training the model.

X

Matrix of data to classify. Each row of X represents one observation, and each column represents one predictor. X must have the same number of columns as the data used to train ens. X should have the same number of rows as the number of elements in Y.

If you trained ens using sample data contained in a matrix, then the input data for this method must also be in a matrix.

Y

Class labels of observations in tbl or X. Y should be of the same type as the classification used to train ens, and its number of elements should equal the number of rows of tbl or X.

Name-Value Pair Arguments

Specify optional comma-separated 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.

'learners'

Indices of weak learners in the ensemble ranging from 1 to ens.NumTrained. oobEdge uses only these learners for calculating loss.

Default: 1:NumTrained

'UseObsForLearner'

A logical matrix of size N-by-T, where:

  • N is the number of rows of X.

  • T is the number of weak learners in ens.

When UseObsForLearner(i,j) is true, learner j is used in predicting the class of row i of X.

Default: true(N,T)

Output Arguments

M

A numeric column vector with the same number of rows as tbl or X. Each row of M gives the classification margin for that row of tbl or X.

Examples

expand all

Find the margin for classifying an average flower from the fisheriris data as 'versicolor'.

Load the Fisher iris data set.

load fisheriris

Train an ensemble of 100 boosted classification trees using AdaBoostM2.

t = templateTree('MaxNumSplits',1); % Weak learner template tree object
ens = fitcensemble(meas,species,'Method','AdaBoostM2','Learners',t);

Classify an average flower and find the classification margin.

flower = mean(meas);
predict(ens,flower)
ans = 1x1 cell array
    {'versicolor'}

margin(ens,flower,'versicolor')
ans = 3.2140

More About

expand all

Extended Capabilities

See Also

| |