www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimatePosteriorProbabilitesForTestSamplesExample.m
%% Estimate Posterior Probabilities for Test Samples % Estimate positive class posterior probabilities for the test set of an % SVM algorithm. %% % Load the |ionosphere| data set. % Copyright 2015 The MathWorks, Inc. load ionosphere %% % Train an SVM classifier. Specify a 20% holdout sample. It is good % practice to standardize the predictors and specify the class order. rng(1) % For reproducibility CVSVMModel = fitcsvm(X,Y,'Holdout',0.2,'Standardize',true,... 'ClassNames',{'b','g'}); %% % |CVSVMModel| is a trained |ClassificationPartitionedModel| cross-validated % classifier. %% % Estimate the optimal score function for mapping observation scores to % posterior probabilities of an observation being classified as |'g'|. ScoreCVSVMModel = fitSVMPosterior(CVSVMModel); %% % |ScoreSVMModel| is a trained |ClassificationPartitionedModel| % cross-validated classifier containing the optimal score % transformation function estimated from the training data. %% % Estimate the out-of-sample positive class posterior probabilities. % Display the results for the first 10 out-of-sample observations. [~,OOSPostProbs] = kfoldPredict(ScoreCVSVMModel); indx = ~isnan(OOSPostProbs(:,2)); hoObs = find(indx); % Holdout observation numbers OOSPostProbs = [hoObs, OOSPostProbs(indx,2)]; table(OOSPostProbs(1:10,1),OOSPostProbs(1:10,2),... 'VariableNames',{'ObservationIndex','PosteriorProbability'})