Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

predict

Class: CompactClassificationSVM

Predict labels using support vector machine classification model

Syntax

  • label = predict(SVMModel,X)
    example
  • [label,score] = predict(SVMModel,X)
    example

Description

example

label = predict(SVMModel,X) returns a vector of predicted class labels for the predictor data in the table or matrix X, based on the full or compact, trained SVM classification model SVMModel.

example

[label,score] = predict(SVMModel,X) also returns a matrix of scores (score), indicating the likelihood that a label comes from a particular class. For SVM, likelihood measures are either classification scores or class posterior probabilities. For each observation in X, the predicted class label corresponds to the maximum score among all classes.

Code Generation support: Yes.

MATLAB Function Block support: Yes.

Input Arguments

expand all

SVM classification model, specified as a ClassificationSVM model object or CompactClassificationSVM model object returned by fitcsvm or compact, respectively.

Predictor data to be classified, specified as a numeric matrix or table.

Each row of X corresponds to one observation, and each column corresponds to one variable.

  • For a numeric matrix:

    • The variables making up the columns of X must have the same order as the predictor variables that trained SVMModel.

    • If you trained SVMModel using a table (for example, Tbl), then X can be a numeric matrix if Tbl contains all numeric predictor variables. To treat numeric predictors in Tbl as categorical during training, identify categorical predictors using the CategoricalPredictors name-value pair argument of fitcsvm. If Tbl contains heterogeneous predictor variables (for example, numeric and categorical data types) and X is a numeric matrix, then predict throws an error.

  • For a table:

    • predict does not support multi-column variables and cell arrays other than cell arrays of character vectors.

    • If you trained SVMModel using a table (for example, Tbl), then all predictor variables in X must have the same variable names and data types as those that trained SVMModel (stored in SVMModel.PredictorNames). However, the column order of X does not need to correspond to the column order of Tbl. Tbl and X can contain additional variables (response variables, observation weights, etc.), but predict ignores them.

    • If you trained SVMModel using a numeric matrix, then the predictor names in SVMModel.PredictorNames and corresponding predictor variable names in X must be the same. To specify predictor names during training, see the PredictorNames name-value pair argument of fitcsvm. All predictor variables in X must be numeric vectors. X can contain additional variables (response variables, observation weights, etc.), but predict ignores them.

If you set 'Standardize',true in fitcsvm to train SVMModel, then the software standardizes the columns of X using the corresponding means in SVMModel.Mu and standard deviations in SVMModel.Sigma.

Data Types: table | double | single

Output Arguments

expand all

Predicted class labels, returned as a categorical or character array, logical or numeric vector, or cell array of character vectors.

label:

  • Is the same data type as the observed class labels (Y) that trained SVMModel

  • Has length equal to the number of rows of X

For one-class learning, the elements of label are the one class represented in the observed class labels.

Predicted class scores or posterior probabilities, returned as a numeric column vector or numeric matrix.

  • For one-class learning, score is a column vector with the same number of rows as the training observations (X). The elements are the positive class scores for the corresponding observations. You cannot obtain posterior probabilities for one-class learning.

  • For two-class learning, score is a two-column matrix with the same number of rows as X.

    • If you fit the optimal score-to-posterior probability transformation function using fitPosterior or fitSVMPosterior, then score contains class posterior probabilities. That is, if the value of SVMModel.ScoreTransform is not none, then the elements of the first and second columns of score are the negative class (SVMModel.ClassNames{1}) and positive class (SVMModel.ClassNames{2}) posterior probabilities for the corresponding observations, respectively.

    • Otherwise, the elements of the first column are the negative class scores and the elements of the second column are the positive class scores for the corresponding observations.

    If SVMModel.KernelParameters.Function is 'linear', then the software estimates the classification score for the observation x using

    f(x)=(x/s)β+b.

    SVMModel stores β, b, s in the properties Beta, Bias, and KernelParameters.Scale, respectively.

Definitions

Classification Score

