www.gusucode.com > stats 源码程序 matlab案例代码 > stats/CrossValidateAnSVMClassifierUsingCrossvalExample.m
%% Cross Validate an SVM Classifier Using crossval %% % Load the |ionosphere| data set. % Copyright 2015 The MathWorks, Inc. load ionosphere rng(1); % For reproducibility %% % Train an SVM classifier. It is good % practice to standardize the predictors and define the class order. SVMModel = fitcsvm(X,Y,'Standardize',true,'ClassNames',{'b','g'}); %% % |SVMModel| is a trained |ClassificationSVM| classifier. |'b'| is the % negative class and |'g'| is the positive class. %% % Cross validate the classifier using 10-fold cross validation. CVSVMModel = crossval(SVMModel) FirstModel = CVSVMModel.Trained{1} %% % |CVSVMModel| is a |ClassificationPartitionedModel| cross-validated % classifier. The software: % % # Randomly partitions the data into 10, equally sized sets. % # Trains an SVM classifier on nine of the sets. % # Repeats steps 1 and 2 _k_ = 10 times. It leaves out one of the % partitions each time, and trains on the other nine partitions. % # Combines generalization statistics for each fold. % % |FirstModel| is the first of the 10 trained classifiers. It is a % |CompactClassificationSVM| classifier. %% % You can estimate the generalization error by passing |CVSVMModel| to % |kfoldLoss|.