CadetX
CX Learn | Complete Machine Learning Guide
Scikit-Learn · Python
CadetX logo CadetX CX Learn
ML-101 · Module 9 Intermediate about 35 minutes 10 Lessons Prereq: Modules 1 to 8

Support Vector Machines (SVM)

A Support Vector Machine (SVM) doesn't just draw any line between two groups. It draws the line with the widest possible gap on both sides. With a clever trick, it can also draw curves, circles and complex shapes. SVMs were among the most powerful algorithms in machine learning for years, and they're still excellent on many problems today.

  • Level: Intermediate
  • Time: about 35 minutes
  • Needs: Modules 1 to 8

By the end of this module you will be able to

  • Explain what an SVM looks for when it draws a boundary
  • Describe the margin and the support vectors
  • Explain the soft margin and how the setting C controls it
  • Explain the kernel trick in simple terms
  • Choose between linear, polynomial and RBF kernels, and tune gamma
  • Know when an SVM is a good choice and when it isn't
  • Build and tune an SVM that reads handwritten digits in scikit-learn

9.1What is a Support Vector Machine?

Imagine two villages on either side of a field, and the council wants to build a road between them. They could build a narrow lane squeezed right up against one village's houses. Or they could build the widest possible road, running right down the middle, with as much space as possible on both sides. The second option is safer: there's plenty of room for error.

An SVM does exactly that with data. For a classification problem, it looks for the boundary that separates the classes with the biggest gap between them.

SVMs in one sentence

A Support Vector Machine finds the boundary that separates the classes with the widest possible margin, so new data is less likely to land on the wrong side.

SVMs are mainly used for classification, though there's also a version for regression (lesson 9.7). They work especially well when there are lots of features, such as text or images.

9.2Which line is best?

When two groups are cleanly separated, there are endless lines that could divide them. Logistic regression (Module 4) picks one based on probabilities. An SVM asks a different question: which line gives the most breathing room?

Two groups of points with three possible dividing lines. Two lines pass very close to some points; the middle line leaves the widest gap on both sides.ABLines A and BSeparate the groups, but passvery close to some points.A new point could easily landon the wrong side.Green lineSits right in the middle,as far as possible from bothgroups. This is the line anSVM chooses.
Figure 1. All three lines separate the blue and amber groups perfectly on this data. But lines A and B hug one group. The green line stays as far from both groups as possible, so it's the safest choice for new data.

The idea is simple: if the boundary is far from all the training points, then a new point that's slightly different from what we've seen is still likely to land on the correct side. A wide gap means better generalisation, the thing we care about most (Module 5).

9.3The margin and support vectors

The SVM boundary with its margin. The solid line is the decision boundary. Dashed lines on each side mark the edges of the margin. The circled points touching the dashed lines are the support vectors.Decision boundaryThe solid line down the middleMarginThe shaded "road" betweenthe dashed lines. The SVMmakes it as wide as possible.Support vectors (2)The circled points on theedge of the road. Only thesedecide where the line goes.
Figure 2. The parts of an SVM. The model maximises the width of the shaded margin. Only the circled points, the support vectors, touch its edges.
TermMeaning
Decision boundary (hyperplane)The line that separates the classes. With 2 features it's a line, with 3 it's a flat plane, and with more it's called a hyperplane
MarginThe gap between the boundary and the nearest points on each side. The SVM makes it as wide as possible
Support vectorsThe training points closest to the boundary, sitting on the edge of the margin

Here's the surprising part: only the support vectors matter. You could delete every other point, retrain, and get exactly the same boundary. They "support" the margin like tent poles hold up a tent, which is where the algorithm gets its name.

Why this is useful

Because the boundary depends only on the tricky points near the border, SVMs aren't distracted by the thousands of "easy" examples far away. That focus is one reason they perform well even with lots of features.

9.4The soft margin and the C setting

Real data is rarely perfectly separable. There's usually some overlap: a few customers who "should" have stayed but left anyway. A strict SVM that demands every point is on the correct side would fail, or draw a terrible boundary to fit a few odd points.

The solution is a soft margin: the SVM is allowed to let some points sit inside the margin, or even on the wrong side, but it pays a penalty for each one. The setting C controls how big that penalty is.

Try it: change C

Small C (e.g. 0.01)Large C (e.g. 100)
Attitude to mistakesRelaxed: "a few errors are fine"Strict: "avoid every training error"
MarginWideNarrow
Support vectorsManyFew
RiskUnderfittingOverfitting to odd points

C is a hyperparameter, just like k in KNN or max_depth in trees. As always, choose it with cross-validation. Good values to try are 0.1, 1, 10 and 100.

9.5The kernel trick

So far, SVMs draw straight lines. But what if the data looks like this: one group in the middle, the other on both sides?

