Machine Learning Lab  /  Supervised Learning  /  Support Vector MachinesClassification
Algorithm 07 · Supervised learning

Support Vector Machines (SVM)

SVM finds a boundary that separates groups of data. It tries to leave the biggest possible gap between the boundary and the closest points from each group.

Explore a simple two-class dataset. Change the margin and kernel idea, add points, and see how a separating line behaves.

BoundarySeparates classes
MarginSpace around boundary
KernelsHandle curved patterns

What is SVM?

Support Vector Machine is a supervised learning algorithm. It can classify data into groups and can also be used for regression.

Imagine drawing a line between red and blue dots. SVM looks for a line that separates them while keeping the nearest dots as far from the line as possible.

What are support vectors?

They are the training points closest to the decision boundary. These points help decide where the boundary should sit.

Moving a far-away point may not change the boundary much. Moving a support vector can change it a lot.

Interactive playground

Explore a separating boundary

This visual is a teaching simulation of a linear separator, not a full SVM optimiser. It helps you see the idea of a boundary and margin. Choose a pattern, adjust the margin display, and add points by clicking the chart.

Class AClass BIllustrative boundaryMargin guides
Click the chart to add a point. The demo boundary is illustrative.

Teaching note: the curved dataset is included to show why a straight line may not separate every pattern. A real SVM can use a kernel to create a non-linear boundary.

Decision boundary

For two features, a linear decision boundary can be written as:

w₁x₁ + w₂x₂ + b = 0

w controls the direction of the line. b shifts its position. The model predicts a class based on which side of the line a point falls on.

Maximum margin

SVM tries to make the gap between the two class edges as wide as possible.

Margin width = 2 / ||w||

The closest training points sit on or near the margin edges. Those points are the support vectors.

Hard margin and soft margin

TypeIdeaWhen useful
Hard marginRequires every training point to be correctly separated with no margin violations.Clean data that can be separated perfectly.
Soft marginAllows some points inside the margin or on the wrong side, with a penalty.Noisy or overlapping data.

The parameter C controls the trade-off. A large C penalises mistakes more strongly. A smaller C allows a wider, softer margin and may accept more training mistakes.

Worked numerical example

Suppose a trained linear SVM has the boundary:

2x₁ + x₂ − 5 = 0

For a new point A = (1, 2):

2(1) + 2 − 5 = −1

The result is negative, so the point is on the negative side of the boundary. For point B = (2, 3):

2(2) + 3 − 5 = 2

The result is positive, so B is on the positive side. The sign tells us the side of the boundary; the class names depend on how the model was trained.

Distance from a point to the boundary

Distance = |w₁x₁ + w₂x₂ + b| / √(w₁² + w₂²)

For this example, the denominator is √(2² + 1²) = √5. Point A is √5/5 units from the line.

What is a kernel?

A kernel helps SVM work with patterns that a straight line cannot separate. It lets the model compare points as if they were represented in a richer feature space.

  • Linear: straight boundary.
  • Polynomial: curved patterns with polynomial relationships.
  • RBF: flexible, smooth non-linear boundaries.
  • Sigmoid: another kernel option, used less often in many common workflows.

Feature scaling matters

SVM uses distances and dot products. If one feature ranges from 0 to 10 and another from 0 to 100,000, the large-scale feature can dominate.

Scaling features first often makes training more sensible. Use a pipeline so scaling is learned from training data only.

Important settings

SettingWhat it controls
CHow strongly the model penalises training errors.
kernelThe kind of boundary or similarity function used.
gammaFor RBF and some other kernels, how far the influence of one training point reaches.
degreePolynomial degree when using the polynomial kernel.

How do we evaluate SVM?

Use data that was not used during training.

  • Accuracy: share of predictions that are correct.
  • Precision: how many predicted positives were correct.
  • Recall: how many real positives were found.
  • F1-score: balance between precision and recall.
  • Confusion matrix: counts correct and incorrect predictions by class.

Strengths and limits

Strengths

  • Works well in many high-dimensional problems.
  • Can model linear and non-linear boundaries.
  • Maximum-margin idea can generalise well.

Limits

  • Feature scaling is often important.
  • Large datasets can take longer to train.
  • Kernel and parameter choices need testing.
  • Results can be harder to explain than a small decision tree.

Python implementation

This example scales features and trains an RBF-kernel SVM using scikit-learn.

from sklearn.datasets import make_moons
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, classification_report

X, y = make_moons(n_samples=500, noise=0.20, random_state=42)

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y
)

model = make_pipeline(
    StandardScaler(),
    SVC(kernel="rbf", C=1.0, gamma="scale")
)

model.fit(X_train, y_train)
predictions = model.predict(X_test)

print("Accuracy:", accuracy_score(y_test, predictions))
print(classification_report(y_test, predictions))

Install scikit-learn first if needed: pip install scikit-learn.

Where is SVM used?

Image classification

Sorting images into known categories using useful features.

Text classification

Classifying documents, topics, or message types.

Medical research

Helping classify measurements into groups for research.

Quality checks

Classifying products or readings as normal or unusual.

Knowledge check

Test what you learned

Answer each question, one at a time. You need to answer all 10 before submitting.

0 of 10 answered