www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimateTheScoreTransformationFunctionForInseparableClasExample.m
%% Estimate Posterior Probabilities for New Data When Classes Are Inseparable %% % Load the |ionosphere| data set. Reserve 20 random observations of the % data, and consider this set new data. % Copyright 2015 The MathWorks, Inc. load ionosphere n = size(X,1); rng(1); % For reproducibility indx = ~ismember([1:n],randsample(n,20)); % Indices for the training data %% % The classes of this data set are inseparable. %% % Train an SVM classifier using the training data. It is % good practice to specify the class order and standardize the data. SVMModel = fitcsvm(X(indx,:),Y(indx),'ClassNames',{'b','g'},... 'Standardize',true); %% % |SVMModel| is a |ClassificationSVM| classifier. %% % Use the new data set to estimate the optimal % score-to-posterior-probability transformation function for mapping scores % to the posterior probability of an observation being classified as |g|. % For efficiency, make a compact version of the SVM classifier |SVMModel|, % and pass it and the new data to |fitPosterior|. CompactSVMModel = compact(SVMModel); [ScoreCSVMModel,ScoreParameters] = fitPosterior(CompactSVMModel,... X(~indx,:),Y(~indx)); ScoreTransform = ScoreCSVMModel.ScoreTransform ScoreParameters %% % |ScoreTransform| is the optimal score transform function. % |ScoreParameters| is a structure array having three fields: the score % transformation function name (|Type|), the sigmoid slope (|Slope|) % and sigmoid intercept (|Intercept|) estimates. % % Alternatively, you can pass |SVMModel| and the new data to % |fitSVMPosterior|, but this does not have the benefit of efficiency. %% % Estimate the posterior probabilities that the observations in the new % data are in class |g|. [labels,postProbs] = predict(ScoreCSVMModel,X(~indx,:)); table(Y(~indx),labels,postProbs(:,2),... 'VariableNames',{'TrueLabel','PredictedLabel','PosteriorProbability'})