Class: ClassificationTree

Classification error by cross validation


E = cvloss(tree)
[E,SE] = cvloss(tree)
[E,SE,Nleaf] = cvloss(tree)
[E,SE,Nleaf,BestLevel] = cvloss(tree)
[E,...] = cvloss(tree,Name,Value)


E = cvloss(tree) returns the cross-validated classification error (loss) for tree, a classification tree.

[E,SE] = cvloss(tree) returns the standard error of E.

[E,SE,Nleaf] = cvloss(tree) returns the number of leaves of tree.

[E,SE,Nleaf,BestLevel] = cvloss(tree) returns the optimal pruning level for tree.

[E,...] = cvloss(tree,Name,Value) cross validates with additional options specified by one or more Name,Value pair arguments. You can specify several name-value pair arguments in any order as Name1,Value1,…,NameN,ValueN.

Input Arguments


A classification tree produced by fitctree.

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 single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.


Vector of pruning levels, with 0 representing the full, unpruned tree. tree must include a pruning sequence as created either by fitctree with 'Prune' set to 'on', or by the prune method. The returned E, SE, and Nleaf are vectors of the same length as Subtrees; BestLevel is a scalar. If you set Subtrees to 'all', cvloss uses the entire pruning sequence.

Default: 0


One of the following strings:

  • 'se'cvloss uses the smallest tree whose cost is within one standard error of the minimum cost.

  • 'min'cvloss uses the minimal cost tree.

Default: 'se'


Number of cross-validation samples, a positive integer.

Default: 10

Output Arguments


The cross-validation classification error (loss). A vector or scalar depending on the setting of the Subtrees name-value pair.


The standard error of E. A vector or scalar depending on the setting of the Subtrees name-value pair.


Number of leaf nodes in tree. Leaf nodes are terminal nodes, which give classifications, not splits. A vector or scalar depending on the setting of the Subtrees name-value pair.


By default, a scalar representing the largest pruning level that achieves a value of E within SE of the minimum error. If you set TreeSize to 'min', BestLevel is the smallest value in Subtrees.


expand all

Compute the Cross-Validation Error

Compute the cross-validation error for the default classification tree for the ionosphere data.

load ionosphere
tree = fitctree(X,Y);
[E,SE,Nleaf,BestLevel] = cvloss(tree)
E =

SE =

Nleaf =

BestLevel =

Find the best level by using the Subtrees name-value pair.

[~,~,~,BestLevel] = cvloss(tree,'Subtrees','all')
BestLevel =


You can construct a cross-validated tree model with crossval, and call kfoldLoss instead of cvloss. The alternative can save time if you are going to examine the cross-validated tree more than once.

However, unlike cvloss, kfoldLoss does not return SE,Nleaf, or BestLevel. kfoldLoss also does not allow you to examine any error other than classification error.

Was this topic helpful?