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

    %% Find the Pruning Level Yielding the Optimal In-sample Loss
%%
% Load the |carsmall| data set.  Consider |Displacement|,
% |Horsepower|, and |Weight| as predictors of the response |MPG|.

% Copyright 2015 The MathWorks, Inc.

load carsmall
X = [Displacement Horsepower Weight];
%%
% Grow a regression tree using all observations.
Mdl = fitrtree(X,MPG);
%%
% View the regression tree.
view(Mdl,'Mode','graph');
%%
% Find the best pruning level that yields the optimal in-sample loss.
[L,se,NLeaf,bestLevel] = loss(Mdl,X,MPG,'Subtrees','all');
bestLevel
%%
% The best pruning level is level 1.
%%
% Prune the tree to level 1.
pruneMdl = prune(Mdl,'Level',bestLevel);
view(pruneMdl,'Mode','graph');