Classical ML Algorithms: Introduction
Deep learning gets the headlines, but most models running on tabular data today are "classical": linear and logistic regression, decision trees, random forests, gradient-boosted trees, support vector machines, nearest neighbours, naive Bayes, and on the unsupervised side k-means, DBSCAN, Gaussian mixtures and PCA. They train in seconds on a laptop, need far less data than a neural network, and many of them can be explained to the people who use their predictions.
This course takes them one at a time. For every algorithm you will see:
- the problem it solves and the intuition, often with a lab you can play with;
- the math in a few formulas, with links to the Mathematics for ML lessons that derive them;
- a from-scratch NumPy version, short enough to read in one sitting;
- the scikit-learn (or XGBoost, LightGBM) version on the same data, and a check that both give the same answer;
- hyperparameters, strengths, weaknesses and when to use it, then practice questions and a short quiz.
The from-scratch versions are for understanding, not for production. Once you have written a decision tree yourself, the max_depth and min_samples_leaf arguments of the library version stop being magic words.
The map
In supervised learning every training example is a pair and the model learns a function with . When is a number it is regression, when it is a category it is classification. In unsupervised learning there is no : the algorithm looks for groups (clustering), for a smaller set of coordinates that keeps most of the information (dimensionality reduction), or for points that do not fit (anomaly detection). Supervised and unsupervised learning in ML Foundations introduce both settings.
Six algorithms, one data set
Here is a preview of where the course is going: six classifiers you will build from scratch, each scored by 5-fold cross-validation on the breast-cancer data that ships with scikit-learn (569 tumours, 30 measurements, malignant or benign). Every model has the same fit/predict interface, so one loop evaluates them all.
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score, StratifiedKFold
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import SVC
from sklearn.naive_bayes import GaussianNB
X, y = load_breast_cancer(return_X_y=True) # 569 tumours, 30 features, 2 classes
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=0)
models = {
"logistic regression": make_pipeline(StandardScaler(), LogisticRegression()),
"kNN (k=5)": make_pipeline(StandardScaler(), KNeighborsClassifier()),
"decision tree": DecisionTreeClassifier(random_state=0),
"random forest": RandomForestClassifier(n_estimators=100, random_state=0),
"SVM (RBF)": make_pipeline(StandardScaler(), SVC()),
"Gaussian naive Bayes": GaussianNB(),
}
for name, model in models.items():
scores = cross_val_score(model, X, y, cv=cv)
print(f"{name:22s} accuracy {scores.mean():.3f} +/- {scores.std():.3f}")logistic regression accuracy 0.979 +/- 0.014
kNN (k=5) accuracy 0.965 +/- 0.021
decision tree accuracy 0.926 +/- 0.022
random forest accuracy 0.965 +/- 0.015
SVM (RBF) accuracy 0.977 +/- 0.007
Gaussian naive Bayes accuracy 0.939 +/- 0.020
Three things to notice already:
- The differences are small and noisy. The spread across folds (about 0.01 to 0.02) is as large as most gaps between models. The comparison bench at the end of the course shows how to compare models without fooling yourself.
- Scaling sits inside the pipeline. kNN, SVMs and regularised logistic regression depend on the scale of the features, so the scaler is part of the model and is fitted on the training folds only. Trees and naive Bayes do not need it. Preprocessing is the subject of Data Preparation.
- A single tree is the weakest here, a forest of them among the strongest. Why averaging many unstable models helps is one of the central ideas of the course (bagging, random forests).
How the course is organised
| Section | Algorithms | Main idea |
|---|---|---|
| Linear models | linear, polynomial, ridge, lasso, logistic, softmax regression | a weighted sum of features, fitted by least squares or by maximum likelihood |
| Trees and ensembles | CART, bagging, random forest, gradient boosting, XGBoost, LightGBM, CatBoost | split the feature space into boxes; combine many trees |
| Neighbours, margins, probabilities | kNN, linear and kernel SVM, Gaussian and multinomial naive Bayes | similarity, the widest margin, Bayes' rule |
| Clustering | k-means, hierarchical, DBSCAN, Gaussian mixtures | groups by distance, by linkage, by density, by probability |
| Reduction and anomalies | PCA, isolation forest, autoencoders | fewer coordinates; points that are easy to isolate |
| Choosing | decision guide, comparison bench | which model when, measured honestly |
The course assumes you have met these models once at the level of the Machine Learning course (its lessons on linear regression, decision trees, k-means and others are the gentler introductions), and that you can read NumPy. The math assumes vectors, matrices, gradients and basic probability; the lessons link to the exact page of Mathematics for ML where each result is derived.
How examples run here
Every ```python run example runs in your browser (Python in WebAssembly, with NumPy, pandas, SciPy, Matplotlib, scikit-learn 1.7, XGBoost and LightGBM). The first run downloads the libraries and takes a few seconds. Examples use fixed seeds and print rounded numbers, so the output you get is the output shown. Two examples need libraries that do not run in a browser, CatBoost and PyTorch: they are marked Recorded and show the output of a real run on a CPU.
Data comes only from generators (make_classification, make_blobs, NumPy) and from the small data sets that ship with scikit-learn (load_iris, load_wine, load_breast_cancer, load_digits, load_diabetes), so nothing is downloaded while you learn.
Test yourself
Which of these is an unsupervised task?
- Predicting house prices from their features
- Grouping customers by purchase behaviour without any labels
- Classifying emails as spam or not
- Predicting tomorrow's temperature
In the example, why is StandardScaler inside the pipeline instead of applied to X once before cross-validation?
- Pipelines run faster
- So the scaler is fitted on the training folds only and no information from the validation fold leaks in
- Because trees need scaled features
- It makes no difference at all
Logistic regression scored 0.979 and the RBF SVM 0.977, with fold spreads of about 0.01. What is the fair conclusion?
- Logistic regression is clearly better
- The SVM is clearly better
- The difference is well within the noise; these two are tied on this data
- Both models are overfitting