Documentation

CompactClassificationSVM

Compact support vector machine (SVM) for one-class and binary classification

Description

CompactClassificationSVMis a compact version of the support vector machine (SVM) classifier. The compact classifier does not include the data used for training the SVM classifier. Therefore, you cannot perform some tasks, such as cross-validation, using the compact classifier. Use a compact SVM classifier for tasks such as predicting the labels of new data.

Creation

Create aCompactClassificationSVMmodel from a full, trainedClassificationSVMclassifier by usingcompact.

Properties

expand all

SVM Properties

This property is read-only.

Trained classifier coefficients, specified as ans-by-1 numeric vector.sis the number of support vectors in the trained classifier,sum(Mdl.IsSupportVector).

Alphacontains the trained classifier coefficients from the dual problem, that is, the estimated Lagrange multipliers. If you remove duplicates by using theRemoveDuplicatesname-value pair argument offitcsvm, then for a given set of duplicate observations that are support vectors,Alphacontains one coefficient corresponding to the entire set. That is, MATLAB®attributes a nonzero coefficient to one observation from the set of duplicates and a coefficient of0to all other duplicate observations in the set.

Data Types:single|double

This property is read-only.

Linear predictor coefficients, specified as a numeric vector. The length ofBetais equal to the number of predictors used to train the model.

MATLAB expands categorical variables in the predictor data using full dummy encoding. That is, MATLAB creates one dummy variable for each level of each categorical variable.Betastores one value for each predictor variable, including the dummy variables. For example, if there are three predictors, one of which is a categorical variable with three levels, thenBetais a numeric vector containing five values.

IfKernelParameters.Functionis'linear'上海合作组织,然后分类re for the observationxis

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

Mdlstoresβ,b, andsin the propertiesBeta,Bias, andKernelParameters.Scale, respectively.

To estimate classification scores manually, you must first apply any transformations to the predictor data that were applied during training. Specifically, if you specify'Standardize',truewhen usingfitcsvm, then you must standardize the predictor data manually by using the meanMdl.Muand standard deviationMdl.Sigma, and then divide the result by the kernel scale inMdl.KernelParameters.Scale.

All SVM functions, such asresubPredictandpredict, apply any required transformation before estimation.

IfKernelParameters.Functionis not'linear', thenBetais empty ([]).

Data Types:single|double

This property is read-only.

Bias term, specified as a scalar.

Data Types:single|double

This property is read-only.

Kernel parameters, specified as a structure array. The kernel parameters property contains the fields listed in this table.

Field Description
Function

Kernel function used to compute the elements of theGram matrix. For details, see'KernelFunction'.

Scale

Kernel scale parameter used to scale all elements of the predictor data on which the model is trained. For details, see'KernelScale'.

To display the values ofKernelParameters, use dot notation. For example,Mdl.KernelParameters.Scaledisplays the kernel scale parameter value.

The software acceptsKernelParametersas inputs and does not modify them.

Data Types:struct

This property is read-only.

Support vector class labels, specified as ans-by-1 numeric vector.sis the number of support vectors in the trained classifier,sum(Mdl.IsSupportVector).

A value of+1inSupportVectorLabelsindicates that the corresponding support vector is in the positive class (ClassNames{2}). A value of–1indicates that the corresponding support vector is in the negative class (ClassNames{1}).

If you remove duplicates by using theRemoveDuplicatesname-value pair argument offitcsvm, then for a given set of duplicate observations that are support vectors,SupportVectorLabelscontains one unique support vector label.

Data Types:single|double

This property is read-only.

Support vectors in the trained classifier, specified as ans-by-pnumeric matrix.sis the number of support vectors in the trained classifier,sum(Mdl.IsSupportVector), andpis the number of predictor variables in the predictor data.

SupportVectorscontains rows of the predictor dataXthat MATLAB considers to be support vectors. If you specify'Standardize',truewhen training the SVM classifier usingfitcsvm, thenSupportVectorscontains the standardized rows ofX.

If you remove duplicates by using theRemoveDuplicatesname-value pair argument offitcsvm, then for a given set of duplicate observations that are support vectors,SupportVectorscontains one unique support vector.

Data Types:single|double

Other Classification Properties

This property is read-only.

Categorical predictor indices, specified as a vector of positive integers.CategoricalPredictorscontains index values corresponding to the columns of the predictor data that contain categorical predictors. If none of the predictors are categorical, then this property is empty ([]).

Data Types:single|double

