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

    %% Train Ensemble of Bagged Classification Trees
%%
% Load Fisher's iris data set.
load fisheriris
%%
% Train an ensemble of bagged classification trees using the entire data
% set. Specify |50| weak learners.  Store which observations are out of bag
% for each tree.
rng(1); % For reproducibility
Mdl = TreeBagger(50,meas,species,'OOBPrediction','On',...
    'Method','classification')
%%
% |Mdl| is a |TreeBagger| ensemble.
%%
% |Mdl.Trees| stores a 50-by-1 cell vector of the trained classification
% trees (|CompactClassificationTree| model objects) that compose the
% ensemble.
%%
% Plot a graph of the first trained classification tree.
view(Mdl.Trees{1},'Mode','graph')
%%
% By default, |TreeBagger| grows deep trees.
%%
% |Mdl.OOBIndices| stores the out-of-bag indices as a matrix of logical
% values.
%%
% Plot the out-of-bag error over the number of grown classification trees.
figure;
oobErrorBaggedEnsemble = oobError(Mdl);
plot(oobErrorBaggedEnsemble)
xlabel 'Number of grown trees';
ylabel 'Out-of-bag classification error';
%%
% The out-of-bag error decreases with the number of grown trees.
%%
% To label out-of-bag observations, pass |Mdl| to |oobPredict|.