www.gusucode.com > stats 源码程序 matlab案例代码 > stats/EstimateTheScoreTransformationFunctionForSeparableClasseExample.m
%% Estimate Posterior Probabilities for New Data When Classes Are Separable %% % Load Fisher's iris data set. Use the petal lengths and widths, and remove % the virginica species from the data. Reserve 10 random observations of % the data, and consider this set new data. % Copyright 2015 The MathWorks, Inc. load fisheriris classKeep = ~strcmp(species,'virginica'); X = meas(classKeep,3:4); Y = species(classKeep); rng(1); % For reproducibility indx1 = 1:numel(species); indx2 = indx1(classKeep); indx = ~ismember(indx2,randsample(indx2,10)); % Indices for the training data gscatter(X(indx,1),X(indx,2),Y(indx)); title('Scatter Diagram of Iris Measurements') xlabel('Petal length') ylabel('Petal width') legend('Setosa','Versicolor') %% % The classes are perfectly separable. Therefore, the % score-to-posterior-probability transformation function is a step % function. %% % Train an SVM classifier. It is good practice to specify the class order % and standardize the data. SVMModel = fitcsvm(X(indx,:),Y(indx),... 'ClassNames',{'setosa','versicolor'},'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 % |versicolor|. 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 %% % |fitPosterior| displays a warning whenever the classes are separable, and % stores the step function in |ScoreSVMModel.ScoreTransform|. % %% % Display the score function type and its estimated values. ScoreParameters %% % |ScoreParameters| is a structure array having four fields: % % * The score transformation function type (|Type|) % * The score corresponding to negative class boundary (|LowerBound|) % * The score corresponding to positive class boundary (|UpperBound|) % * The positive class probability (|PositiveClassProbability|) % %% % 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 versicolor irises. [labels,postProbs] = predict(ScoreCSVMModel,X(~indx,:)); table(Y(~indx),labels,postProbs(:,2),... 'VariableNames',{'TrueLabel','PredictedLabel','PosteriorProbability'}) %% % Since the classes are separable, the step function transforms the % positive-class score to: % % * |0|, if the score is less than |ScoreParameters.LowerBound| % * |1|, if the score is greater than |ScoreParameters.UpperBound| % * |ScoreParameters.PositiveClassProbability|, if the score is in the % interval [ |ScoreParameters.LowerBound| , |ScoreParameters.LowerBound|] %