Guide · Open source
How to use ML-From-Scratch: install and run the examples
Updated · written with AI from the project's own documentation and checked against our catalogue data
ML-From-Scratch is an open-source Python project by Erik Linder-Norén that implements fundamental machine learning models and algorithms using only NumPy. It is aimed at people who want to see how an algorithm works inside rather than just call a library function. This guide shows you how to install it, run its bundled examples, and know what to expect from the code.
Quick start
- 1Check prerequisites. Make sure Python and Git are installed on your computer.
- 2Clone the repository. Run git clone on the eriklindernoren/ML-From-Scratch GitHub URL.
- 3Enter the project folder. Change into the ML-From-Scratch directory with cd.
- 4Install the package. Run python setup.py install from inside the project folder.
- 5Run an example. Start with python mlfromscratch/examples/polynomial_regression.py to see a model train.
- 6Read the source. Open the matching file in the mlfromscratch folder and step through the algorithm.
What is ML-From-Scratch and who is it for?
The project is a set of Python implementations of machine learning models and algorithms written from scratch. Its stated purpose is not to make the algorithms as fast or efficient as possible, but to present their inner workings in a transparent and accessible way. The README says it aims to cover topics from linear regression to deep learning.
It suits students studying how regression or neural networks work, developers who learn better by reading code, and teachers who want examples of algorithm internals. It is not a good fit for production systems that need speed or scale, and the examples assume some Python and ML background.
Install ML-From-Scratch on your computer
You need Python and Git. The project is installed by cloning the repository and running its setup script. The README lists these three commands in order.
- Clone the repository from GitHub.
- Move into the project folder.
- Install the package with the setup script.
$ git clone https://github.com/eriklindernoren/ML-From-Scratch
$ cd ML-From-Scratch
$ python setup.py installRun the polynomial regression example
The first example fits a regularized polynomial regression model to temperature data measured in Linköping, Sweden in 2016. The README includes a figure showing the training progress. Run it from inside the project folder.
$ python mlfromscratch/examples/polynomial_regression.pyTry the other bundled examples
The README includes runnable scripts for several other methods. Each one is a separate file you run the same way.
- Classification with a CNN on the digit dataset: mlfromscratch/examples/convolutional_neural_network.py. The README reports a model summary and an accuracy of about 0.987 on this example.
- Density-based clustering with DBSCAN on the moons dataset: mlfromscratch/examples/dbscan.py.
- Generating handwritten digits with a GAN: mlfromscratch/unsupervised_learning/generative_adversarial_network.py.
- Deep Q-Network on the CartPole-v1 environment from OpenAI gym: mlfromscratch/examples/deep_q_network.py.
- Image reconstruction with a restricted Boltzmann machine: mlfromscratch/examples/restricted_boltzmann_machine.py.
- Evolved neural network: mlfromscratch/examples/neuroevolution.py.
- Genetic algorithm that produces a target string: mlfromscratch/examples/genetic_algorithm.py.
Read the code to learn how an algorithm works
The main reason to use this project is to read the source. Each example prints a layer table with parameter counts and output shapes, which helps you connect the code to the architecture. The genetic algorithm example, for instance, describes its approach in its own output: fitness is based on alphabetical distance to the target, parents are chosen in proportion to fitness, reproduction uses single-point crossover, and mutation assigns new characters at random.
Start with a small example such as polynomial regression or the genetic algorithm, then move to the neural network examples once the structure is familiar.
Know the limits before you rely on it
The README is clear that efficiency is not the goal. If you need fast training, large datasets or a ready-made API, a mature library is a better choice. Projects the README lists for comparison are scikit-learn, PyTorch, TensorFlow and Keras.
What people use it for
- Studying how linear and polynomial regression fit data by reading the training code.
- Showing students the layers of a CNN digit classifier with its printed parameter table.
- Stepping through DBSCAN clustering on the moons dataset.
- Reading how a GAN's generator and discriminator are structured.
- Exploring a genetic algorithm that evolves a target string.
Frequently asked questions
Is ML-From-Scratch free to use?
The project is released under the MIT licence, as listed on its repository. Check the LICENSE file in the repository for the full terms before you reuse code.
What do I need to install ML-From-Scratch?
The README lists Python and Git as the prerequisites. You clone the repository and run python setup.py install from the project folder.
How do I run an example in ML-From-Scratch?
Run a script from the project folder, for example python mlfromscratch/examples/polynomial_regression.py. Each example in the README has its own command.
Is ML-From-Scratch good for production machine learning?
No. The README says the purpose is transparency rather than optimized, computationally efficient algorithms, so it is not meant for production systems that need speed or scale.
What is the difference between ML-From-Scratch and scikit-learn?
ML-From-Scratch shows how algorithms work internally with NumPy code you can read. scikit-learn is a library you call with minimal code, and it is the better choice when you want a ready-made tool.
Do I need to know Python to use ML-From-Scratch?
Yes, the examples assume some comfort with Python and machine learning concepts, so complete beginners to Python may find them hard to follow.
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
ML-From-Scratch is a good fit for students, teachers and developers who want to read clear NumPy implementations of machine learning algorithms and step through how they work. It is a poor fit if you need fast training, scale or a simple library to call, in which case scikit-learn, PyTorch, TensorFlow or Keras are better options.
Install options & downloads