Main Content


Cross-validation loss of partitioned regression ensemble


L = kfoldLoss(cvens)
L = kfoldLoss(cvens,Name,Value)


L = kfoldLoss(cvens) returns the cross-validation loss of cvens.

L = kfoldLoss(cvens,Name,Value) returns cross-validation loss 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


Object of class RegressionPartitionedEnsemble. Create obj with fitrensemble along with one of the cross-validation options: 'crossval', 'kfold', 'holdout', 'leaveout', or 'cvpartition'. Alternatively, create obj from a regression ensemble with crossval.

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.


Indices of folds ranging from 1 to cvens.KFold. Use only these folds for predictions.

Default: 1:cvens.KFold


Function handle for loss function, or 'mse', meaning mean squared error. If you pass a function handle fun, loss calls it as


where Y, Yfit, and W are numeric vectors of the same length.

  • Y is the observed response.

  • Yfit is the predicted response.

  • W is the observation weights.

The returned value fun(Y,Yfit,W) should be a scalar.

Default: 'mse'


Method for computing cross-validation loss.

  • 'average'L is a scalar value, the average loss over all folds.

  • 'individual'L is a vector with one element per fold.

  • 'cumulative'L is a vector with a length of minimum number of observations used for training in each fold. Each element in the vector L is obtained by taking the average of loss across all folds.

Default: 'average'

Output Arguments


The loss (mean squared error) between the observations in a fold when compared against predictions made with an ensemble trained on the out-of-fold data. L can be a vector, and can mean different things, depending on the name-value pair settings.


expand all

Find the cross-validation loss for a regression ensemble of the carsmall data.

Load the carsmall data set and select displacement, horsepower, and vehicle weight as predictors.

load carsmall
X = [Displacement Horsepower Weight];

Train an ensemble of regression trees.

rens = fitrensemble(X,MPG);

Create a cross-validated ensemble from rens and find the k-fold cross-validation loss.

rng(10,'twister') % For reproducibility
cvrens = crossval(rens);
L = kfoldLoss(cvrens)
L = 28.7114