# trekhleb/homemade-machine-learning architecture

> Architecture diagram of the GitHub repository trekhleb/homemade-machine-learning, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/trekhleb/homemade-machine-learning
- Repository: https://github.com/trekhleb/homemade-machine-learning

## Overview

Homemade Machine Learning is an educational Python repository of from-scratch algorithms, with interactive notebook demos for experimenting with training data and configurations and viewing predictions and charts. A user selects a demo, supplies or adjusts data and settings, and observes an algorithm’s results. The main capabilities span supervised regression and classification, unsupervised clustering and anomaly detection, and multilayer neural networks. Shared feature preparation supports several models. Edges between sampled modules follow the source index; demo relationships are documented in the README, and notebook internals were not sampled.

Main entry points: [`homemade/utils/features/prepare_for_training.py`](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/prepare_for_training.py) (Training preparation), [`data`](https://github.com/trekhleb/homemade-machine-learning/tree/master/data) (Example datasets), [`homemade/neural_network/multilayer_perceptron.py`](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/neural_network/multilayer_perceptron.py) (Multilayer perceptron), [`homemade/logistic_regression/logistic_regression.py`](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/logistic_regression/logistic_regression.py) (Logistic regression)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_algorithms["Learning algorithms"]
  node_linear["Linear regression"]
  node_logistic["Logistic regression"]
  node_kmeans["K-means<br/>[k_means.py]"]
  node_anomaly["Gaussian anomaly detection"]
  node_mlp["Multilayer perceptron"]
end

subgraph group_features["Feature preparation"]
  node_prepare["Training preparation"]
  node_normalize["Normalization<br/>[normalize.py]"]
  node_polynomial["Polynomial features"]
  node_sinusoid["Sinusoid features"]
  node_sigmoid["Sigmoid<br/>[sigmoid.py]"]
  node_sigmoid_gradient["Sigmoid gradient"]
end

subgraph group_demos["Interactive demos and data"]
  node_regression_demos["Regression demos"]
  node_classification_demos["Classification demos"]
  node_clustering_demo["Clustering demo<br/>[k_means_demo.ipynb]"]
  node_anomaly_demo["Anomaly demo"]
  node_network_demos["Neural network demos"]
  node_datasets["Example datasets"]
end

node_user(("Learner"))

node_user -->|"explores"| node_regression_demos
node_user -->|"explores"| node_classification_demos
node_user -->|"explores"| node_clustering_demo
node_user -->|"explores"| node_anomaly_demo
node_user -->|"explores"| node_network_demos
node_regression_demos -->|"demonstrates"| node_linear
node_classification_demos -->|"demonstrates"| node_logistic
node_clustering_demo -->|"demonstrates"| node_kmeans
node_anomaly_demo -->|"demonstrates"| node_anomaly
node_network_demos -->|"demonstrates"| node_mlp
node_linear -->|"prepares data"| node_prepare
node_logistic -->|"prepares data"| node_prepare
node_mlp -->|"prepares data"| node_prepare
node_prepare -->|"normalizes"| node_normalize
node_prepare -->|"generates features"| node_polynomial
node_prepare -->|"generates features"| node_sinusoid
node_polynomial -->|"normalizes"| node_normalize
node_logistic -->|"computes probabilities"| node_sigmoid
node_mlp -->|"activates layers"| node_sigmoid
node_mlp -->|"computes gradients"| node_sigmoid_gradient
node_sigmoid_gradient -->|"uses function"| node_sigmoid
node_regression_demos -.->|"uses examples"| node_datasets
node_classification_demos -.->|"uses examples"| node_datasets
node_clustering_demo -.->|"uses examples"| node_datasets
node_anomaly_demo -.->|"uses examples"| node_datasets
node_network_demos -.->|"uses examples"| node_datasets

click node_linear "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/linear_regression/linear_regression.py"
click node_logistic "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/logistic_regression/logistic_regression.py"
click node_kmeans "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/k_means/k_means.py"
click node_anomaly "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/anomaly_detection/gaussian_anomaly_detection.py"
click node_mlp "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/neural_network/multilayer_perceptron.py"
click node_prepare "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/prepare_for_training.py"
click node_normalize "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/normalize.py"
click node_polynomial "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/generate_polynomials.py"
click node_sinusoid "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/generate_sinusoids.py"
click node_sigmoid "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/hypothesis/sigmoid.py"
click node_sigmoid_gradient "https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/hypothesis/sigmoid_gradient.py"
click node_regression_demos "https://github.com/trekhleb/homemade-machine-learning/tree/master/notebooks/linear_regression"
click node_classification_demos "https://github.com/trekhleb/homemade-machine-learning/tree/master/notebooks/logistic_regression"
click node_clustering_demo "https://github.com/trekhleb/homemade-machine-learning/blob/master/notebooks/k_means/k_means_demo.ipynb"
click node_anomaly_demo "https://github.com/trekhleb/homemade-machine-learning/blob/master/notebooks/anomaly_detection/anomaly_detection_gaussian_demo.ipynb"
click node_network_demos "https://github.com/trekhleb/homemade-machine-learning/tree/master/notebooks/neural_network"
click node_datasets "https://github.com/trekhleb/homemade-machine-learning/tree/master/data"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_linear,node_logistic,node_kmeans,node_anomaly,node_mlp toneBlue
class node_prepare,node_normalize,node_polynomial,node_sinusoid,node_sigmoid,node_sigmoid_gradient toneAmber
class node_regression_demos,node_classification_demos,node_clustering_demo,node_anomaly_demo,node_network_demos,node_datasets toneMint
class node_user toneIndigo
```

## Components

### Learning algorithms

- [Linear regression](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/linear_regression/linear_regression.py): `homemade/linear_regression/linear_regression.py`
- [Logistic regression](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/logistic_regression/logistic_regression.py): `homemade/logistic_regression/logistic_regression.py`
- [K-means](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/k_means/k_means.py): `homemade/k_means/k_means.py`
- [Gaussian anomaly detection](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/anomaly_detection/gaussian_anomaly_detection.py): `homemade/anomaly_detection/gaussian_anomaly_detection.py`
- [Multilayer perceptron](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/neural_network/multilayer_perceptron.py): `homemade/neural_network/multilayer_perceptron.py`

### Feature preparation

- [Training preparation](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/prepare_for_training.py): `homemade/utils/features/prepare_for_training.py`
- [Normalization](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/normalize.py): `homemade/utils/features/normalize.py`
- [Polynomial features](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/generate_polynomials.py): `homemade/utils/features/generate_polynomials.py`
- [Sinusoid features](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/features/generate_sinusoids.py): `homemade/utils/features/generate_sinusoids.py`
- [Sigmoid](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/hypothesis/sigmoid.py): `homemade/utils/hypothesis/sigmoid.py`
- [Sigmoid gradient](https://github.com/trekhleb/homemade-machine-learning/blob/master/homemade/utils/hypothesis/sigmoid_gradient.py): `homemade/utils/hypothesis/sigmoid_gradient.py`

### Interactive demos and data

- [Regression demos](https://github.com/trekhleb/homemade-machine-learning/tree/master/notebooks/linear_regression): `notebooks/linear_regression`
- [Classification demos](https://github.com/trekhleb/homemade-machine-learning/tree/master/notebooks/logistic_regression): `notebooks/logistic_regression`
- [Clustering demo](https://github.com/trekhleb/homemade-machine-learning/blob/master/notebooks/k_means/k_means_demo.ipynb): `notebooks/k_means/k_means_demo.ipynb`
- [Anomaly demo](https://github.com/trekhleb/homemade-machine-learning/blob/master/notebooks/anomaly_detection/anomaly_detection_gaussian_demo.ipynb): `notebooks/anomaly_detection/anomaly_detection_gaussian_demo.ipynb`
- [Neural network demos](https://github.com/trekhleb/homemade-machine-learning/tree/master/notebooks/neural_network): `notebooks/neural_network`
- [Example datasets](https://github.com/trekhleb/homemade-machine-learning/tree/master/data): `data`

### Other components

- **Learner**

## Connections

- Learner → Regression demos: explores (evidence: `README.md`)
- Learner → Classification demos: explores (evidence: `README.md`)
- Learner → Clustering demo: explores (evidence: `README.md`)
- Learner → Anomaly demo: explores (evidence: `README.md`)
- Learner → Neural network demos: explores (evidence: `README.md`)
- Regression demos → Linear regression: demonstrates (evidence: `README.md`)
- Classification demos → Logistic regression: demonstrates (evidence: `README.md`)
- Clustering demo → K-means: demonstrates (evidence: `README.md`)
- Anomaly demo → Gaussian anomaly detection: demonstrates (evidence: `README.md`)
- Neural network demos → Multilayer perceptron: demonstrates (evidence: `README.md`)
- Linear regression → Training preparation: prepares data (evidence: `homemade/linear_regression/linear_regression.py`)
- Logistic regression → Training preparation: prepares data (evidence: `homemade/logistic_regression/logistic_regression.py`)
- Multilayer perceptron → Training preparation: prepares data (evidence: `homemade/neural_network/multilayer_perceptron.py`)
- Training preparation → Normalization: normalizes (evidence: `homemade/utils/features/prepare_for_training.py`)
- Training preparation → Polynomial features: generates features (evidence: `homemade/utils/features/prepare_for_training.py`)
- Training preparation → Sinusoid features: generates features (evidence: `homemade/utils/features/prepare_for_training.py`)
- Polynomial features → Normalization: normalizes (evidence: `homemade/utils/features/generate_polynomials.py`)
- Logistic regression → Sigmoid: computes probabilities (evidence: `homemade/logistic_regression/logistic_regression.py`)
- Multilayer perceptron → Sigmoid: activates layers (evidence: `homemade/neural_network/multilayer_perceptron.py`)
- Multilayer perceptron → Sigmoid gradient: computes gradients (evidence: `homemade/neural_network/multilayer_perceptron.py`)
- Sigmoid gradient → Sigmoid: uses function (evidence: `homemade/utils/hypothesis/sigmoid_gradient.py`)
- Regression demos → Example datasets: uses examples
- Classification demos → Example datasets: uses examples
- Clustering demo → Example datasets: uses examples
- Anomaly demo → Example datasets: uses examples
- Neural network demos → Example datasets: uses examples
