DSA LABData Science Applications
Experiment 08 · CO3

Drawing the Line
Between Sick & Well

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.

Course IT416
Outcome CO3
Model SVM
Kernels Linear · RBF

Diagnosis by decision boundary

LIVE KERNEL
0
Kernels
0
Test features
0
Data shapes
0
Concept videos
01

Lab Manual

Problem
A healthcare organisation wants to diagnose whether a patient has a particular disease from medical test results. Examine the dataset, choose suitable classification techniques with different kernel options if needed, compare their performance, and identify the best model for accurate diagnosis.
Aim
To build a Support Vector Machine classifier for disease diagnosis, explore linear and non-linear (RBF, polynomial) kernels, and select the kernel that best separates the classes.
Objectives
  • Understand the maximal-margin idea behind SVMs.
  • See why some data needs a non-linear kernel (the kernel trick).
  • Tune C (margin softness) and γ (RBF reach).
  • Compare kernels by accuracy and pick the best diagnostic model.
Software
Python 3.x · Jupyter / Colab · Pandas, scikit-learn, Matplotlib.
Dataset
A medical diagnosis dataset (e.g. UCI Breast Cancer Wisconsin, Pima Indians Diabetes) — test measurements with a disease/no-disease label.
Outcome
CO3 — building, comparing and selecting a classification model.
02

The Kernel Idea, in One Minute

Maximal margin

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.

The kernel trick

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.

For diagnosis: test results rarely separate along a straight line. The right kernel can lift accuracy dramatically — but too flexible a kernel (huge γ) overfits. Choosing the kernel is the modelling decision.
03

Kernel Playground

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.

SVM decision boundaryshaded = predicted region
Kernel
Data
γ (RBF reach)1.4
Training accuracy
Try this: with Concentric data, switch to Linear — accuracy collapses toward a coin-flip, because no straight line can wrap a ring. Switch back to RBF and it jumps back up.
04

Watch the Concepts

Two clear explainers from StatQuest with Josh Starmer — the main ideas of SVMs and how the RBF kernel works.

StatQuest · Part 1Support Vector Machines: Main IdeasMaximal margins, soft margins and support vectors.
StatQuest · Part 3The Radial (RBF) KernelHow the RBF kernel bends boundaries to fit non-linear data.

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.

05

The Program

Train SVMs with different kernels and pick the best by cross-validated accuracy.

1 — load_scale.py
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)
2 — compare_kernels.py
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))
3 — tune_and_select.py
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.
06

Procedure & Result

Steps followed

1 · Load
Read the diagnosis dataset.
2 · Scale
Standardise features (essential for SVMs).
3 · Train
Fit SVMs with linear, poly, RBF kernels.
4 · Tune
Grid-search C and γ with cross-validation.
5 · Compare
Accuracy & confusion matrix per kernel.
6 · Select
Pick the best diagnostic model.

Result

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.

Conclusion: The kernel is the key choice. A linear kernel is fine when classes separate cleanly; when they don't, the RBF kernel's flexible boundary wins — provided C and γ are tuned to avoid overfitting. Always scale features first.
07

Viva Questions

What is a Support Vector Machine?
A supervised classifier that finds the hyperplane separating classes with the maximum margin. The points closest to the boundary — the support vectors — determine it.
What is the kernel trick?
A way to make data separable by implicitly mapping it into a higher-dimensional space using a kernel function, without ever computing the new coordinates — enabling non-linear boundaries efficiently.
Compare linear, polynomial and RBF kernels.
Linear draws a straight boundary (fast, good when data is linearly separable). Polynomial fits curved boundaries of a chosen degree. RBF (Gaussian) fits very flexible local boundaries and is the common default for non-linear data.
What do the parameters C and γ control?
C trades off margin width against misclassifications (large C = fewer errors, narrower margin, risk of overfitting). γ (RBF) sets each point's influence radius (large γ = tight, wiggly boundary that can overfit).
Why must features be scaled before training an SVM?
SVMs rely on distances. Features on larger scales would dominate the kernel, so standardising (mean 0, variance 1) ensures every feature contributes fairly.
How do you choose the best kernel for a problem?
Try several kernels, tune their parameters with cross-validation (e.g. GridSearchCV), and pick the one with the best validated performance for the metric that matters in the application.
What are advantages and limitations of SVMs?
Advantages: effective in high dimensions, robust with clear margins, flexible via kernels. Limitations: slow on very large datasets, sensitive to scaling and parameter choice, and probabilities/explanations are less direct than simpler models.