libsvm
c++ tutorial
machine learning
support vector machine
programming guide

Tutorial for libsvm c

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 is a widely-used C/C++ library for Support Vector Machine (SVM) classification and regression. It supports multi-class classification, probability estimation, and various kernel types. LIBSVM provides both a command-line interface and a C/C++ API for integrating SVMs directly into applications.

Data Format

LIBSVM uses a sparse data format where each line represents one sample:

 
<label> <index1>:<value1> <index2>:<value2> ...

Example file (train.txt):

 
11 1:0.2 2:0.4
2-1 1:0.8 2:0.1
31 1:0.3 2:0.6
4-1 1:0.9 2:0.2
  • Labels are class identifiers (1, -1 for binary; 1, 2, 3, ... for multi-class)
  • Indices start at 1 (not 0)
  • Zero-valued features can be omitted (sparse representation)

Command-Line Usage

Training

bash
1# Download and build
2git clone https://github.com/cjlin1/libsvm.git
3cd libsvm
4make
5
6# Train a model
7./svm-train train.txt model.txt
8
9# Train with options
10./svm-train -s 0 -t 2 -c 10 -g 0.01 train.txt model.txt

Key parameters:

FlagOptionValues
-sSVM type0=C-SVC, 1=nu-SVC, 3=epsilon-SVR, 4=nu-SVR
-tKernel type0=linear, 1=polynomial, 2=RBF, 3=sigmoid
-cCost parameterHigher = stricter margin (default 1)
-gGamma for RBF/poly/sigmoidDefault 1/num_features
-dDegree for polynomialDefault 3
-vCross-validation foldse.g., -v 5 for 5-fold CV

Prediction

bash
1./svm-predict test.txt model.txt output.txt
2# Accuracy = 95% (19/20)
3
4# With probability estimates
5./svm-train -b 1 train.txt model.txt
6./svm-predict -b 1 test.txt model.txt output.txt

Scaling

Always scale features to [-1, 1] or [0, 1] before training:

bash
./svm-scale -l 0 -u 1 train.txt > train_scaled.txt
./svm-scale -l 0 -u 1 test.txt > test_scaled.txt

C/C++ API

Core Structures

c
1#include "svm.h"
2
3// svm_problem: holds the training data and labels
4struct svm_problem {
5    int l;                    // number of training samples
6    double *y;                // array of labels
7    struct svm_node **x;      // array of feature vectors
8};
9
10// svm_node: a single feature (index:value pair)
11struct svm_node {
12    int index;    // feature index (1-based, -1 terminates)
13    double value; // feature value
14};
15
16// svm_parameter: contains settings for SVM type, kernel type, and hyperparameters
17struct svm_parameter {
18    int svm_type;     // C_SVC, NU_SVC, ONE_CLASS, EPSILON_SVR, NU_SVR
19    int kernel_type;  // LINEAR, POLY, RBF, SIGMOID
20    double C;         // cost parameter
21    double gamma;     // kernel parameter
22    // ... more fields
23};

Complete Training Example

c
1#include <stdio.h>
2#include <stdlib.h>
3#include "svm.h"
4
5int main() {
6    // Define training data: 4 samples, 2 features each
7    struct svm_problem prob;
8    prob.l = 4;
9
10    // Labels
11    double labels[] = {1, -1, 1, -1};
12    prob.y = labels;
13
14    // Feature vectors (each terminated by index=-1)
15    struct svm_node nodes[4][3] = {
16        {{1, 0.2}, {2, 0.4}, {-1, 0}},
17        {{1, 0.8}, {2, 0.1}, {-1, 0}},
18        {{1, 0.3}, {2, 0.6}, {-1, 0}},
19        {{1, 0.9}, {2, 0.2}, {-1, 0}}
20    };
21
22    struct svm_node *x[4];
23    for (int i = 0; i < 4; i++)
24        x[i] = nodes[i];
25    prob.x = x;
26
27    // Set parameters
28    struct svm_parameter param;
29    param.svm_type = C_SVC;
30    param.kernel_type = RBF;
31    param.C = 10;
32    param.gamma = 0.5;
33    param.cache_size = 100;
34    param.eps = 1e-3;
35    param.shrinking = 1;
36    param.probability = 0;
37    param.nr_weight = 0;
38
39    // Check parameters
40    const char *error = svm_check_parameter(&prob, &param);
41    if (error) {
42        printf("Parameter error: %s\n", error);
43        return 1;
44    }
45
46    // Train the model
47    struct svm_model *model = svm_train(&prob, &param);
48
49    // Predict a new sample
50    struct svm_node test[] = {{1, 0.25}, {2, 0.5}, {-1, 0}};
51    double prediction = svm_predict(model, test);
52    printf("Prediction: %.0f\n", prediction);
53
54    // Save and load model
55    svm_save_model("model.txt", model);
56    struct svm_model *loaded = svm_load_model("model.txt");
57
58    // Clean up
59    svm_free_and_destroy_model(&model);
60    svm_free_and_destroy_model(&loaded);
61
62    return 0;
63}

Compiling

bash
1# Compile with libsvm
2gcc -o my_svm my_svm.c svm.cpp -lstdc++ -lm
3
4# Or with g++
5g++ -o my_svm my_svm.c svm.cpp -lm

Cross-Validation

c
1double *target = malloc(prob.l * sizeof(double));
2svm_cross_validation(&prob, &param, 5, target);  // 5-fold CV
3
4int correct = 0;
5for (int i = 0; i < prob.l; i++)
6    if (target[i] == prob.y[i]) correct++;
7printf("CV Accuracy: %.2f%%\n", 100.0 * correct / prob.l);
8free(target);

Common Pitfalls

  • Feature scaling: LIBSVM is sensitive to feature scales. Always scale features to [-1, 1] or [0, 1] before training. Unscaled features lead to poor accuracy and slow convergence.
  • Index must start at 1: Feature indices in svm_node are 1-based. Using 0-based indices causes incorrect results or crashes.
  • Terminator node: Each feature vector must end with a node where index = -1. Forgetting this causes buffer overruns.
  • Memory management: svm_train allocates the model internally. Always free with svm_free_and_destroy_model, not free().
  • Grid search: Use the provided tools/grid.py script to find optimal C and gamma via grid search with cross-validation.

Summary

  • LIBSVM uses sparse index:value format for data, with labels as the first column
  • Core structures: svm_problem (data), svm_parameter (settings), svm_node (features)
  • Always scale features before training and terminate feature vectors with index=-1
  • Use svm_train() / svm_predict() for the C API or command-line tools for quick experiments
  • Run grid search with cross-validation to find optimal C and gamma parameters

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.