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

    %% Predict Class Labels Using Discriminant Analysis Model
%%
% Load Fisher's iris data set.  Determine the sample size.
load fisheriris
N = size(meas,1);
%%
% Partition the data into training and test sets.  Hold out 10% of the data
% for testing.  
rng(1); % For reproducibility
cvp = cvpartition(N,'Holdout',0.1);
idxTrn = training(cvp); % Training set indices
idxTest = test(cvp);    % Test set indices
%%
% Store the training data in a table.
tblTrn = array2table(meas(idxTrn,:));
tblTrn.Y = species(idxTrn);
%%
% Train a discriminant analysis model using the training set and default
% options.
Mdl = fitcdiscr(tblTrn,'Y');
%%
% Predict labels for the test set. You trained |Mdl| using a table of data,
% but you can predict labels using a matrix.
labels = predict(Mdl,meas(idxTest,:));
%%
% Construct a confusion matrix for the test set.
Mdl.ClassNames
confusionmat(species(idxTest),labels)
%%
% |Mdl| misclassifies one versicolor iris as virginica in the test set.