www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimatekFoldCrossValidationClassificationErrorExample.m
%% Estimate _k_-Fold Cross-Validation Classification Error %% % 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 average of the out-of-fold, classification error rates. ce = kfoldLoss(CVMdl) %% % Alternatively, you can obtain the per-fold classification error rates by % specifying the name-value pair |'Mode','individual'| in |kfoldLoss|.