The kernel trick lets a straight-line classifier draw curved boundaries. It pretends the data lives in a bigger space where a flat cut works — then splits it there.
Where you meet it
- The moment a linear model is stuck near 50% on data that clearly has shape — a blob inside a ring.
SVC(kernel="rbf")orkernel="poly"in scikit-learn — the default SVM is already kernelised.- Any "these classes aren't linearly separable" slide in your ML course.
- Feature engineering in disguise: instead of hand-adding
x²,x·y, … you let the kernel invent them.
What it does
Some data no straight line can split. Instead of giving up on lines, the kernel maps every point into a higher-dimensional space where a flat boundary can separate the classes — and finds it there. Back in your original 2D picture, that flat cut shows up as a curve.
The clever part isn't the bigger space — it's that you never have to build it. The kernel jumps straight to the one number the SVM actually needs.
How it works
An SVM can be written so it only ever touches the data through dot products x · x′ — a similarity score between two points. A kernel K(x, x′) is a shortcut that returns the dot product in the bigger space, φ(x) · φ(x′), without ever computing the new coordinates φ(x). The whole trick is to swap every x · x′ for K(x, x′).
# an SVM can be written so it only touches the data
# through dot products x · x′
K(x, x′) = φ(x) · φ(x′) # the dot product in a bigger space …
# … but computed WITHOUT ever building φ(x)
# a lift that separates the ring: z = x² + y² (distance-from-centre²)
# inner blob → low, outer ring → high → a flat plane cuts between
That last line is the picture the tool animates: raise each point by its squared distance from the centre and the ring-in-blob becomes a bowl — inner points low, outer points high — so a single flat plane slices them apart. Drop that plane back to 2D and it is your circle. The kernel menu just changes the shape you get: linear keeps a line, poly bends it into a conic (a circle here), RBF wraps a soft bump around each point — the flexible default.
Watch out
- Scale your features first. RBF and poly measure distances between points, so mismatched units quietly distort every kernel value. Standardise before you fit.
- RBF's
γ(gamma) is a sharpness dial. Too large and each point only "sees" itself — a wiggly boundary that memorises the training set (overfitting). Too small and the boundary is too smooth to fit. It has to be tuned. - Polynomial degree does the same: higher degree means more flexibility, which means more room to overfit.
- More power isn't free — a curvier boundary can fit noise as happily as signal. Match the kernel to the shape you actually see. (There's also a separate knob,
C, that lets the model tolerate a few misclassified points — a different dial from the kernel.)
Don't bend the line — lift the data until a straight cut fits, then let it fall back as a curve.
Go deeper
Der Kernel-Trick lässt einen Geraden-Klassifikator gebogene Grenzen ziehen. Er tut so, als läge die Daten in einem größeren Raum, wo ein flacher Schnitt reicht — und trennt sie dort.
Wo es vorkommt
- Der Moment, in dem ein lineares Modell bei rund 50% festhängt, obwohl die Daten klar eine Form haben — ein Klumpen in einem Ring.
SVC(kernel="rbf")oderkernel="poly"in scikit-learn — die Standard-SVM ist schon kernelisiert.- Jede "diese Klassen sind nicht linear trennbar"-Folie in deiner ML-Vorlesung.
- Feature-Engineering in Verkleidung: statt
x²,x·y, … von Hand zu ergänzen, lässt du den Kernel sie erfinden.
Was es tut
Manche Daten trennt keine Gerade. Statt Geraden aufzugeben, hebt der Kernel jeden Punkt in einen höherdimensionalen Raum, in dem eine flache Grenze die Klassen doch trennt — und findet sie dort. Zurück im ursprünglichen 2D-Bild erscheint dieser flache Schnitt als Kurve.
Das Clevere ist nicht der größere Raum — es ist, dass du ihn nie bauen musst. Der Kernel springt direkt zu der einen Zahl, die die SVM wirklich braucht.
Wie es funktioniert
Eine SVM lässt sich so schreiben, dass sie die Daten nur über Skalarprodukte x · x′ berührt — ein Ähnlichkeitswert zwischen zwei Punkten. Ein Kernel K(x, x′) ist eine Abkürzung, die das Skalarprodukt im größeren Raum liefert, φ(x) · φ(x′), ohne die neuen Koordinaten φ(x) je auszurechnen. Der ganze Trick: jedes x · x′ durch K(x, x′) ersetzen.
# eine SVM lässt sich so schreiben, dass sie die Daten
# nur über Skalarprodukte x · x′ berührt
K(x, x′) = φ(x) · φ(x′) # das Skalarprodukt im größeren Raum …
# … aber berechnet OHNE φ(x) je zu bauen
# ein Lift, der den Ring trennt: z = x² + y² (Abstand-vom-Zentrum²)
# innerer Klumpen → tief, äußerer Ring → hoch → eine flache Ebene trennt
Die letzte Zeile ist das Bild, das das Tool animiert: heb jeden Punkt um seinen quadrierten Abstand vom Zentrum, und aus Ring-um-Klumpen wird eine Schale — innere Punkte tief, äußere hoch — sodass eine einzige flache Ebene beide zerschneidet. Klapp die Ebene zurück in 2D, und sie ist dein Kreis. Das Kernel-Menü ändert nur die Form: linear bleibt eine Gerade, poly biegt sie zu einem Kegelschnitt (hier ein Kreis), RBF legt eine weiche Blase um jeden Punkt — der flexible Standard.
Worauf achten
- Erst die Features skalieren. RBF und poly messen Abstände zwischen Punkten — ungleiche Einheiten verzerren still jeden Kernel-Wert. Vor dem Fit standardisieren.
- RBFs
γ(Gamma) ist ein Schärfe-Regler. Zu groß, und jeder Punkt "sieht" nur sich selbst — eine zappelige Grenze, die den Trainingssatz auswendig lernt (Overfitting). Zu klein, und die Grenze ist zu glatt zum Passen. Muss getunt werden. - Der Polynom-Grad tut dasselbe: höherer Grad heißt mehr Flexibilität, also mehr Raum zum Overfitten.
- Mehr Kraft ist nicht umsonst — eine kurvigere Grenze passt sich Rauschen genauso gern an wie dem Signal. Wähl den Kernel nach der Form, die du wirklich siehst. (Es gibt auch einen separaten Regler
C, der ein paar falsch zugeordnete Punkte erlaubt — ein anderer Regler als der Kernel.)
Biege nicht die Linie — hebe die Daten, bis ein gerader Schnitt passt, und lass ihn als Kurve zurückfallen.