Main Content

margin

Classification margins for classification tree model

Description

m = margin(tree,Tbl,ResponseVarName) returns the classification margins m for the trained classification tree model tree using the predictor data in table Tbl and the true class labels in Tbl.ResponseVarName.

The classification margin is the difference between the classification score for the true class and the maximal classification score for the false classes. m is a column vector with the same number of rows as in Tbl.

m = margin(tree,Tbl,Y)uses the predictor data in table Tbl and the true class labels in Y.

example

m = margin(tree,X,Y) uses the predictor data in matrix X and the true class labels in Y.

Examples

collapse all

Compute the classification margin for the Fisher iris data, trained on its first two columns of data, and view the last 10 entries.

load fisheriris
X = meas(:,1:2);
tree = fitctree(X,species);
M = margin(tree,X,species);
M(end-10:end)
ans =
    0.1111
    0.1111
    0.1111
   -0.2857
    0.6364
    0.6364
    0.1111
    0.7500
    1.0000
    0.6364
    0.2000

The classification tree trained on all the data is better.

tree = fitctree(meas,species);
M = margin(tree,meas,species);
M(end-10:end)
ans =
    0.9565
    0.9565
    0.9565
    0.9565
    0.9565
    0.9565
    0.9565
    0.9565
    0.9565
    0.9565
    0.9565

Input Arguments

collapse all

Trained classification tree, specified as a ClassificationTree model object trained with fitctree, or a CompactClassificationTree model object created with compact.

Sample data, specified as a table. Each row of Tbl corresponds to one observation, and each column corresponds to one predictor variable. Optionally, Tbl can contain an additional column for the response variable. Tbl must contain all the predictors used to train tree. Multicolumn variables and cell arrays other than cell arrays of character vectors are not allowed.

If Tbl contains the response variable used to train tree, then you do not need to specify ResponseVarName or Y.

If you train tree using sample data contained in a table, then the input data for margin must also be in a table.

Data Types: table

Response variable name, specified as the name of a variable in Tbl. If Tbl contains the response variable used to train tree, then you do not need to specify ResponseVarName.

You must specify ResponseVarName as a character vector or string scalar. For example, if the response variable is stored as Tbl.Response, then specify it as "Response". Otherwise, the software treats all columns of Tbl, including Tbl.Response, as predictors.

The response variable must be a categorical, character, or string array, a logical or numeric vector, or a cell array of character vectors. If the response variable is a character array, then each element must correspond to one row of the array.

Data Types: char | string

Predictor data, specified as a numeric matrix. Each column of X represents one variable, and each row represents one observation.

X must have the same number of columns as the data used to train tree. X must have the same number of rows as the number of rows in Y.

Data Types: single | double

Class labels, specified as a categorical, character, or string array, a logical or numeric vector, or a cell array of character vectors. Y must be of the same type as the class labels used to train tree, and its number of elements must equal the number of rows of X.

Data Types: categorical | char | string | logical | single | double | cell

More About

collapse all

Margin

The classification margin is the difference between the classification score for the true class and maximal classification score for the false classes. Margin is a column vector with the same number of rows as in the matrix X.

Score (tree)

For trees, the score of a classification of a leaf node is the posterior probability of the classification at that node. The posterior probability of the classification at a node is the number of training sequences that lead to that node with the classification, divided by the number of training sequences that lead to that node.

For an example, see Posterior Probability Definition for Classification Tree.

Extended Capabilities

Version History

Introduced in R2011a