This property is read-only.

Unique class labels used in training the model, specified as a categorical or character array, logical or numeric vector, or cell array of character vectors.

Data Types:single|double|logical|char|cell|categorical

This property is read-only.

Misclassification cost, specified as a numeric square matrix, where成本(i, j)is the cost of classifying a point into classjif its true class isi.

During training, the software updates the prior probabilities by incorporating the penalties described in the cost matrix.

  • For two-class learning,Costalways has this form:成本(i, j)= 1ifi ~= j, and成本(i, j)= 0ifi = j. The rows correspond to the true class and the columns correspond to the predicted class. The order of the rows and columns ofCostcorresponds to the order of the classes inClassNames.

  • For one-class learning,Cost = 0.

For more details, seeAlgorithms.

Data Types:double

This property is read-only.

Expanded predictor names, specified as a cell array of character vectors.

If the model uses dummy variable encoding for categorical variables, thenExpandedPredictorNamesincludes the names that describe the expanded variables. Otherwise,ExpandedPredictorNamesis the same asPredictorNames.

Data Types:char|cell

This property is read-only.

Predictor means, specified as a numeric vector. If you specify'Standardize',1or'Standardize',truewhen you train an SVM classifier usingfitcsvm, then the length ofMuis equal to the number of predictors.

MATLAB expands categorical variables in the predictor data using full dummy encoding. That is, MATLAB creates one dummy variable for each level of each categorical variable.Mustores one value for each predictor variable, including the dummy variables. However, MATLAB does not standardize the columns that contain categorical variables.

If you set'Standardize',falsewhen you train the SVM classifier usingfitcsvm, thenMuis an empty vector ([]).

Data Types:single|double

This property is read-only.

Predictor variable names, specified as a cell array of character vectors. The order of the elements ofPredictorNamescorresponds to the order in which the predictor names appear in the training data.

Data Types:char|cell

This property is read-only.

Prior probabilities for each class, specified as a numeric vector. The order of the elements ofPriorcorresponds to the elements ofMdl.ClassNames.

For two-class learning, if you specify a cost matrix, then the software updates the prior probabilities by incorporating the penalties described in the cost matrix.

For more details, seeAlgorithms.

Data Types:single|double

Score transformation, specified as a character vector or function handle.ScoreTransformrepresents a built-in transformation function or a function handle for transforming predicted classification scores.

To change the score transformation function tofunction, for example, use dot notation.

  • For a built-in function, enter a character vector.

    Mdl.ScoreTransform = 'function';

    This table describes the available built-in functions.

    Value Description
    'doublelogit' 1/(1 +e–2x)
    'invlogit' log(x/ (1–x))
    'ismax' Sets the score for the class with the largest score to1, and sets the scores for all other classes to0
    'logit' 1/(1 +ex)
    'none'or'identity' x(no transformation)
    'sign' –1 forx< 0
    0 forx= 0
    1 forx> 0
    'symmetric' 2x– 1
    'symmetricismax' Sets the score for the class with the largest score to1, and sets the scores for all other classes to–1
    'symmetriclogit' 2/(1 +ex) – 1

  • For a MATLAB function or a function that you define, enter its function handle.

    Mdl.ScoreTransform = @function;

    functionshould accept a matrix (the original scores) and return a matrix of the same size (the transformed scores).

Data Types:char|function_handle

This property is read-only.

Predictor standard deviations, specified as a numeric vector.

If you specify'Standardize',truewhen you train the SVM classifier usingfitcsvm, then the length ofSigmais equal to the number of predictor variables.

MATLAB expands categorical variables in the predictor data using full dummy encoding. That is, MATLAB creates one dummy variable for each level of each categorical variable.Sigmastores one value for each predictor variable, including the dummy variables. However, MATLAB does not standardize the columns that contain categorical variables.

If you set'Standardize',falsewhen you train the SVM classifier usingfitcsvm, thenSigmais an empty vector ([]).

Data Types:single|double

Object Functions

compareHoldout Compare accuracies of two classification models using new data
discardSupportVectors Discard support vectors for linear support vector machine (SVM) classifier
edge Find classification edge for support vector machine (SVM) classifier
fitPosterior Fit posterior probabilities for compact support vector machine (SVM) classifier
loss Find classification error for support vector machine (SVM) classifier
margin Find classification margins for support vector machine (SVM) classifier
predict Predict labels using support vector machine (SVM) classifier

Examples

collapse all

