Introduction
LIBSVM is an open-source library for Support Vector Machines (SVMs) developed by Chih-Chung Chang and Chih-Jen Lin at National Taiwan University. It provides efficient implementations of various SVM formulations, including classification (C-SVC, nu-SVC), regression (epsilon-SVR, nu-SVR), and one-class SVM. Known for its speed, modularity, and ease of use, LIBSVM supports multiple kernel functions, probability estimates, and cross-validation. It is widely used in machine learning research and industry, with interfaces for C++, Java, Python, MATLAB, and other languages.
History and Development
LIBSVM was first released in 2000 by the research group of Chih-Jen Lin at National Taiwan University. The project aimed to provide a reliable, easy-to-use implementation of the support vector machine algorithm, which had gained prominence in the late 1990s. The library evolved through multiple versions, incorporating improvements in training algorithms (e.g., sequential minimal optimization), support for probability estimates, and multi-class classification strategies. The last major release (version 3.x) continued to see maintenance updates. LIBSVM became one of the most cited and reused SVM implementations in the academic community.
Key Features
LIBSVM offers a compact set of core features: training and prediction for classification, regression, and one-class problems; several built-in kernel functions (linear, polynomial, radial basis function, sigmoid); probability output for classification; cross-validation; automatic parameter selection tools; and data scaling utilities. Its interfaces span many programming languages, and the library is optimized for both computational efficiency and memory use through caching and decomposition methods.
Supported SVM Formulations
Classification SVMs
C-SVC
C-SVC (C-Support Vector Classification) is the standard SVM for binary classification. It solves the primal optimization problem with a penalty parameter C that controls the trade-off between maximizing the margin and minimizing the classification error on the training data. The decision function is based on the sign of the weighted sum of support vectors.
nu-SVC
nu-SVC (nu-Support Vector Classification) is an alternative formulation where the parameter nu (0 < nu ≤ 1) controls the number of support vectors and training errors. It replaces the C parameter with a more intuitive bound: nu is an upper bound on the fraction of training errors and a lower bound on the fraction of support vectors. This formulation is equivalent to C-SVC in the sense that a suitable C can be mapped to a corresponding nu.
Regression SVMs
epsilon-SVR
epsilon-SVR (epsilon-Support Vector Regression) aims to find a function that deviates from the actual target values by at most epsilon for each training point, while being as flat as possible. The parameter epsilon defines the tube width; points outside the tube contribute to the loss. The C parameter controls the penalty for such deviations.
nu-SVR
nu-SVR (nu-Support Vector Regression) is the regression counterpart of nu-SVC. It replaces epsilon with a parameter nu that controls the number of support vectors and the fraction of points outside the epsilon tube. This formulation allows automatic adjustment of the epsilon value.
One-Class SVM
Use Cases for Anomaly Detection
One-class SVM is designed for novelty or anomaly detection. It learns a boundary that encloses the majority of the training data (assumed to be normal) and identifies outliers as points falling outside that boundary. It uses a parameter mu or nu to control the fraction of training points considered as outliers. Applications include fault detection, network intrusion detection, and outlier analysis in high-dimensional datasets.
Algorithms and Implementation
Training Algorithms
Sequential Minimal Optimization (SMO)
SMO is a decomposition algorithm that breaks the SVM quadratic programming (QP) problem into a series of smallest possible sub-problems, each involving only two Lagrange multipliers. LIBSVM implements a variant of SMO with working set selection rules that improve convergence. It stores the kernel matrix in a cache to avoid recomputation.
Decomposition Methods
For large datasets, LIBSVM uses a decomposition approach that selects a subset (working set) of variables to optimize at each iteration. The working set size is typically small (e.g., 2 to 100 variables). The algorithm alternates between selecting a working set and solving a smaller QP subproblem, using caching to accelerate kernel evaluation.
Kernel Functions
Linear Kernel
K(x, y) = x · y. The linear kernel corresponds to a linear decision boundary and is equivalent to a non-mapped SVM. It is often used for high-dimensional or sparse data.
Polynomial Kernel
K(x, y) = (γ · x · y + coef0)^d. The polynomial kernel computes a polynomial of degree d. It introduces curvature in the decision boundary.
Radial Basis Function (RBF) Kernel
| K(x, y) = exp(-γ | x - y | ^2). The RBF (Gaussian) kernel is the most commonly used kernel in LIBSVM. It can handle non-linear relationships and has fewer parameters than the polynomial kernel. It is a universal approximator. |
|---|
Sigmoid Kernel
K(x, y) = tanh(γ · x · y + coef0). The sigmoid kernel is inspired by neural networks. For certain parameter values, it behaves like a two-layer perceptron. However, it is not always positive definite.
Precomputed Kernel
LIBSVM allows the user to provide a custom kernel matrix directly, bypassing the built-in kernel functions. The precomputed kernel option is specified by setting the kernel type to 4. This is useful for domain-specific similarity measures.
Probability Estimates
Platt Scaling
For binary classification, LIBSVM estimates class probabilities by fitting a sigmoid function (Platt scaling) to the decision values. The sigmoid parameters are learned using the training data with cross-validation to avoid overfitting.
Pairwise Coupling for Multi-class
For multi-class classification (using one-versus-one), LIBSVM estimates pairwise class probabilities and then couples them to obtain multi-class probability estimates. The coupling method is based on solving a system of equations that reflect the pairwise probabilities.
Cross-Validation and Model Selection
v-Fold Cross-Validation
LIBSVM supports v-fold cross-validation, which divides the training set into v subsets. The model is trained on v-1 subsets and tested on the held-out subset, rotating through all subsets. The average accuracy is reported, which helps to evaluate model performance.
Grid Search for Parameter Tuning
To find optimal hyperparameters (e.g., C and gamma for RBF kernel), users can perform grid search: train models for a set of predefined parameter pairs (e.g., on a logarithmic grid) and select the combination with the best cross-validation accuracy.
Automatic Parameter Selection Tools (e.g., EasySVM)
EasySVM (distributed with LIBSVM) automates model selection for classification. It uses a simple grid search with cross-validation to recommend the best C and gamma for the RBF kernel. Similar tools are available for regression (EasySVR).
Interfaces and Usage
Command-Line Interface
Basic Commands: svm-train, svm-predict, svm-scale
The command-line tools are the primary way to use LIBSVM on Unix-like systems and Windows. svm-train reads training data and produces a model file. svm-predict uses the trained model to predict labels or values on test data. svm-scale scales feature values to a given range (commonly [0,1] or [-1,1]).
Options and Parameter Files
All command-line tools accept options for specifying the SVM type, kernel type, parameters (C, gamma, nu, epsilon, etc.), and paths to input/output files. Options can be provided on the command line or in a parameter file (using the -param option). The -v flag invokes cross-validation.
Library Calls
C/C++ API
The core LIBSVM library is written in C. The API consists of functions such as svm_train, svm_predict, svm_cross_validation, and svm_save_model/svm_load_model. Parameters are passed via the svm_parameter structure. The C++ interface provides a thin wrapper.
Java API
LIBSVM includes a Java translation of the C code. The Java class svm_train and svm_predict provide similar functionality. It can be used in Java applications and distributed systems.
Python Interface (LIBSVM Python)
The Python interface (formerly libsvm, now often used through scikit-learn) provides functions for training, prediction, and cross-validation. The module svmutil simplifies data loading, model persistence, and parameter tuning. The interface supports both dense and sparse data representations.
Other Language Bindings (R, Perl, Ruby, .NET)
LIBSVM has contributed interfaces for R (package e1071 includes a wrapper), Perl, Ruby, and .NET. These bindings typically wrap the C library, allowing native-speed SVM operations within each language environment.
MATLAB and Octave Interface
Function Signatures
LIBSVM provides a MEX interface for MATLAB and Octave. Key functions include svmtrain and svmpredict. The training function returns a model structure; the prediction function returns predicted labels and optionally accuracy and decision values. The interface supports the same SVM types and kernels as the command-line version.
Integration with Statistics Toolbox
While MATLAB has its own SVM functionality in the Statistics and Machine Learning Toolbox, the LIBSVM interface is favored for its speed and flexibility. It can be combined with MATLAB’s data preprocessing and visualization tools.
Data Format and Files
Training and Testing Data Format
Sparse Format
LIBSVM uses a sparse format to store feature vectors: each line corresponds to one sample, starting with the label (or target value), followed by pairs of <index>:<value> where index is the feature number (1-based) and value is the feature value. Zero-valued features are omitted. This format reduces storage and CPU time for high-dimensional, sparse data.
Label and Feature Representation
For classification, labels are typically integer class IDs (e.g., +1 and -1 for binary; 1,2,3,... for multi-class). For regression, labels are real numbers. Features can be integer or real; the scale should be consistent (see scaling). The format supports missing features by simply omitting indices.
Model File Format
Header Parameters
The model file begins with a header containing metadata: the SVM type, kernel type, kernel parameters, number of classes, number of support vectors, and other parameters (e.g., rho bias terms). Each field is written as label value.
Support Vectors and Coefficients
Following the header, the model lists the support vectors and their coefficients. Each support vector is written as a sparse line of feature indices and values, preceded by the coefficient (alpha_i times the label for classification). For multi-class models, each binary classifier's coefficients are stored separately.
Data Scaling and Normalization
Purpose of Scaling
Scaling avoids attributes in greater numeric ranges dominating those in smaller ranges. It also improves numerical stability for kernel computations (e.g., RBF kernel uses Euclidean distance). Common practice is to scale each feature to the range [0,1] or [-1,1].
Using svm-scale
The svm-scale tool reads training or test data and writes scaled output to standard output or a file. Users specify the lower and upper bounds (default [0,1]) using the -l and -u options. The scaling parameters computed from the training set (feature-wise minimum and range) are saved to a scaling file, which can be reapplied to test data using the -r option.
Extensions and Variants
Preprocessing and Feature Selection Tools
LIBSVM does not include built-in feature selection. However, several auxiliary scripts (e.g., checkdata.py, subset.py) help inspect data, generate random subsets, or convert data formats. External tools like chi-square or mutual information filters can be used before feeding data to LIBSVM.
Multi-class Classification
One-Versus-One Strategy
LIBSVM uses the one-versus-one (OVO) approach for multi-class problems. For k classes, it trains k(k-1)/2 binary classifiers, each separating two classes. This strategy is efficient for moderate numbers of classes.
Voting and Decision Values
During prediction, each binary classifier votes for one of the two classes. The class with the most votes is assigned. Optionally, decision values (pairwise distances) can be used to compute multi-class probability estimates via pairwise coupling.
Weighted SVM for Imbalanced Data
Cost-Sensitive Learning
LIBSVM supports weighted training for imbalanced datasets. The user can assign a weight (cost) to each class via the -wi option, where i is the class label and weight is a positive number. This magnifies the penalty for misclassifying minority class samples, which can improve recall.
Performance and Benchmarking
Comparison with Other SVM Libraries
LIBSVM vs SVMlight
SVMlight (by Thorsten Joachims) is an earlier SVM implementation known for handling very large datasets with its own decomposition algorithm. LIBSVM is generally faster for medium-scale problems due to its optimized caching and SMO implementation. SVMlight offers more custom options (e.g., custom kernels, ranking SVMs) but is less user-friendly.
LIBSVM vs LIBLINEAR
LIBLINEAR is a separate project by the same group focusing on linear SVMs and logistic regression. For large-scale, sparse, or high-dimensional data (e.g., text classification), LIBLINEAR is orders of magnitude faster than LIBSVM, because it avoids kernel computations. LIBSVM remains competitive for non-linear problems.
Scalability on Large Datasets
LIBSVM’s memory and training time scale roughly quadratically with the number of support vectors and linearly with the number of non-zero features. For datasets with hundreds of thousands of samples, LIBSVM can become slow. Parallel and distributed implementations have been developed (e.g., PSVM, MPI-based variants), but the standard library is single-threaded.
Memory and Time Efficiency
Caching Techniques
LIBSVM caches the dot products (or kernel evaluations) computed during training. The cache size can be set by the -c parameter (in MB). A larger cache reduces the need for recomputation of kernel values, significantly speeding up training for medium-sized datasets.
Parallel and Distributed Implementations
The official LIBSVM is single-threaded. However, third-party implementations (e.g., ThunderSVM) parallelize the training process on multi-core CPUs or GPUs. LIBSVM’s grid search can be trivially parallelized by running multiple training jobs in parallel.
Related Software and Alternatives
LIBSVM in Popular Machine Learning Frameworks
Scikit-learn (with libsvm wrapper)
Scikit-learn’s sklearn.svm module wraps LIBSVM (and LIBLINEAR) for its SVM implementations. It provides a high-level API with integrated preprocessing, cross-validation, and pipeline support. The wrapper inherits LIBSVM’s performance and options.
Weka (libsvm package)
Weka, a popular machine learning toolkit in Java, includes a LIBSVM wrapper that allows using LIBSVM as a classifier or regressor within the Weka workbench. The wrapper supports the same kernel types and parameter tuning via the Weka Experimenter.
Other Kernel Method Libraries (e.g., SVM-Torch, GPDT)
SVM-Torch is a library for SVM learning using a multi-layer perceptron-like architecture. GPDT (Generalized Polynomial Discriminant Training) is another tool for kernel methods. These are less widely used than LIBSVM but offer alternative implementations for specialized research.
References and Further Reading
Original Papers and Documentation
The primary reference is the LIBSVM user manual and the accompanying paper: Chih-Chung Chang and Chih-Jen Lin, "LIBSVM: A library for support vector machines," ACM Transactions on Intelligent Systems and Technology, 2(3):27, 2011. The official website (https://www.csie.ntu.edu.tw/~cjlin/libsvm/) provides the source code, documentation, precompiled binaries, and FAQ.
Books and Tutorials on SVM
Standard textbooks cover the theoretical background: Bernhard Schölkopf and Alexander J. Smola, "Learning with Kernels" (MIT Press, 2002); Nello Cristianini and John Shawe-Taylor, "An Introduction to Support Vector Machines" (Cambridge University Press, 2000). The LIBSVM website also includes a list of tutorial slides and video lectures.