SVM
MATLAB
10-fold cross-validation
machine learning
classification

Example of 10-fold SVM classification in MATLAB

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Ten-fold cross-validation is a standard way to evaluate an SVM classifier in MATLAB without relying on a single train-test split. The workflow is simple: partition the dataset into 10 folds, train on 9 folds, test on the remaining fold, and average the results across all 10 runs.

A Minimal Working Example

MATLAB makes this fairly direct with fitcsvm and crossval.

matlab
1load fisheriris
2
3X = meas;
4y = species;
5
6svmModel = fitcsvm(X, y, ...
7    'KernelFunction', 'rbf', ...
8    'Standardize', true);
9
10cvModel = crossval(svmModel, 'KFold', 10);
11loss = kfoldLoss(cvModel);
12
13fprintf('10-fold classification error: %.4f\n', loss);
14fprintf('10-fold classification accuracy: %.4f\n', 1 - loss);

This is the fastest way to get a ten-fold result for a classification task in MATLAB.

Key points:

  • 'fitcsvm trains the support vector machine'
  • 'crossval(..., 'KFold', 10) creates the ten-fold evaluation wrapper'
  • 'kfoldLoss returns the average cross-validation loss'

Get Predictions Per Fold

If you want more than the overall error rate, use kfoldPredict.

matlab
1load fisheriris
2
3X = meas;
4y = species;
5
6svmModel = fitcsvm(X, y, ...
7    'KernelFunction', 'rbf', ...
8    'Standardize', true);
9
10cvModel = crossval(svmModel, 'KFold', 10);
11[predictedLabels, scores] = kfoldPredict(cvModel);
12
13accuracy = mean(strcmp(predictedLabels, y));
14fprintf('Cross-validated accuracy: %.4f\n', accuracy);

This gives you one prediction for each observation using only models that did not train on that observation's fold.

That is what makes the evaluation honest.

Binary Example with Numeric Labels

For binary classification, the same structure works with numeric classes.

matlab
1rng(1)
2
3X = [randn(100, 2) + 1; randn(100, 2) - 1];
4y = [ones(100, 1); -ones(100, 1)];
5
6svmModel = fitcsvm(X, y, ...
7    'KernelFunction', 'linear', ...
8    'Standardize', true);
9
10cvModel = crossval(svmModel, 'KFold', 10);
11[predictedLabels, ~] = kfoldPredict(cvModel);
12
13accuracy = mean(predictedLabels == y);
14fprintf('10-fold accuracy: %.4f\n', accuracy);

This is a good template when your data is already numeric and preprocessed.

Why Standardization Matters

SVM performance is often sensitive to feature scale. If one feature is much larger in magnitude than another, the classifier can behave poorly.

That is why examples commonly include:

matlab
'Standardize', true

This tells MATLAB to normalize predictor scaling during training. For many real datasets, leaving this out will hurt results more than people expect.

Hyperparameter Tuning

Ten-fold cross-validation can also be used while tuning the kernel and box constraint, but do not confuse "cross-validation for evaluation" with "cross-validation for model selection."

For example:

matlab
1svmModel = fitcsvm(X, y, ...
2    'KernelFunction', 'rbf', ...
3    'Standardize', true, ...
4    'OptimizeHyperparameters', {'BoxConstraint', 'KernelScale'});

That is useful, but remember that once you use the same data for extensive tuning, the final performance estimate becomes less clean unless you use nested validation logic.

For many practical problems, start with a simple cross-validated baseline first and tune second.

Inspect More Than Accuracy

Accuracy is not always enough, especially with class imbalance. You may also want a confusion matrix.

matlab
[predictedLabels, ~] = kfoldPredict(cvModel);
confusionchart(y, predictedLabels);

This helps answer questions such as:

  • which class is misclassified most often
  • whether one class dominates the metric
  • whether a model that looks good on accuracy is actually weak on one class

Common Pitfalls

  • Evaluating on the same data used to train the model instead of using cross-validation.
  • Forgetting feature standardization for SVMs with differently scaled inputs.
  • Treating tuned cross-validation results as if they were untouched holdout results.
  • Looking only at accuracy when class balance is poor.
  • Using ten-fold CV mechanically without first checking that labels and predictors are aligned correctly.

Summary

  • In MATLAB, ten-fold SVM classification is commonly done with fitcsvm, crossval, and kfoldLoss.
  • Use kfoldPredict when you need fold-safe predictions for every sample.
  • Standardization is usually important for SVM performance.
  • Accuracy is useful, but confusion matrices and class-level behavior matter too.
  • Start with a simple cross-validated baseline before doing heavier hyperparameter tuning.

Course illustration
Course illustration

All Rights Reserved.