Guide · Open source
How to use homemade-machine-learning: run from-scratch ML demos
Updated · written with AI from the project's own documentation and checked against our catalogue data
homemade-machine-learning is an open-source collection of Python examples that implement popular machine learning algorithms from scratch, with the underlying math explained alongside each one. It is aimed at students and developers who want to understand how these algorithms work rather than call a library. By the end of this guide you will know what you need installed, how to get the code, and which notebook demos to open first.
Quick start
- 1Install Python and Jupyter Notebook. Make sure Python is installed and install Jupyter Notebook. Optionally create a venv first.
- 2Clone the repository. Run git clone https://github.com/trekhleb/homemade-machine-learning.git in your terminal.
- 3Open the notebooks. Go to the notebooks folder in the cloned repository and launch Jupyter Notebook.
- 4Start with linear regression. Open the univariate linear regression demo and change the training data to see how predictions shift.
- 5Compare the code and the math. Open the matching folder under homemade/ and read the math notes next to the Python implementation.
- 6Try a classification or clustering demo. Run the logistic regression or k-means notebook to see decision boundaries and clusters on Iris data.
What homemade-machine-learning covers
The repository covers supervised learning (linear regression, logistic regression), unsupervised learning (k-means clustering, anomaly detection using a Gaussian distribution) and a neural network section with a multilayer perceptron. Each algorithm has a short math explanation, a Python implementation file and one or more Jupyter Notebook demos.
The explanations are based in most cases on Andrew Ng's machine learning course, according to the README.
- Linear regression: univariate, multivariate and non-linear demos
- Logistic regression: linear and non-linear boundaries, plus MNIST and Fashion MNIST demos
- K-means clustering: applied to Iris flowers
- Anomaly detection: applied to server operational parameters
- Multilayer perceptron: MNIST and Fashion MNIST demos
Install the prerequisites: Python and Jupyter Notebook
The project's prerequisites are Python and Jupyter Notebook. The README asks you to make sure Python is installed on your machine and suggests using venv, Python's standard environment tool, to keep dependencies separate.
If you prefer not to install anything locally, the README includes a Binder badge that opens the notebooks folder in a hosted environment.
- Install Python (the README links to a Python installation guide)
- Optionally create a virtual environment with venv
- Install Jupyter Notebook so you can open the .ipynb demo files
Clone the repository
Download the code with git. The README gives this command for cloning the repository:
git clone https://github.com/trekhleb/homemade-machine-learning.gitRun an interactive notebook demo
Open the notebooks folder in your local clone and launch Jupyter Notebook. The demos let you change training data and algorithm configuration and see charts and predictions immediately in your browser. A good first demo is Univariate Linear Regression, which predicts a country's happiness score from its economy GDP.
If you do not want to clone, the README links each demo through nbviewer, which renders the notebooks without running them.
- Univariate Linear Regression: predict happiness score by economy GDP
- Logistic Regression (Linear Boundary): predict Iris flower class from petal_length and petal_width
- K-means Algorithm: split Iris flowers into clusters
- Multilayer Perceptron: recognize handwritten digits from 28x28 pixel images
Read the math and the implementation side by side
Each algorithm has three linked resources: a Math page with theory and further reading, a Code file with the implementation, and the Demo notebook. Reading the Python file next to its demo is the intended way to see how the math becomes code.
The implementations are written from scratch rather than through third-party library calls, which is the point of the project.
- Math: homemade/linear_regression (and the matching folder for each algorithm)
- Code: homemade/linear_regression/linear_regression.py
- Code: homemade/k_means/k_means.py
- Code: homemade/neural_network/multilayer_perceptron.py
Know the limits before you use it
The README states that these implementations are not intended for production use. The project avoids library one-liners on purpose, so it is not a drop-in replacement for a library, and it does not focus on speed or state-of-the-art accuracy.
For those needs, the README points to related projects in the same family, such as machine-learning-octave for an Octave/MatLab version.
- Not for production systems
- Not for large-scale performance or state-of-the-art models
- Not a one-line library call
What people use it for
- Learning how linear regression fits a line to data by editing the training examples in the univariate demo
- Seeing how a logistic regression decision boundary changes between linear and non-linear data
- Experimenting with k-means clusters on the Iris flower dataset
- Following along with Andrew Ng's machine learning course concepts while running the code
- Exploring anomaly detection on server parameters like latency and threshold
Frequently asked questions
Is homemade-machine-learning free to use?
The repository is licensed under MIT, according to the project listing. You can use, study and modify the code under that license.
What do I need to install to run homemade-machine-learning?
The prerequisites are Python and Jupyter Notebook. Clone the repository with git, then open the notebook demos in Jupyter.
Can I use homemade-machine-learning in production?
No. The README states the implementations are not intended for production use. They are built to help you understand the algorithms.
Which algorithms are included?
The repository includes linear regression, logistic regression, k-means clustering, anomaly detection using a Gaussian distribution and a multilayer perceptron neural network.
Are there alternatives to homemade-machine-learning?
Comparable projects listed for this item include scikit-learn, machine-learning-experiments and machine-learning-octave. scikit-learn is a library you call with functions, while this project implements the algorithms yourself for learning.
Does it require the Octave or MatLab version?
No. The Python code and notebooks are self-contained in this repository. The README points to machine-learning-octave only as a separate Octave/MatLab version.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
homemade-machine-learning suits students and developers who want to see how core machine learning algorithms work by reading simple Python code and running notebooks. If you need production models, large-scale performance or a library you can call in one line, pick scikit-learn or another maintained library instead.
Install options & downloads