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

    %% Create Cross-Validated Binary Linear Classification Model
%%
% Load the NLP data set.
load nlpdata
%%
% |X| is a sparse matrix of predictor data, and |Y| is a categorical vector
% of class labels.  There are more than two classes in the data.
%%
% Identify the labels that correspond to the Statistics and Machine
% Learning Toolbox(TM) documentation web pages.
Ystats = Y == 'stats';
%%
% Cross-validate a binary, linear classification model that can identify whether 
% the word counts in a documentation web page are from the Statistics and
% Machine Learning Toolbox(TM) documentation. 
rng(1); % For reproducibility 
CVMdl = fitclinear(X,Ystats,'CrossVal','on')
%%
% |CVMdl| is a |ClassificationPartitionedLinear| cross-validated model.
% Because |fitclinear| implements 10-fold cross-validation by default,
% |CVMdl.Trained| contains ten |ClassificationLinear| models that contain
% the results of training linear classification models for each of the folds.
%%
% Estimate labels for out-of-fold observations and estimate the
% generalization error by passing |CVMdl| to |kfoldPredict| and
% |kfoldLoss|, respectively.
oofLabels = kfoldPredict(CVMdl);
ge = kfoldLoss(CVMdl)
%%
% The estimated generalization error is less than 0.1% misclassified
% observations.