Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

38 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Machine Learning from First Principles

Derive, implement, and verify machine learning algorithms from mathematical principles without black-box dependencies.

Python Version Mathematics Pytest License


๐Ÿงฎ Sister Repository: For standalone, deep-dive mathematical prerequisites (Linear Algebra, Calculus & Optimization, Probability & Statistics, Information Theory, Numerical Computing), check out applied-mathematics-foundation.


๐ŸŽฏ Core Philosophy

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.


๐Ÿ“‚ Repository Structure

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.


๐Ÿ—บ๏ธ Topics & Curriculum

The repository covers 17 distinct algorithmic modules organized into three main phases:

Phase 1: Core Mathematical ML

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

Phase 2: Classical Machine Learning

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

Phase 3: Deep Learning & Neural Architectures

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

โšก Quick Start & Installation

Prerequisites

  • Python 3.12+
  • Virtual environment (venv or conda)

1. Clone & Setup Environment

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\activate

2. Install Dependencies

pip install -r requirements.txt
pip install -e .

3. Run Verification Tests

Ensure all algorithm implementations pass the unit test suite:

pytest

๐Ÿ”ฌ Software Engineering & Testing

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
  • Gradient checks and numerical stability checks

๐Ÿ“„ License

This repository is released under the MIT License.

About

๐Ÿค– 17 ML & Deep Learning algorithms derived mathematically and implemented from scratch in pure NumPy & PyTorch โ€” OLS, SVM, Random Forest, CNN, LSTM, Transformer, VAE + 37 unit tests

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages