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

    %% Predict Labels Using a Classification Tree
% Examine predictions for a few rows in a data set left out of training.
%%
% Load Fisher's iris data set.

% Copyright 2015 The MathWorks, Inc.

load fisheriris
%%
% Partition the data into training (50%) and validation (50%) sets.
n = size(meas,1);
rng(1) % For reproducibility
idxTrn = false(n,1);
idxTrn(randsample(n,round(0.5*n))) = true; % Training set logical indices
idxVal = idxTrn == false;                  % Validation set logical indices
%%
% Grow a classification tree using the training set.
Mdl = fitctree(meas(idxTrn,:),species(idxTrn));
%%
% Predict labels for the validation data.  Count the number of
% misclassified observations.
label = predict(Mdl,meas(idxVal,:));
label(randsample(numel(label),5)) % Display several predicted labels
numMisclass = sum(~strcmp(label,species(idxVal)))
%%
% The software misclassifies three out-of-sample observations.