Reduce the size of a full SVM classifier by removing the training data. Full SVM classifiers (that is,ClassificationSVMclassifiers) hold the training data. To improve efficiency, use a smaller classifier.

Load theionospheredata set.

loadionosphere

Train an SVM classifier. Standardize the predictor data and specify the order of the classes.

SVMModel = fitcsvm(X,Y,'Standardize',true,...'ClassNames',{'b','g'})
SVMModel = ClassificationSVM ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' NumObservations: 351 Alpha: [89x1 double] Bias: -0.1341 KernelParameters: [1x1 struct] Mu: [1x34 double] Sigma: [1x34 double] BoxConstraints: [351x1 double] ConvergenceInfo: [1x1 struct] IsSupportVector: [351x1 logical] Solver: 'SMO' Properties, Methods

SVMModelis aClassificationSVMclassifier.

Reduce the size of the SVM classifier.

CompactSVMModel = compact(SVMModel)
CompactSVMModel = classreg.learning.classif.CompactClassificationSVM ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' Alpha: [89x1 double] Bias: -0.1341 KernelParameters: [1x1 struct] Mu: [1x34 double] Sigma: [1x34 double] SupportVectors: [89x34 double] SupportVectorLabels: [89x1 double] Properties, Methods

CompactSVMModelis aCompactClassificationSVMclassifier.

Display the amount of memory each classifier uses.

whos(“SVMModel”,'CompactSVMModel')
类属性名称大小字节CompactSVMModel 1x1 30224 classreg.learning.classif.CompactClassificationSVM SVMModel 1x1 140546 ClassificationSVM

The full SVM classifier (SVMModel) is more than four times larger than the compact SVM classifier (CompactSVMModel).

To label new observations efficiently, you can removeSVMModelfrom the MATLAB® Workspace, and then passCompactSVMModeland new predictor values topredict.

To further reduce the size of your compact SVM classifier, use thediscardSupportVectorsfunction to discard support vectors.

Load theionospheredata set.

loadionosphere

Train and cross-validate an SVM classifier. Standardize the predictor data and specify the order of the classes.

rng(1);% For reproducibilityCVSVMModel = fitcsvm(X,Y,'Standardize',true,...'ClassNames',{'b','g'},'CrossVal','on')
CVSVMModel = classreg.learning.partition.ClassificationPartitionedModel CrossValidatedModel: 'SVM' PredictorNames: {1x34 cell} ResponseName: 'Y' NumObservations: 351 KFold: 10 Partition: [1x1 cvpartition] ClassNames: {'b' 'g'} ScoreTransform: 'none' Properties, Methods

CVSVMModelis aClassificationPartitionedModelcross-validated SVM classifier. By default, the software implements 10-fold cross-validation.

Alternatively, you can cross-validate a trainedClassificationSVMclassifier by passing it tocrossval.

Inspect one of the trained folds using dot notation.

CVSVMModel.Trained{1}
ans = classreg.learning.classif.CompactClassificationSVM ResponseName: 'Y' CategoricalPredictors: [] ClassNames: {'b' 'g'} ScoreTransform: 'none' Alpha: [78x1 double] Bias: -0.2209 KernelParameters: [1x1 struct] Mu: [1x34 double] Sigma: [1x34 double] SupportVectors: [78x34 double] SupportVectorLabels: [78x1 double] Properties, Methods

Each fold is aCompactClassificationSVMclassifier trained on 90% of the data.

Estimate the generalization error.

genError = kfoldLoss(CVSVMModel)
genError = 0.1168

On average, the generalization error is approximately 12%.

References

[1] Hastie, T., R. Tibshirani, and J. Friedman. The Elements of Statistical Learning, Second Edition. NY: Springer, 2008.

[2] Scholkopf, B., J. C. Platt, J. C. Shawe-Taylor, A. J. Smola, and R. C. Williamson. “Estimating the Support of a High-Dimensional Distribution.” Neural Computation. Vol. 13, Number 7, 2001, pp. 1443–1471.

[3] Christianini, N., and J. C. Shawe-Taylor. An Introduction to Support Vector Machines and Other Kernel-Based Learning Methods. Cambridge, UK: Cambridge University Press, 2000.

[4] Scholkopf, B., and A. Smola. Learning with Kernels: Support Vector Machines, Regularization, Optimization and Beyond, Adaptive Computation and Machine Learning. Cambridge, MA: The MIT Press, 2002.

Extended Capabilities

Introduced in R2014a

Was this topic helpful?