← All Posts
From Scratch · C++ · Machine Learning

Machine Learning from Scratch in C++

This series builds machine-learning algorithms from scratch in plain C++, using nothing but the standard library, no NumPy, no scikit-learn, no LibTorch. The goal is working, compilable code that you can read end to end. Every post ships a complete program and reports the numbers it actually prints. The conceptual derivations (entropy, information gain, the bias-variance story) live in the companion Machine Learning · Decision Trees notes; here the focus is implementation.

The series has two halves. The models grow the decision-tree family one idea at a time, each reusing the last. The data preprocessing toolkit covers the steps that come before any model: getting raw data into a clean numeric matrix and evaluating a model honestly.

The Models

Regression

Linear and logistic regression from scratch, from reliable solvers and regularization to diagnostics, classification metrics, and calibration.

  • Linear Regression: least squares, iterative solvers, regularization, robust losses, and evaluation.
  • Logistic Regression: log-loss, Newton and IRLS solvers, softmax classification, metrics, and calibration.

Decision Trees

The foundation: one recursive tree, then the same skeleton for regression.

Random Forest

Average many decorrelated trees to cut variance.

  • Random Forest — bootstrap sampling, feature sub-sampling (mtry = √D), majority vote, and out-of-bag error.

Gradient Boosted Machines

Add shallow regression trees in sequence, each following the current gradient target.

Boosting & AdaBoost

Bridge gradient boosting and AdaBoost, then compare sequential boosting with bagging.

Explainability

Explain trained models by perturbing features, fitting local surrogates, and estimating additive attributions.

Data Preprocessing

Real data rarely arrives as a clean numeric matrix with a ready split. The Data Preprocessing sub-series implements the everyday steps from scratch and links each one to the model it feeds.

Data Preprocessing

One toolbox, composed. Every model here is the same recursive tree with one knob changed, and every preprocessing step produces the matrix those trees consume. Reading the series top to bottom, nothing is a black box.