1. Home
  2. AI & Machine Learning
  3. Support Vector Machine (SVM)

Support Vector Machine (SVM)

Of all the lines that separate two classes, pick the one with the widest street between them. A kernel lifts data into 3D when no straight line works.

Interactive 3DIntermediate12 min readAI/MLUpdated

Drag to rotate · Right-drag to pan · Click, then scroll to zoom · Space play · ←→ step

What's happening

Pseudocode

    Try this in the 3D model

    • Press Train SVM. Three candidate lines separate the data. Why does the SVM pick none of them?
    • Count the glowing support vectors. Which points could you move without changing the line?
    • Press New points a few times. Is the margin always set by just two or three points?
    • Switch to the ring data. What shape does the flat plane become when the points drop back to the floor?

    The idea: the widest street

    Draw two groups of points on paper, say spam and normal email described by two numbers. Usually many straight lines separate them. Which one should a classifier use?

    A line that squeezes past some points is risky: a new point that is only slightly different could fall on the wrong side. A support vector machine chooses the line that stays as far as possible from both classes. Imagine the widest possible street between the two groups, and draw the boundary down the middle of it.

    The maths in one paragraph

    A line (or, in more dimensions, a hyperplane) is the set of points with w · x + b = 0. Label the classes y = +1 and y = −1. The SVM requires every point to lie on its own side, outside the street:

    yᵢ · (w · xᵢ + b) ≥ 1        for every training point

    The street then has width 2 / ‖w‖, so the SVM minimises ‖w‖² subject to those constraints. This is a convex quadratic problem with a single best solution, which needs no lucky starting point (unlike training a neural network).

    Support vectors

    At the optimum, only a few points touch the edges of the street. These are the support vectors. Every other point could move (without crossing the street) or disappear, and the boundary would stay exactly the same. The final model depends only on the support vectors, which makes it compact.

    When no line works: the kernel trick

    Points arranged as a ring around a cluster can’t be split by any line. But add a third feature, z = x² + y² (the squared distance from the centre). Lifted into 3D, the middle points stay low and the ring rises high, and now a flat plane separates them. Viewed from above, that plane is a circle on the original floor.

    Computing new features explicitly can be expensive. The SVM only ever needs dot products between points, so we can replace x · x′ with a kernel function K(x, x′) that equals a dot product in some richer space, without building that space. Popular kernels:

    Kernel K(x, x′) Boundary shape
    Linear x · x′ Straight line / flat plane
    Polynomial (x · x′ + c)ᵈ Curves of degree d
    RBF (Gaussian) exp(−γ ‖x − x′‖²) Any smooth shape

    Soft margins

    Real data overlaps and has outliers, so a perfect separating line may not exist. Soft-margin SVMs let some points break the rule, at a cost controlled by a parameter C. A large C means a hard margin with few mistakes allowed (risk of overfitting). A small C means a wider, more tolerant margin.

    Code

    from sklearn.svm import SVC
    from sklearn.datasets import make_circles
    
    # Linear SVM on separable data
    X = [[1, 2], [2, 3], [3, 3], [6, 5], [7, 8], [8, 6]]
    y = [-1, -1, -1, 1, 1, 1]
    clf = SVC(kernel="linear", C=1e6).fit(X, y)
    print(clf.support_vectors_)          # only the points on the margin
    print(clf.predict([[4, 4], [7, 6]]))
    
    # Ring data: a straight line fails, the RBF kernel succeeds
    Xc, yc = make_circles(n_samples=200, factor=0.3, noise=0.05, random_state=0)
    print(SVC(kernel="linear").fit(Xc, yc).score(Xc, yc))   # 0.69: a line cannot do it
    print(SVC(kernel="rbf").fit(Xc, yc).score(Xc, yc))      # 1.0

    SVM vs other classifiers

    SVM Logistic regression Neural network
    Boundary Widest margin Best probability fit Learned, any shape
    Output Class (scores) Probabilities Anything
    Curved boundaries With kernels With extra features Built in
    Best for Small/medium data, many features (text) Simple, explainable models Huge data (images, speech)

    Common mistakes

    • Forgetting to scale the features. SVMs measure distances, so a feature in rupees and one in kilograms must be put on similar scales first.
    • Using a hard margin on noisy data. One outlier can then drag the boundary. Use a soft margin with a sensible C.
    • Thinking the kernel actually builds the high-dimensional points. It doesn’t, and that is the whole trick.
    • Expecting probabilities. A plain SVM outputs a class and a score, not a calibrated probability.

    Complexity at a glance

    Case / operationTimeWhy
    Training (kernel SVM, n points)O(n²) to O(n³)Solving a quadratic optimisation problem.
    Training (linear SVM, modern solvers)≈ O(n · d)Coordinate descent and similar methods.
    PredictionO(s · d)s support vectors, d features (O(d) for a linear SVM).
    Extra spaceO(s · d) for the model

    Quick check

    Test yourself — pick an answer to see if you got it.

    1. What does an SVM maximise?

    2. What are support vectors?

    3. Why is the kernel trick useful?

    4. For a linear SVM the margin width is 2 / ‖w‖. To widen the margin, the optimiser…

    Saved only in this browser — no account needed.
    Spotted a mistake or a bug in the 3D model?

    Report a mistake

    in Support Vector Machine (SVM). Thank you — every report makes the lesson better for the next reader.

    We'll also include a link to the step of the 3D model you're on and your browser type, so we can reproduce it.