Documentation

This is machine translation

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

crossval

Class: ClassificationSVM

Cross-validated support vector machine classifier

Syntax

  • CVSVMModel = crossval(SVMModel)
    example
  • CVSVMModel = crossval(SVMModel,Name,Value)
    example

Description

example

CVSVMModel = crossval(SVMModel) returns a cross-validated (partitioned) support vector machine classifier (CVSVMModel) from a trained SVM classifier (SVMModel).

By default, crossval uses 10-fold cross validation on the training data to create CVSVMModel.

example

CVSVMModel = crossval(SVMModel,Name,Value) returns a partitioned SVM classifier with additional options specified by one or more Name,Value pair arguments.

For example, you can specify the number of folds or holdout sample proportion.

Tips

Assess the predictive performance of SVMModel on cross-validated data using the "kfold" methods and properties of CVSVMModel, such as kfoldLoss.

Input Arguments

expand all

Full, trained SVM classifier, specified as a ClassificationSVM model trained using fitcsvm.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

expand all

Cross-validation partition, specified as the comma-separated pair consisting of 'CVPartition' and a cvpartition partition object as created by cvpartition. The partition object specifies the type of cross-validation, and also the indexing for training and validation sets.

To create a cross-validated model, you can use one of these four name-value pair arguments only: CVPartition, Holdout, KFold, or Leaveout.

Fraction of data used for holdout validation, specified as the comma-separated pair consisting of 'Holdout' and a scalar value in the range (0,1). If you specify 'Holdout',p, then the software:

  1. Randomly reserves p*100% of the data as validation data, and trains the model using the rest of the data

  2. Stores the compact, trained model in the Trained property of the cross-validated model.

To create a cross-validated model, you can use one of these four name-value pair arguments only: CVPartition, Holdout, KFold, or Leaveout.

Example: 'Holdout',0.1

Data Types: double | single

Number of folds to use in a cross-validated classifier, specified as the comma-separated pair consisting of 'KFold' and a positive integer value greater than 1. If you specify, e.g., 'KFold',k, then the software:

  1. Randomly partitions the data into k sets

  2. For each set, reserves the set as validation data, and trains the model using the other k – 1 sets

  3. Stores the k compact, trained models in the cells of a k-by-1 cell vector in the Trained property of the cross-validated model.

To create a cross-validated model, you can use one of these four name-value pair arguments only: CVPartition, Holdout, KFold, or Leaveout.

Example: 'KFold',5

Data Types: single | double

Leave-one-out cross-validation flag, specified as the comma-separated pair consisting of 'Leaveout' and 'on' or 'off'. If you specify 'Leaveout','on', then, for each of the n observations, where n is size(Mdl.X,1), the software:

  1. Reserves the observation as validation data, and trains the model using the other n – 1 observations

  2. Stores the n compact, trained models in the cells of an n-by-1 cell vector in the Trained property of the cross-validated model.

To create a cross-validated model, you can use one of these four name-value pair arguments only: CVPartition, Holdout, KFold, or Leaveout.

Example: 'Leaveout','on'

Data Types: char

Output Arguments

expand all

Cross-validated SVM classifier, returned as a ClassificationPartitionedModel classifier.

Examples

expand all

Load the ionosphere data set.

load ionosphere
rng(1); % For reproducibility

Train an SVM classifier. It is good practice to standardize the predictors and define the class order.

SVMModel = fitcsvm(X,Y,'Standardize',true,'ClassNames',{'b','g'});

SVMModel is a trained ClassificationSVM classifier. 'b' is the negative class and 'g' is the positive class.

Cross validate the classifier using 10-fold cross validation.

CVSVMModel = crossval(SVMModel)
FirstModel = CVSVMModel.Trained{1}
CVSVMModel = 

  classreg.learning.partition.ClassificationPartitionedModel
    CrossValidatedModel: 'SVM'
         PredictorNames: {1×34 cell}
           ResponseName: 'Y'
        NumObservations: 351
                  KFold: 10
              Partition: [1×1 cvpartition]
             ClassNames: {'b'  'g'}
         ScoreTransform: 'none'



FirstModel = 

  classreg.learning.classif.CompactClassificationSVM
             ResponseName: 'Y'
    CategoricalPredictors: []
               ClassNames: {'b'  'g'}
           ScoreTransform: 'none'
                    Alpha: [78×1 double]
                     Bias: -0.2209
         KernelParameters: [1×1 struct]
                       Mu: [1×34 double]
                    Sigma: [1×34 double]
           SupportVectors: [78×34 double]
      SupportVectorLabels: [78×1 double]


CVSVMModel is a ClassificationPartitionedModel cross-validated classifier. The software:

  1. Randomly partitions the data into 10, equally sized sets.

  2. Trains an SVM classifier on nine of the sets.

  3. Repeats steps 1 and 2 k = 10 times. It leaves out one of the partitions each time, and trains on the other nine partitions.

  4. Combines generalization statistics for each fold.

FirstModel is the first of the 10 trained classifiers. It is a CompactClassificationSVM classifier.

You can estimate the generalization error by passing CVSVMModel to kfoldLoss.

By default, crossval uses 10-fold cross validation to cross validate an SVM classifier. You have several other options, such as specifying a different number of folds or holdout sample proportion. This example shows how to specify a holdout-sample proportion.

Load the ionosphere data set.

load ionosphere
rng(1); % For reproducibility

Train an SVM classifier. It is good practice to standardize the predictors and define the class order.

SVMModel = fitcsvm(X,Y,'Standardize',true,'ClassNames',{'b','g'});

SVMModel is a trained ClassificationSVM classifier. 'b' is the negative class and 'g' is the positive class.

Cross validate the classifier by specifying a 15% holdout sample.

CVSVMModel = crossval(SVMModel,'Holdout',0.15)
TrainedModel = CVSVMModel.Trained{1}
CVSVMModel = 

  classreg.learning.partition.ClassificationPartitionedModel
    CrossValidatedModel: 'SVM'
         PredictorNames: {1×34 cell}
           ResponseName: 'Y'
        NumObservations: 351
                  KFold: 1
              Partition: [1×1 cvpartition]
             ClassNames: {'b'  'g'}
         ScoreTransform: 'none'



TrainedModel = 

  classreg.learning.classif.CompactClassificationSVM
             ResponseName: 'Y'
    CategoricalPredictors: []
               ClassNames: {'b'  'g'}
           ScoreTransform: 'none'
                    Alpha: [74×1 double]
                     Bias: -0.2952
         KernelParameters: [1×1 struct]
                       Mu: [1×34 double]
                    Sigma: [1×34 double]
           SupportVectors: [74×34 double]
      SupportVectorLabels: [74×1 double]


CVSVMModel is a ClassificationPartitionedModel. TrainedModel is a CompactClassificationSVM classifier trained using 85% of the data.

Estimate the generalization error.

kfoldLoss(CVSVMModel)
ans =

    0.0769

The out-of-sample misclassification error is approximately 8%.

Alternatives

Instead of training an SVM classifier and then cross-validating it, you can create a cross-validated classifier directly using fitcsvm and by specifying any of these name-value pair arguments: 'CrossVal', 'CVPartition', 'Holdout', 'Leaveout', or 'KFold'.

Was this topic helpful?