Categories &

Functions List

Function Reference: fitSVMPosterior

statistics: ScoreSVMModel = fitSVMPosterior (SVMModel)

statistics: ScoreSVMModel = fitSVMPosterior (SVMModel, name, value)

statistics: ScoreSVMModel = fitSVMPosterior (SVMModel, X, Y)

statistics: ScoreSVMModel = fitSVMPosterior (SVMModel, Tbl, ResponseVarName)

statistics: ScoreSVMModel = fitSVMPosterior (SVMModel, Tbl, Y)

statistics: ScoreSVMModel = fitSVMPosterior (SVMModel, Tbl)

statistics: [ScoreSVMModel, ScoreTransform] = fitSVMPosterior (…)

Fit the transformation from SVM scores to posterior probabilities.

ScoreSVMModel = fitSVMPosterior (SVMModel) returns the two-class SVM classifier SVMModel with the transformation from the score of the second class, SVMModel.ClassNames(2), to its posterior probability stored in its ScoreTransform property, so that predict, or kfoldPredict for a cross-validated model, returns posterior probabilities, the two columns summing to 1. SVMModel is a ClassificationSVM, a CompactClassificationSVM or a ClassificationPartitionedModel cross-validating an SVM, and ScoreSVMModel is of the same class.

The transformation is fitted to scores of observations the model was not trained on:

  • For a ClassificationSVM, scores from cross-validating it, 10-fold unless one of the options below says otherwise.
  • For a ClassificationPartitionedModel, its own out-of-fold scores, from the partition it was built with; no option may be given.
  • With predictors X and their classes Y, the scores the model gives X, without cross-validation; a CompactClassificationSVM holds no training data and needs them. X may also be a table Tbl, matched to the predictors by variable name, with the classes in the variable ResponseVarName names, in Y, or in the variable the model was fitted on. Rows whose score is NaN are left out.
NameValue
'KFold'The number of folds, an integer greater than 1. The default is 10.
'Holdout'The fraction of the data held out, a scalar in the range (0,1).
'Leaveout''on' for leave-one-out cross-validation, or 'off' (default).
'CVPartition'A cvpartition object over the observations of SVMModel.

Only one of these options may be given, and only with a ClassificationSVM and no data. The transformation is one of three:

  • A sigmoid 1 / (1 + exp (A * s + B)) of the score s when the scores of the two classes overlap, with A and B fitted by maximum likelihood, as a logistic regression of the class on the score. Neither the observation weights nor the prior enter the fit, as in MATLAB.
  • A step function when the scores separate the classes: 0 below the largest score of the first class, 1 above the smallest score of the second, and SVMModel.Prior(2) between, with a warning.
  • A constant when one class has prior probability 0, posterior 1 for the other class.

A ScoreTransform already set on SVMModel is reset to 'none' first, with a warning. The property holds the transformation as '@(S)sigmoid(S,A,B)', '@(S)step(S,LowerBound,UpperBound,PositiveClassProbability)' or '@(S)constant(S,K)', K being the index of the predicted class. Each parameter is written exactly, in the fewest significant digits that read back to it, where MATLAB shows seven, so a model saved and reloaded predicts as before. The folds of a cross-validated model keep 'none', the transformation being applied to the scores they give together.

[ScoreSVMModel, ScoreTransform] = fitSVMPosterior (…) also returns the parameters in a structure whose field Type is 'sigmoid', with fields Slope and Intercept, 'step', with fields LowerBound, UpperBound and PositiveClassProbability, or 'constant', with the field PredictedClass.

Reference: J. Platt. Probabilistic outputs for support vector machines and comparisons to regularized likelihood methods. In Advances in Large Margin Classifiers, MIT Press, 2000.

See also: ClassificationSVM.fitPosterior, CompactClassificationSVM.fitPosterior, fitcsvm, cvpartition

Source Code: fitSVMPosterior

Posterior probabilities for two overlapping species

 load fisheriris
 X = meas(51:150, 3:4);
 Y = species(51:150);
 SVMModel = fitcsvm (X, Y, 'KernelFunction', 'linear');
 [ScoreSVMModel, ScoreTransform] = fitSVMPosterior (SVMModel)
ScoreSVMModel =

  ClassificationSVM

             ResponseName: 'Y'
               ClassNames: {'versicolor' 'virginica'}
           ScoreTransform: '@(S)sigmoid(S,-3.317072679537066,-0.026795496583401086)'
          NumObservations: 100
            NumPredictors: 2
                    Alpha: [24x1 double]
                     Beta: [2x1 double]
                     Bias: -14.414868
         KernelParameters: [1x1 struct]
ScoreTransform =

  scalar structure containing the fields:

    Type = sigmoid
    Slope = -3.3171
    Intercept = -0.026795
 [label, posterior] = predict (ScoreSVMModel, X([1, 2, 60, 61], :))
label =
  4x1 cell array

    {'versicolor'}    
    {'versicolor'}    
    {'virginica' }    
    {'virginica' }    

posterior =

   9.6409e-01   3.5906e-02
   9.8185e-01   1.8147e-02
   2.8520e-07   1.0000e+00
   1.6441e-02   9.8356e-01

Two species the scores separate get a step function

 load fisheriris
 SVMModel = fitcsvm (meas(1:100, 3:4), species(1:100));
 [~, ScoreTransform] = fitSVMPosterior (SVMModel)
warning: fitSVMPosterior: the classes are perfectly separated, so the transformation is a step function.
warning: called from
    fitSVMPosterior at line 227 column 5
    __eval_demo__ at line 84 column 9
    __demo_notebook__ at line 43 column 3
    __build_demos__ at line 79 column 7
    function_texi2html at line 138 column 5
    package_texi2html at line 350 column 9

ScoreTransform =

  scalar structure containing the fields:

    Type = step
    LowerBound = -0.8431
    UpperBound = 0.6897
    PositiveClassProbability = 0.5000