Left: points on a single line, with one group in the middle and the other on both sides. No single cut can separate them. Right: after adding a second feature, x squared, the outer points rise up and a straight line separates the groups.One feature: xNo single cut separates blue from amber→add x²Two features: x and x²A straight line works!xx²
Figure 3. On the left, with one feature, no single cut can separate the groups. On the right, we add a second feature, x², that is calculated from the first. Now the outer amber points rise above the blue ones, and a straight line separates them.

That's the key idea: data that can't be separated by a straight line might become separable if you look at it in more dimensions. A straight line in the new, bigger space becomes a curve when you bring it back to the original space.

Creating all those extra features could be slow. The kernel trick is a mathematical shortcut that lets the SVM work as if it had added the extra dimensions, without ever actually creating them. You don't need to know the maths; you just choose a kernel.

Ring-shaped data. A linear SVM cannot separate the inner group from the outer ring. An RBF-kernel SVM draws a circular boundary that separates them well.Linear kernelTraining accuracy: 59%RBF kernelTraining accuracy: 100%
Figure 4. Ring-shaped data. A linear SVM (left) has no hope: any straight line cuts through both groups. The RBF kernel (right) draws a circle-shaped boundary around the inner group.

An everyday picture

Imagine red and blue marbles mixed on a table, with the blue ones in the middle. You can't separate them with a ruler. Now imagine hitting the table so the blue ones jump up into the air. For a split second, you could slide a sheet of paper between them. That's what a kernel does: it "lifts" the data into a new dimension where a flat cut works.

9.6Choosing a kernel

KernelBoundary shapeWhen to use itscikit-learn
LinearStraight line (hyperplane)Many features, such as text; or data that's nearly separable by a line. FastestSVC(kernel="linear")
PolynomialCurves of a set complexity (degree)When you expect interactions between features. Less commonSVC(kernel="poly", degree=3)
RBF (radial basis function)Flexible curves, circles, islandsThe default and best starting point for most problemsSVC(kernel="rbf")

Gamma: how curvy can the RBF boundary get?

The RBF kernel has a second setting, gamma. It controls how far each training point's influence reaches.

  • Small gamma: each point influences a wide area, so the boundary is smooth and simple.
  • Large gamma: each point only influences a tiny area around itself, so the boundary can wrap tightly around individual points.
An RBF SVM on moon-shaped data with three gamma values. Small gamma gives an almost straight boundary. Medium gamma gives a smooth curve. Large gamma draws tight bubbles around individual points.gamma = 0.3 (small)Too simple: underfitsgamma = 3 (medium)Just rightgamma = 300 (large)Bubbles: overfits
Figure 5. The same moon-shaped data with three values of gamma. Too small and the boundary is almost straight (underfitting). Too large and it draws little bubbles around single points (overfitting). The middle value captures the real shape.

C and gamma work together, so it's normal to tune them at the same time with GridSearchCV, just as you'll see in lesson 9.8.

Always scale your features

SVMs measure distances between points, just like KNN in Module 6. Features with big numbers will dominate unless you scale them first. Put a scaler and the SVM together in a Pipeline.

9.7SVMs for regression

SVMs can predict numbers too, using Support Vector Regression (SVR). It flips the idea around. Instead of the widest gap between groups, it fits a "tube" around a line or curve and tries to fit as many points as possible inside the tube.

  • Points inside the tube count as "close enough": they cost nothing.
  • Only points outside the tube count as errors, and they become the support vectors.
  • The width of the tube is set with a setting called epsilon.

In scikit-learn, use SVR in place of SVC. It supports the same kernels. In practice, for most number-prediction problems on table-shaped data, the gradient boosting models from Module 8 are usually a stronger choice.

9.8Reading handwriting with an SVM in Python

Before deep learning took over, SVMs were famous for recognising handwritten digits. scikit-learn includes a dataset of 1,797 small images of handwritten digits. Each image is an 8 × 8 grid of pixels, so each digit has 64 features: one for how dark each pixel is.

Ten example handwritten digits from 0 to 9, each an 8 by 8 grid of grey pixels.0123456789
Figure 6. One example of each digit from the dataset, drawn from its real pixel values. Each image is just 64 numbers between 0 (black) and 16 (white).
digits_svm.py
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

# 1. 1,797 handwritten digits, each an 8x8 image = 64 pixel features
X, y = load_digits(return_X_y=True)
print("Images:", X.shape[0], "| Features per image:", X.shape[1])
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y)

# 2. Compare a linear SVM with an RBF (curved) SVM
#    Pixels run from 0 to 16, so min-max scaling puts them all on 0 to 1
for kernel in ["linear", "rbf"]:
    svm = Pipeline([("scale", MinMaxScaler()),
                    ("svm", SVC(kernel=kernel))])
    svm.fit(X_train, y_train)
    print(f"{kernel:6} kernel accuracy: {svm.score(X_test, y_test):.3f}")

