libsvm
multi-class classification
machine learning
support vector machines
data science

Multi-class classification in libsvm

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

LIBSVM supports multi-class classification out of the box. You do not need to manually wrap it in a one-vs-rest loop just because your labels have more than two classes.

Internally, LIBSVM handles the multi-class problem using a one-vs-one strategy. That means it trains a binary classifier for each pair of classes and combines their votes at prediction time.

How LIBSVM Handles Multiple Classes

Suppose your labels are 1, 2, and 3. LIBSVM trains these pairwise classifiers:

  • class 1 versus class 2
  • class 1 versus class 3
  • class 2 versus class 3

At prediction time, each binary model votes for one class, and the class with the most votes wins.

For k classes, LIBSVM trains k * (k - 1) / 2 binary classifiers. That can become expensive for many classes, but it works well for a broad range of practical problems.

The Data Format

Training data still uses the normal LIBSVM format:

text
1 1:0.2 2:1.3 3:0.7
2 1:1.1 2:0.4 3:0.9
3 1:0.5 2:0.8 3:1.7

The first value on each line is the class label. For multi-class classification, just use more than two distinct labels.

That is the key point: there is no special multi-class file format. Multi-class behavior comes from the presence of several label values.

Basic Training and Prediction

Train as usual:

bash
svm-train train.txt model.txt

Predict as usual:

bash
svm-predict test.txt model.txt predictions.txt

The commands are the same as binary classification. LIBSVM decides internally how many pairwise classifiers are required based on the labels in the training data.

Feature Scaling Still Matters

SVMs are very sensitive to feature scale, especially with RBF kernels. Before training, scale the data consistently:

bash
svm-scale -l -1 -u 1 train.txt > train.scaled
svm-scale -r scale.params test.txt > test.scaled

The usual workflow is:

  1. scale the training data and save the scaling parameters
  2. apply the same scaling parameters to test or production data
  3. train and predict on the scaled data

Skipping scaling is one of the easiest ways to get weak results even when the model setup is otherwise correct.

Tune C and gamma

For an RBF kernel, the most important hyperparameters are usually:

  • 'C, the regularization strength'
  • 'gamma, the kernel parameter'

A common workflow is a grid search with cross-validation:

bash
svm-train -s 0 -t 2 -c 1 -g 0.1 -v 5 train.scaled

Here:

  • '-s 0 means C-SVC classification'
  • '-t 2 means RBF kernel'
  • '-v 5 performs 5-fold cross-validation'

You repeat that across candidate C and gamma values, then train the final model on the full training set with the best combination.

Probability Estimates

If you need probability estimates, enable them during training and prediction:

bash
svm-train -b 1 train.scaled model.txt
svm-predict -b 1 test.scaled model.txt predictions.txt

This can be useful for ranking or downstream decision thresholds, but it adds overhead and the probabilities are only as good as the calibration quality of the model.

Python Example

If you are using a LIBSVM Python binding:

python
1from libsvm.svmutil import svm_problem, svm_parameter, svm_train, svm_predict
2
3y = [1, 2, 3, 1]
4X = [
5    {1: 0.2, 2: 1.3},
6    {1: 1.1, 2: 0.4},
7    {1: 0.5, 2: 1.7},
8    {1: 0.3, 2: 1.0},
9]
10
11problem = svm_problem(y, X)
12param = svm_parameter("-s 0 -t 2 -c 1 -g 0.5")
13model = svm_train(problem, param)
14
15svm_predict(y, X, model)

Again, nothing special is required for multi-class beyond having multiple labels.

Common Pitfalls

The most common mistake is assuming LIBSVM only supports binary classification and that you must build your own one-vs-rest wrapper. For standard multi-class classification, LIBSVM already handles the decomposition internally.

Another common issue is not scaling features consistently. Multi-class support does not rescue poor preprocessing.

People also forget that one-vs-one means many classifiers when the number of classes grows. Training time and model size can increase quickly for large label sets.

Finally, probability estimates are optional. Do not enable them unless you actually need them, because they add extra cost and complexity.

Summary

  • LIBSVM supports multi-class classification natively.
  • It uses a one-vs-one strategy internally, not one-vs-rest by default.
  • Training and prediction commands are the same as for binary classification.
  • Good feature scaling and parameter tuning are still essential.
  • Multi-class support is easy to use, but cost grows as the number of classes increases.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.