The SVM classification score for classifying observation x is the signed distance from x to the decision boundary ranging from -∞ to +∞. A positive score for a class indicates that x is predicted to be in that class, a negative score indicates otherwise.

The score for predicting x into the positive class, also the numerical, predicted response for x, f(x), is the trained SVM classification function

f(x)=j=1nαjyjG(xj,x)+b,

where (α1,...,αn,b) are the estimated SVM parameters, G(xj,x) is the dot product in the predictor space between x and the support vectors, and the sum includes the training set observations. The score for predicting x into the negative class is –f(x).

If G(xj,x) = xjx (the linear kernel), then the score function reduces to

f(x)=(x/s)β+b.

s is the kernel scale and β is the vector of fitted linear coefficients.

Posterior Probability

The probability that an observation belongs in a particular class, given the data.

For SVM, the posterior probability is a function of the score, P(s), that observation j is in class k = {-1,1}.

  • For separable classes, the posterior probability is the step function

    P(sj)={0;s<maxyk=1skπ;maxyk=1sksjminyk=+1sk1;sj>minyk=+1sk,

    where:

    • sj is the score of observation j.

    • +1 and –1 denote the positive and negative classes, respectively.

    • π is the prior probability that an observation is in the positive class.

  • For inseparable classes, the posterior probability is the sigmoid function

    P(sj)=11+exp(Asj+B),

    where the parameters A and B are the slope and intercept parameters.

Prior Probability

The prior probability is the believed relative frequency that observations from a class occur in the population for each class.

Examples

expand all

Load the ionosphere data set.

load ionosphere
rng(1); % For reproducibility

Train an SVM classifier. Specify a 15% holdout sample for testing. It is good practice to specify the class order and standardize the data.

CVSVMModel = fitcsvm(X,Y,'Holdout',0.15,'ClassNames',{'b','g'},...
    'Standardize',true);
CompactSVMModel = CVSVMModel.Trained{1}; % Extract trained, compact classifier
testInds = test(CVSVMModel.Partition);   % Extract the test indices
XTest = X(testInds,:);
YTest = Y(testInds,:);

CVSVMModel is a ClassificationPartitionedModel classifier. It contains the property Trained, which is a 1-by-1 cell array holding a CompactClassificationSVM classifier that the software trained using the training set.

Label the test sample observations. Display the results for the first 10 observations in the test sample.

[label,score] = predict(CompactSVMModel,XTest);
table(YTest(1:10),label(1:10),score(1:10,2),'VariableNames',...
    {'TrueLabel','PredictedLabel','Score'})
ans = 

    TrueLabel    PredictedLabel     Score  
    _________    ______________    ________

    'b'          'b'                -1.7178
    'g'          'g'                 2.0003
    'b'          'b'                -9.6847
    'g'          'g'                 2.5619
    'b'          'b'                -1.5481
    'g'          'g'                 2.0984
    'b'          'b'                -2.7017
    'b'          'b'               -0.66307
    'g'          'g'                 1.6047
    'g'          'g'                 1.7731

A goal of classification is to predict labels of new observations using a trained algorithm. Many applications train algorithms on large data sets, which can use resources that are better used elsewhere. This example shows how to efficiently label new observations using an SVM classifier.

Load the ionosphere data set. Suppose that the last 10 observations become available after training the SVM classifier.

load ionosphere

n = size(X,1);       % Training sample size
isInds = 1:(n-10);   % In-sample indices
oosInds = (n-9):n;   % Out-of-sample indices

Train an SVM classifier. It is good practice to standardize the predictors and specify the order of the classes. Conserve memory by reducing the size of the trained SVM classifier.

SVMModel = fitcsvm(X(isInds,:),Y(isInds),'Standardize',true,...
    'ClassNames',{'b','g'});
CompactSVMModel = compact(SVMModel);
whos('SVMModel','CompactSVMModel')
  Name                 Size             Bytes  Class                                                 Attributes

  CompactSVMModel      1x1              29936  classreg.learning.classif.CompactClassificationSVM              
  SVMModel             1x1             137268  ClassificationSVM                                               

The positive class is 'g'. The CompactClassificationSVM classifier (CompactSVMModel) uses less space than the ClassificationSVM classifier (SVMModel) because the latter stores the data.

Estimate the optimal score-to-posterior-probability-transformation function.

CompactSVMModel = fitPosterior(CompactSVMModel,...
    X(isInds,:),Y(isInds))
CompactSVMModel = 

  classreg.learning.classif.CompactClassificationSVM
             ResponseName: 'Y'
    CategoricalPredictors: []
               ClassNames: {'b'  'g'}
           ScoreTransform: '@(S)sigmoid(S,-1.968351e+00,3.122242e-01)'
                    Alpha: [88×1 double]
                     Bias: -0.2142
         KernelParameters: [1×1 struct]
                       Mu: [1×34 double]
                    Sigma: [1×34 double]
           SupportVectors: [88×34 double]
      SupportVectorLabels: [88×1 double]


The optimal score transformation function (CompactSVMModel.ScoreTransform) is the sigmoid function because the classes are inseparable.

Predict the out-of-sample labels and positive class posterior probabilities. Since true labels are available, compare them with the predicted labels.

[labels,PostProbs] = predict(CompactSVMModel,X(oosInds,:));
table(Y(oosInds),labels,PostProbs(:,2),'VariableNames',...
    {'TrueLabels','PredictedLabels','PosClassPosterior'})
ans = 

    TrueLabels    PredictedLabels    PosClassPosterior
    __________    _______________    _________________

    'g'           'g'                0.98419          
    'g'           'g'                0.95545          
    'g'           'g'                0.67792          
    'g'           'g'                0.94447          
    'g'           'g'                0.98744          
    'g'           'g'                 0.9248          
    'g'           'g'                 0.9711          
    'g'           'g'                0.96986          
    'g'           'g'                0.97803          
    'g'           'g'                0.94361          

PostProbs is a 10-by-2 matrix; its first column is the negative class posterior probabilities, and second column is the positive class posterior probabilities corresponding to the new observations.

Related Examples

Algorithms

  • By default, the software computes optimal posterior probabilities using Platt's method [1]:

    1. Performing 10-fold cross validation

    2. Fitting the sigmoid function parameters to the scores returned from the cross validation

    3. Estimating the posterior probabilities by entering the cross-validation scores into the fitted sigmoid function

  • The software incorporates prior probabilities in the SVM objective function during training.

  • For SVM, predict classifies observations into the class yielding the largest score (i.e., the largest posterior probability). The software accounts for misclassification costs by applying the average-cost correction before training the classifier. That is, given the class prior vector P, misclassification cost matrix C, and observation weight vector w, the software defines a new vector of observation weights (W) such that

    Wj=wjPjk=1KCjk.

Additional Capabilities

Code Generation

predict generates reference C code. Notes and limitations for code generation include:

  • You must call predict within a function that you declare (that is, you cannot call predict at the top-level).

  • This table contains input-and-output-argument notes and limitations.

    ArgumentNotes and Limitations
    SVMModel
    • You must load the model using loadCompactModel within a function that you declare.

    • Must be a compile-time constant, that is, its value cannot change while codegen generates the code.

    X
    • Must be a single- or double-precision matrix and can be variable sized. However, the number of columns in X must be numel(Mdl.PredictorNames).

    • Rows and columns must correspond to observations and predictors, respectively.

    scoreReturned as the same data type as X, that is, a single- or double-precision matrix

For code generation notes and limitations on Mdl, see Code Generation Support, Usage Notes, and Limitations.

MATLAB Function Block

You can use this function in the MATLAB® Function Block in Simulink®.

References

[1] Platt, J. "Probabilistic outputs for support vector machines and comparisons to regularized likelihood methods." In Advances in Large Margin Classifiers. MIT Press, 1999, pages 61–74.

Was this topic helpful?