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

    %% Predict Class Labels Using Linear Classification Model
%%
% Load the NLP data set.
load nlpdata
n = size(X,1); % Number of observations
%%
% Identify the labels that correspond to the Statistics and Machine
% Learning Toolbox(TM) documentation web pages.
Ystats = Y == 'stats';
%%
% Hold out 5% of the data.
rng(1); % For reproducibility
cvp = cvpartition(n,'Holdout',0.05)
%%
% |cvp| is a |CVPartition| object that defines the random partition of _n_ 
% data into training and test sets.
%%
% Train a binary, linear classification model using the training set that
% can identify whether the word counts in a documentation web page are
% from the Statistics and Machine Learning Toolbox(TM) documentation. For
% faster training time, orient the predictor data matrix so that the
% observations are in columns.
idxTrain = training(cvp); % Extract training set indices
X = X';
Mdl = fitclinear(X(:,idxTrain),Ystats(idxTrain),'ObservationsIn','columns');
%%
% Predict observations and classification error for the hold out sample.  
idxTest = test(cvp); % Extract test set indices
labels = predict(Mdl,X(:,idxTest),'ObservationsIn','columns');
L = loss(Mdl,X(:,idxTest),Ystats(idxTest),'ObservationsIn','columns')
%%
% |Mdl| misclassifies fewer than 1% of the out-of-sample observations.