Supervised Machine Learning

In supervised learning, the model learns from labeled training data, making predictions based on known input-output pairs. This section explores several key algorithms with interactive demos running directly in your browser via ONNX Runtime Web (no backend, no API keys).

The bundled .onnx files are small tutorial models trained on synthetic data so the UI and math stay consistent with the lesson. Regenerate them anytime with python scripts/export_tutorial_onnx.py.

Naive Bayes

A probabilistic classifier based on Bayes' Theorem. In this example, we predict the appropriate drug for a patient based on their health metrics.

ONNX Enabled

Drug Classification

Predicts drug type (Y, C, X, A, B) based on patient vitals.

K-Nearest Neighbors

KNN classifies a new point by looking at the k closest training examples and taking a majority vote. No training phase β€” it memorises the whole dataset.

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=5)
knn.fit(X_train, y_train)
y_pred = knn.predict(X_test)

Live Demo β€” Iris Species (KNN)

Adjust the four Iris measurements and see the predicted species in real time.

KNN Explorer

Click anywhere on the canvas to test a query point. The model finds the K closest training points and takes a majority vote. Watch the decision boundary update as you change K.

Interactive Decision Boundary

Click anywhere to test a point. The model finds the K closest training points and takes a majority vote.

Click the canvas to see a prediction.

Legend

Class A
Class B
Class C
β˜…Query point

Decision Tree

A Decision Tree splits the feature space with a series of threshold rules, forming a tree where each leaf is a class. Highly interpretable β€” you can follow the exact path to any prediction.

from sklearn.tree import DecisionTreeClassifier
dt = DecisionTreeClassifier(max_depth=4, random_state=42)
dt.fit(X_train, y_train)
y_pred = dt.predict(X_test)

Live Demo β€” Iris Species (Decision Tree)

Same inputs as KNN β€” compare how the two algorithms differ on edge cases.

Logistic Regression

Logistic Regression models the probability of a binary outcome using the sigmoid function. Despite the name, it's a classification algorithm β€” the output is a probability between 0 and 1.

from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(max_iter=300)
lr.fit(X_train, y_train)
proba = lr.predict_proba(X_test)[:, 1]  # P(survived)

Live Demo β€” Titanic Survival

Would this passenger have survived? Adjust the features and check the model's prediction.

Linear Regression

Linear Regression fits a line (or hyperplane) through data to predict a continuous target. The model learns weights w and bias b such that Ε· = wΒ·x + b minimises the mean squared error.

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
lr.fit(X_train, y_train)
predictions = lr.predict(X_test)
print(lr.coef_, lr.intercept_)

Live Demo β€” Insurance Cost Predictor

Predicts annual medical insurance charges based on patient profile.

Artificial Neural Networks

Neural networks are a set of algorithms, modeled loosely after the human brain, that are designed to recognize patterns.

Chicago Crime Predictor

Forecasts crime counts or predicts arrests using an MLP (Multi-Layer Perceptron).

Features

Try it yourself — Tune k in KNN

Train a KNN classifier for k = 1, 3, 5, 7, 9, 11 on the Iris dataset (80/20 split). Print test accuracy for each k. Which gives the best generalisation?

Show hint
from sklearn.neighbors import KNeighborsClassifier
for k in [1,3,5,7,9,11]:
    acc = KNeighborsClassifier(k).fit(X_train,y_train).score(X_test,y_test)
    print(f'k={k}: {acc:.2%}')

Knowledge Check

Test your understanding β€” pick the best answer, then click Check.

1. What is the key difference between classification and regression?

2. In KNN, what happens as k approaches the full training set size?

3. Which metric applies to classification but NOT regression?