An example using python bindings for SVM library, 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.
Introduction
LIBSVM is one of the most widely used SVM (Support Vector Machine) libraries, providing implementations for classification, regression, and distribution estimation. Its Python bindings through the libsvm package (or via scikit-learn which wraps LIBSVM internally) let you train and predict with SVMs directly in Python. This article demonstrates using the libsvm package directly with svmutil, as well as the more common scikit-learn wrapper.
Installation
Using LIBSVM Directly with svmutil
LIBSVM Parameter Options
Using List-Based Features
Cross-Validation
Saving and Loading Models
scikit-learn Wrapper (Recommended for Most Users)
scikit-learn's SVC uses LIBSVM internally with a more Pythonic API:
Grid Search for Best Parameters
Multi-Class Classification
LIBSVM handles multi-class problems automatically using one-vs-one:
Common Pitfalls
- Not scaling features: SVM performance is sensitive to feature scales. Features with large ranges dominate the distance calculation. Always normalize or standardize features before training. Use
sklearn.preprocessing.StandardScaleror LIBSVM's built-insvm-scaletool. - Using dictionary features incorrectly: LIBSVM's dictionary format uses 1-based indexing (
{1: val, 2: val}), not 0-based. Index 0 is reserved. Using 0 as a feature index produces incorrect results silently. - Choosing the wrong kernel: Linear kernels work well for high-dimensional data (text classification). RBF kernels work well for low-dimensional data. Starting with RBF and tuning C and gamma via grid search is a reasonable default strategy.
- Not setting
-qfor quiet mode: By default, LIBSVM prints training progress to stdout for every iteration. In production or notebooks, this floods the output. Always pass-qto suppress training messages. - Ignoring
gammawith RBF kernel: The default gamma (1/num_features) may not be optimal. Too small gamma makes the model underfit; too large makes it overfit. Always tune gamma alongside C using cross-validation.
Summary
- Install
libsvmfor direct LIBSVM Python bindings or use scikit-learn'sSVC(recommended) - LIBSVM format:
svm_train(labels, features, '-t 2 -c 1 -q')with dictionary or list features - Key parameters:
-t(kernel type),-c(regularization),-g(gamma),-d(degree) - Use
-v nfor n-fold cross-validation,svm_save_model/svm_load_modelfor persistence - scikit-learn's
SVCprovides a cleaner API with the same LIBSVM backend - Always scale features and tune C/gamma via grid search for optimal results
Related reading
- An understandable clusterization
- Anaconda showing this error , can''t train model properly
- Analysis of the output from tf.nn.dynamic_rnn tensorflow function
- Analyze a tensorflow graph or a .pb file on Tensorboard
- Anaconda export Environment file
- Anaconda ImportError /usr/lib64/libstdc.so.6 version GLIBCXX_3.4.21' not found
- Andrew Ng's Coursera Assignment - Training full Trigger Word detection model
- Android Mapview Merging overlapping markers into a new marker
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.