A hospital diagnoses a disease from medical test results. Some patterns a straight line can't separate — so we reach for Support Vector Machines and the kernel trick. Switch kernels and watch the boundary bend to fit.
An SVM doesn't just separate the classes — it finds the boundary with the widest possible gap (margin) between them. The closest points that touch the margin are the support vectors; they alone define the boundary.
When classes can't be split by a straight line, a kernel lifts the data into a higher dimension where they can be split — without ever computing those coordinates. The RBF kernel wraps flexible, curved boundaries around clusters; γ sets how tightly.
Each dot is a patient plotted by two test results — healthy or diseased. Switch the kernel and data shape and watch the decision region (shaded) and accuracy change. With concentric data, a linear boundary fails and only the RBF kernel separates the two.
Two clear explainers from StatQuest with Josh Starmer — the main ideas of SVMs and how the RBF kernel works.
Videos are embedded from YouTube and belong to their creators. If a frame is blank, your network may block YouTube — search the titles on youtube.com.
Train SVMs with different kernels and pick the best by cross-validated accuracy.
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
X, y = load_breast_cancer(return_X_y=True) # or pd.read_csv(...)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.25, random_state=1)
sc = StandardScaler().fit(X_tr) # SVMs need scaled features!
X_tr, X_te = sc.transform(X_tr), sc.transform(X_te)
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
for k in ["linear", "poly", "rbf"]:
model = SVC(kernel=k, C=1.0, gamma="scale").fit(X_tr, y_tr)
pred = model.predict(X_te)
print(k, "accuracy:", round(accuracy_score(y_te, pred), 3))
print(confusion_matrix(y_te, pred))
from sklearn.model_selection import GridSearchCV
grid = {"C":[0.1,1,10], "gamma":["scale",0.01,0.1,1], "kernel":["rbf"]}
gs = GridSearchCV(SVC(), grid, cv=5, scoring="accuracy").fit(X_tr, y_tr)
print("best params:", gs.best_params_)
print("best CV accuracy:", gs.best_score_)
# Choose the kernel + C + gamma with the best validated accuracy.
SVM classifiers were built for disease diagnosis with linear, polynomial and RBF kernels. After scaling and cross-validated tuning of C and γ, the RBF kernel gave the best accuracy on non-linearly separable data and was selected as the diagnostic model — demonstrating CO3.