# 3. Tune C and gamma for the RBF kernel with cross-validation
grid = GridSearchCV(
    Pipeline([("scale", MinMaxScaler()), ("svm", SVC(kernel="rbf"))]),
    param_grid={"svm__C": [0.1, 1, 10, 100],
                "svm__gamma": ["scale", 0.01, 0.1, 1]},
    cv=5)
grid.fit(X_train, y_train)
print("Best settings:", grid.best_params_)
print("Cross-validation accuracy:", round(grid.best_score_, 3))

# 4. Final test
pred = grid.predict(X_test)
print("Test accuracy:", round(accuracy_score(y_test, pred), 3))
print("Mistakes:", (pred != y_test).sum(), "out of", len(y_test))
print("Support vectors used:", grid.best_estimator_["svm"].n_support_.sum())
Output
Images: 1797 | Features per image: 64
linear kernel accuracy: 0.984
rbf    kernel accuracy: 0.991
Best settings: {'svm__C': 10, 'svm__gamma': 'scale'}
Cross-validation accuracy: 0.989
Test accuracy: 0.991
Mistakes: 4 out of 450
Support vectors used: 561

What the output tells us

  • Even a straight-line SVM does brilliantly (98.4%), because with 64 features there's lots of room to separate the digits.
  • The RBF kernel does even better (99.1%), because it can draw curved boundaries around the different ways people write each digit.
  • GridSearchCV tried 16 combinations of C and gamma and picked C = 10 with gamma = "scale" (scikit-learn's sensible automatic value).
  • Only 4 mistakes in 450 test images. That's a 99.1% test accuracy, very close to the 98.9% cross-validation score, so it isn't a lucky split.
  • 561 support vectors out of 1,347 training images. These are the "tricky" digits, like a 1 that looks a bit like a 7, which define the boundaries.

How does an SVM handle 10 classes?

An SVM naturally separates two classes. For more, scikit-learn automatically trains one SVM for every pair of classes (0 vs 1, 0 vs 2, and so on: 45 pairs for 10 digits), and the digit that wins the most "matches" is the prediction. This is called one-vs-one.

9.9Strengths, weaknesses and real-world uses

StrengthsWeaknesses
Very accurate on small and medium datasetsSlow on big datasets: training time grows fast beyond roughly 100,000 rows
Excellent with many features, even more features than rows (text, genetics)Must scale features
Flexible: kernels handle complex, curved boundariesNeeds tuning of C and gamma together
Resistant to overfitting when C is chosen wellNo probabilities by default (set probability=True, which is slower)
Memory efficient: only the support vectors are needed to predictHard to explain, especially with the RBF kernel
AreaExample
Text classificationSorting news articles by topic, spam detection, sentiment analysis of reviews
Image recognitionHandwriting recognition, face detection (before deep learning)
Healthcare and biologyClassifying diseases from gene data, where there are thousands of features but few patients
FinanceCredit risk and fraud detection on medium-sized datasets
ManufacturingSpotting faulty products from sensor readings

Where do SVMs fit today?

For very large datasets, gradient boosting (Module 8) and neural networks have mostly taken over. But when you have a few thousand rows and lots of features, an SVM is still one of the best tools to try, and it often beats more fashionable methods.

SummaryKey takeaways

  • An SVM finds the boundary with the widest margin between classes.
  • The support vectors are the points on the edge of the margin; only they decide where the boundary goes.
  • A soft margin allows some mistakes. Small C = wide margin, more tolerant; large C = narrow margin, stricter.
  • The kernel trick lets an SVM draw curved boundaries by working in extra dimensions without creating them.
  • RBF is the usual default kernel; linear is great for text and very many features.
  • Gamma controls how curvy an RBF boundary can get: too large overfits.
  • Always scale features and tune C and gamma together with cross-validation.
  • SVMs shine on small to medium datasets with many features, but are slow on very large data.

Check your understanding

Your machine learning roadmap

  1. 01
    Introduction to Machine LearningWhat ML is and how it works
  2. 02
    Preparing Data for Machine LearningFeatures, encoding, scaling, train/test split
  3. 03
    Linear RegressionPredicting numbers
  4. 04
    Logistic RegressionPredicting yes or no
  5. 05
    Evaluating ModelsAccuracy, precision, recall, overfitting, cross-validation
  6. 06
    K-Nearest NeighboursLearning from similar examples
  7. 07
    Decision TreesFlowcharts that learn
  8. 08
    Random Forest and Gradient BoostingMany models working together
  9. 09
    Support Vector MachinesYou are here
  10. 10
    Naive BayesProbability-based classification
  11. 11
    Clustering with K-MeansFinding groups without labels
  12. 12
    Dimensionality Reduction with PCASimplifying big datasets
  13. 13
    Capstone ProjectBuild and present a full ML project