Derive, implement, and verify machine learning algorithms from mathematical principles without black-box dependencies.
๐งฎ 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/ # 17 algorithm modules (theory, implementation, exercises)
โโโ synthesis/ # Cross-model comparisons & decision guides
โโโ src/ml_first_principles/ # Clean, installable Python library written from scratch
โโโ tests/ # Unit tests & numerical regression suites
โโโ INDEX.md # Full curriculum index & prerequisite DAG
โโโ NOTEBOOK_STANDARDS.md # Writing & coding standards
โโโ README.md
๐ Mathematical Prerequisites (Linear Algebra, Calculus, Probability, Information Theory, etc.) are maintained in the dedicated applied-mathematics-foundation repository.
The repository covers 17 distinct algorithmic modules organized into three main phases:
| Topic | Module | Mathematical Core | Status |
|---|---|---|---|
01 |
Linear Regression | OLS, Normal Equations, QR Decomposition | โ Complete |
02 |
Gradient Descent | Convexity, Step Size, Momentum, Adaptive Rates | โ Complete |
03 |
Regularization | L1/L2 Norms, Lasso, Ridge, ElasticNet, KKT | โ Complete |
04 |
Logistic Regression | MLE, Sigmoid, Cross-Entropy, Newton-Raphson | โ Complete |
10 |
PCA | SVD, Eigendecomposition, Covariance Manifolds | โ Complete |
| Topic | Module | Mathematical Core | Status |
|---|---|---|---|
05 |
Decision Trees | Gini Impurity, Information Gain, Entropy | โ Complete |
06 |
Ensemble Methods | Bagging, Random Forest, AdaBoost, Gradient Boosting | โ Complete |
07 |
K-Nearest Neighbors | Metric Spaces, KD-Trees, Distance Measures | โ Complete |
08 |
Naive Bayes | Bayes Theorem, MAP, Gaussian/Multinomial Priors | โ Complete |
09 |
Support Vector Machines | Dual Formulation, Convex Quadratic Program, Kernels | โ Complete |
11 |
Clustering | K-Means, EM Algorithm, Gaussian Mixture Models | โ Complete |
12 |
Dimensionality Reduction | t-SNE, UMAP, Spectral Embeddings | โ Complete |
| Topic | Module | Mathematical Core | Status |
|---|---|---|---|
13 |
Neural Networks | Computational Graphs, Chain Rule, Backprop | โ Complete |
14 |
Convolutional Networks | Cross-Correlation, Receptive Fields, Pooling | โ Complete |
15 |
Recurrent Networks | RNNs, BPTT, Vanishing Gradients, LSTM, GRU | โ Complete |
16 |
Transformers | Scaled Dot-Product, Multi-Head Self-Attention, Positional Encoding | โ Complete |
17 |
Autoencoders | Bottleneck Representations, Variational Inference (VAE) | โ Complete |
- 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 .Ensure all algorithm implementations pass the unit test suite:
pytestAll 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
- Gradient checks and numerical stability checks
This repository is released under the MIT License.