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

    %% Estimate _k_-Fold Cross-Validation Margins
%%
% 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.
%%
% The models should identify whether the word counts in a web page are
% from the Statistics and Machine Learning Toolbox(TM) documentation. So,
% 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| model. By default, the
% software implements 10-fold cross validation.  You can alter the number
% of folds using the |'KFold'| name-value pair argument.
%%
% Estimate the cross-validated margins.
m = kfoldMargin(CVMdl);
size(m)
%%
% |m| is a 31572-by-1 vector.  |m(j)| is the average of the out-of-fold
% margins for observation |j|.
%%
% Plot the _k_-fold margins using box plots.
figure;
boxplot(m);
h = gca;
h.YLim = [-5 30];
title('Distribution of Cross-Validated Margins')