Derive, implement, and verify machine learning algorithms from mathematical principles without black-box dependencies.
๐ Browse this curriculum as a website: hien078.github.io/Machine-Learning-from-scratch โ rendered theory, executed notebooks, and cross-topic maps.
๐งฎ Sister Repository: For standalone, deep-dive mathematical prerequisites (Linear Algebra, Calculus & Optimization, Probability & Statistics, Information Theory, Numerical Computing), check out applied-mathematics-foundation.
Machine Learning is applied mathematics and numerical computation. This repository strictly follows a first-principles methodology:
Phenomenon & Motivation
โ Mathematical Formulation
โ Analytical Derivation
โ From-Scratch NumPy/PyTorch Implementation
โ Numerical Verification & Behavioral Tests
โ ML/AI Connections & Trade-offs
Every algorithm is built step-by-step from raw matrix operations and calculus before comparing with production libraries.
Machine-Learning-from-scratch/
โโโ topics/ # 22 algorithm modules + synthesis/ cross-topic maps
โโโ projects/ # Applied capstones using the library end-to-end
โโโ src/ml_first_principles/ # Clean, installable Python library written from scratch
โโโ tests/ # Unit tests & numerical regression suites
โโโ INDEX.md # Full curriculum index & prerequisite DAG
โโโ CONTRIBUTING.md # Process, notebook standards, roadmap & decisions log
โโโ pyproject.toml # Package metadata, dev extras, lint & test config
โโโ README.md
๐ Mathematical Prerequisites (Linear Algebra, Calculus, Probability, Information Theory, etc.) are maintained in the dedicated applied-mathematics-foundation repository.
22 algorithm modules across five phases. INDEX.md is the single source of truth โ full topic matrix with mathematical core, prerequisites, prerequisite DAG, and per-topic maturity.
| Phase | Focus | Modules |
|---|---|---|
| 1 | Core Mathematical ML โ least squares, optimization, regularization, MLE, spectral methods | 01โ04, 10 |
| 2 | Classical ML โ trees, ensembles, metric and probabilistic methods, kernels, clustering | 05โ09, 11, 12 |
| 3 | Deep Learning โ backprop, convolution, recurrence, autoencoders | 13โ15, 17 |
| 4 | Transformers โ scaled dot-product and multi-head self-attention | 16 |
| 5 | Modern AI โ RL, generative models, GNNs, LLM engineering, self-supervised learning | 18โ22 |
All 22 topics are ๐ Verified: every gate of the Notebook Standards (ยง10) passes.
- Python 3.12+
- Virtual environment (
venvorconda)
git clone https://github.com/hien078/Machine-Learning-from-scratch.git
cd Machine-Learning-from-scratch
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activatepip install -r requirements.txt
pip install -e ".[dev]" # library + pytest test toolingOptional โ PyTorch: the library-comparison sections of topics 13โ17 additionally import
torch. Install it separately (pip install torch) to execute those notebooks; everything else runs on the pinned dependencies above.
Ensure all algorithm implementations pass the unit test suite:
pytestThe projects/ directory holds end-to-end capstones built on the library: a tabular benchmark against sklearn, a from-scratch NumPy char-level transformer, a digits autoencoder vs PCA study, and a Q-learning gridworld analysis. Each trains in under 30 seconds, uses only bundled data, and commits its generated report.
All algorithm implementations inside src/ml_first_principles/ are paired with automated regression tests in tests/:
- Linear Models, Optimizers, Tree Models, Ensembles
- Distance Metrics, Probabilistic Models, Neural Core, Visualization
- Phase 5 modules: RL (GridWorld, Q-Learning), Generative (VAE/GAN), GNN (GCN/GAT), LLM (BPE/LoRA/DPO), SSL (InfoNCE/MAE)
- Gradient checks, numerical stability checks, and package-export consistency
Every gate โ lint, format, notebook format, types, tests with the coverage floor โ runs from one command, the same one CI runs:
mlfp checkNotebook execution is validated separately because it is slow:
mlfp nb-exec runs every notebook top-to-bottom on a fresh
kernel (add --write to refresh the committed outputs โ the only sanctioned way to
produce them).
This repository is released under the MIT License.