Documentation Center

  • Trial Software
  • Product Updates


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.

See Also

| | |

Was this topic helpful?