When two groups of data points need to be separated, many lines could do the job. Support Vector Machine (SVM) asks a sharper question: which line separates them with the widest possible gap? That simple idea makes SVM one of the most powerful classical machine learning algorithms. This guide explains what SVM is, how it works, why and when to use it, with a worked example and Python code.
Table of Contents
What Is SVM?
Support Vector Machine (SVM) is a supervised machine learning algorithm used mainly for classification, and also for regression (SVR) and outlier detection. It finds the boundary — called a hyperplane — that separates classes with the maximum margin, the largest possible distance to the nearest points of each class.
Using the kernel trick, SVM can also draw curved, non-linear boundaries, which makes it effective on complex data with many features.
A Simple Analogy
Imagine laying a road between two villages. Many routes would avoid the houses, but the safest road runs right down the middle with the widest clearance on both sides. Only the houses closest to the road decide where it goes — the ones further away do not matter. In SVM, the road is the decision boundary, the clearance is the margin, and the closest houses are the support vectors.
Key Concepts
Hyperplane
The decision boundary. In 2 dimensions it is a line, in 3 dimensions a plane, and in more dimensions a “hyperplane”. It is written as w · x + b = 0, where w is a weight vector (the direction) and b is the bias (the offset). A point is classified by the sign of f(x) = w · x + b: positive → one class, negative → the other.
Margin
The gap between the hyperplane and the closest points of each class. The two edges of the margin are the lines where f(x) = +1 and f(x) = −1, and the margin width is 2 ÷ ‖w‖. SVM maximises this width — a wider margin usually means better generalisation to new data.
Support vectors
The training points that lie on or inside the margin. They alone define the boundary — remove any other point and the model does not change. This is where the algorithm gets its name.
How SVM Works
- Scale the features so they contribute equally to distances.
- Search for the hyperplane that separates the classes while keeping the margin as wide as possible.
- Allow some violations (soft margin) when the data overlaps, controlled by the parameter C.
- If the data is not linearly separable, use a kernel to map it into a higher-dimensional space where a flat boundary works.
- Classify new points by which side of the hyperplane they fall on.
Mathematically, training is a convex optimisation problem (minimising ‖w‖ plus a penalty for errors, the hinge loss), so it always finds the single best solution — no random starting points or local minima.
Worked Example by Hand
Suppose an SVM trained on two features has learned w = (1, 1) and b = −6, so f(x) = x₁ + x₂ − 6. (Illustrative numbers.)
| Point (x₁, x₂) | f(x) = x₁ + x₂ − 6 | Prediction | Note |
|---|---|---|---|
| (1, 2) | −3 | Class −1 | Well inside its side |
| (2, 3) | −1 | Class −1 | Exactly on the margin edge → a support vector |
| (4, 4) | +2 | Class +1 | Beyond the margin |
| (5, 3) | +2 | Class +1 | Beyond the margin |
Margin width = 2 ÷ ‖w‖ = 2 ÷ √(1² + 1²) = 2 ÷ 1.414 ≈ 1.41 units. A model with smaller ‖w‖ would have a wider margin — that is precisely what SVM optimises.
Hard Margin vs Soft Margin (C)
- Hard margin — no point may cross the margin. Works only if the data is perfectly separable and is very sensitive to outliers.
- Soft margin — allows some points inside the margin or on the wrong side, with a penalty. This is what is used in practice.
The parameter C controls the trade-off:
| C value | Behaviour | Risk |
|---|---|---|
| Small C (e.g. 0.01) | Wider margin, tolerates more misclassified training points | Underfitting |
| Large C (e.g. 100) | Narrower margin, tries hard to classify every training point | Overfitting to noise |
The Kernel Trick
Some data cannot be separated by a straight line — for example, one class forming a ring around the other. The kernel trick lets SVM behave as if the data were mapped into a higher-dimensional space where a flat hyperplane separates it, without actually computing that mapping. It only needs a kernel function measuring similarity between pairs of points.
| Kernel | Boundary shape | When to use |
|---|---|---|
| Linear | Straight line / flat plane | Many features (e.g. text), data that is roughly linearly separable; fastest |
| RBF (Gaussian) | Flexible, curved regions | Good default for non-linear problems |
| Polynomial | Curved, polynomial-shaped | When feature interactions of a certain degree matter |
| Sigmoid | Neural-network-like | Rarely used in practice |
Key Hyperparameters: C, Kernel, Gamma
- kernel — linear, rbf, poly or sigmoid. Try linear and rbf first.
- C — regularisation strength (see above). Typical search: 0.1, 1, 10, 100.
- gamma (RBF/poly) — how far the influence of one training point reaches. High gamma → each point affects only a small area → wiggly boundary, overfitting. Low gamma → smoother boundary, risk of underfitting.
- class_weight=”balanced” — useful when one class is much rarer.
C and gamma interact, so tune them together with a grid search and cross-validation, as in the Python example below.
SVM for Regression (SVR)
Support Vector Regression fits a tube of width ε (epsilon) around the prediction line and ignores errors inside the tube; only points outside it become support vectors and affect the model. With an RBF kernel, SVR can model non-linear relationships. For simple linear numeric prediction, start with linear regression as a baseline.
Why Use SVM?
- High accuracy on small and medium datasets, often competitive with more complex models.
- Works well with many features — even when there are more features than samples, as in text and genetics data.
- Robust generalisation — maximising the margin reduces overfitting.
- Handles non-linear problems through kernels.
- Memory-efficient predictions — the model depends only on the support vectors.
- Unique, stable solution — convex optimisation means the same data always gives the same model.
Real-World Applications
| Area | Use of SVM |
|---|---|
| Text classification | Spam filtering, sentiment analysis, news categorisation (linear SVM on TF-IDF features — see NLP) |
| Image classification | Classic face detection and handwriting recognition on extracted features |
| Bioinformatics | Classifying genes and proteins from high-dimensional data |
| Healthcare | Disease classification from medical measurements |
| Finance | Credit-risk classification, fraud detection |
| Manufacturing | Fault detection from sensor readings |
| Anomaly detection | One-Class SVM learns what “normal” looks like |
Python Example: Tumour Classification
This example uses the breast cancer dataset built into scikit-learn (569 samples, 30 features, 2 classes), so it runs as-is. It tries linear and RBF kernels with different C and gamma values using a grid search.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
# 1. Load a built-in dataset: 569 tumours, 30 features, 2 classes
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42, stratify=y)
# 2. Pipeline: scaling is essential for SVM
pipe = Pipeline([("scale", StandardScaler()), ("svm", SVC())])
# 3. Try linear and RBF kernels with different C and gamma values
param_grid = [
{"svm__kernel": ["linear"], "svm__C": [0.01, 0.1, 1, 10]},
{"svm__kernel": ["rbf"], "svm__C": [0.1, 1, 10, 100],
"svm__gamma": ["scale", 0.01, 0.001]},
]
search = GridSearchCV(pipe, param_grid, cv=5, scoring="f1")
search.fit(X_train, y_train)
print("Best settings:", search.best_params_)
print("Best CV F1 :", round(search.best_score_, 3))
# 4. Evaluate the best model once on the test set
pred = search.predict(X_test)
print("Test accuracy:", round(accuracy_score(y_test, pred), 3))
print(confusion_matrix(y_test, pred))
print(classification_report(y_test, pred))
# 5. How many support vectors did the model keep?
print("Support vectors:", search.best_estimator_.named_steps["svm"].n_support_)Output (abridged): best settings — RBF kernel, C = 10, gamma = 0.01; cross-validation F1 0.984; test accuracy 0.982 with just 2 errors out of 114; support vectors: 25 and 27 per class.
Notice that only 52 of the 455 training points are support vectors — the rest do not affect the boundary at all. Need probabilities? Use SVC(probability=True), which adds a calibration step (slower).
Advantages and Limitations
| Advantages | Limitations |
|---|---|
| Effective with many features | Slow to train on very large datasets (hundreds of thousands of rows) |
| Maximum margin helps generalisation | Needs careful scaling and tuning of C and gamma |
| Flexible with kernels | Hard to interpret with non-linear kernels |
| Memory-efficient model (support vectors only) | No probabilities by default |
| Stable, unique solution | Sensitive to noisy, overlapping classes; less suited to very large tabular datasets than gradient boosting |
For very large linear problems, LinearSVC or SGDClassifier in scikit-learn scale much better than SVC.
SVM vs Other Algorithms
| SVM | Logistic regression | KNN | Random forest | |
|---|---|---|---|---|
| Boundary | Max-margin; linear or kernel | Linear | Flexible, local | Non-linear, axis-aligned splits |
| Output | Class (probability optional) | Probability | Class / average | Class + probability |
| Needs scaling? | Yes | Helpful | Yes | No |
| Large datasets | Slow (kernel) | Fast | Slow predictions | Good |
| Interpretability | Low (kernel) | High | Medium | Medium |
| Sweet spot | High-dimensional, small–medium data | Explainable yes/no | Similarity tasks | Tabular business data |
Related reads: logistic regression and KNN.
How to Learn SVM
- Understand classification and logistic regression first
- Learn vectors, dot products and the idea of distance from a line
- Train a linear SVM in scikit-learn, then visualise its margin on 2D data
- Try RBF kernels and see how C and gamma change the boundary
- Use GridSearchCV with cross-validation to tune
- Apply it to a text classification or medical dataset project
Reference: the scikit-learn SVM guide.
Learn it in Jaipur: classification, model evaluation and hyperparameter tuning with scikit-learn are part of the machine learning module in our AI & Machine Learning course, and our Data Science course applies them to real datasets. 6 months, 2-hour practical classes, IBM certificate on completion — in Jhotwara or live online.
Frequently Asked Questions
What does SVM stand for?
Support Vector Machine. “Support vectors” are the training points closest to the decision boundary that define it.
Is SVM supervised or unsupervised?
Standard SVM is supervised. One-Class SVM is an unsupervised variant used for anomaly detection.
Which kernel should I use?
Start with linear for text or very high-dimensional data, and RBF for most other problems. Compare them with cross-validation.
Can SVM do multi-class classification?
Yes. SVM is binary at its core, but scikit-learn handles multiple classes automatically by combining several binary SVMs (one-vs-one).
Is SVM still relevant with deep learning?
Yes, for small and medium datasets, high-dimensional data and situations where you need a strong model without large amounts of data or GPUs. For images, audio and huge datasets, deep learning usually wins.
Learn machine learning hands-on in Jaipur
Explore our AI & Machine Learning, Data Science and Predictive AI courses. Send an enquiry or WhatsApp us on +91 82096 15423. Also read: What Is Naive Bayes?, What Is KNN? and What Is Logistic Regression?
