www.gusucode.com > stats 源码程序 matlab案例代码 > stats/CrossValidateRegressionTreeExample.m

    %% Cross-Validate Regression Tree
%%
% Load the |carsmall| data set.  Consider |Acceleration|, |Displacement|,
% |Horsepower|, and |Weight| as predictor variables.
load carsmall
X = [Acceleration Displacement Horsepower Weight];
%%
% Grow a regression tree using the entire data set.
Mdl = fitrtree(X,MPG);
%%
% |Mdl| is a |RegressionTree| model.
%%
% Cross-validate the regression tree using 10-fold cross-validation.
CVMdl = crossval(Mdl);
%%
% |CVMdl| is a |RegressionPartitionedModel| cross-validated model.
% |crossval| stores the ten trained, compact regression trees in the
% |Trained| property of |CVMdl|.
%%
% Display the compact regression tree that |crossval| trained using all
% observations except those in the first fold.
CVMdl.Trained{1}
%%
% Estimate the generalization error of |Mdl| by computing the 10-fold
% cross-validated mean-squared error.
L = kfoldLoss(CVMdl)