Machine Learning from Scratch

How machines learn from data, built from plain Python on tiny datasets so you can watch every number change. No libraries, no black boxes.

Machine Learning · beginner · 13 lessons · 2 h 25 min · free

What you'll learn

Before you start

Lessons

Data as Numbers

Turning things into lists of numbers, and comparing them.

  1. Vectors and the Dot Product: Describe a film and a viewer as lists of numbers, then multiply and add to measure how well they match: the dot product behind recommendations and neural networks.
  2. Putting Features on the Same Scale: Grams and a 1-to-10 sweetness score live on very different scales. See how that makes one feature drown out the other, and how scaling to 0-1 fixes it.

Learning from Data

How a model improves by measuring its mistakes and correcting them.

  1. How a Model Learns: Gradient Descent: Train a tiny model on four data points and watch it discover a hidden rule by predicting, measuring its error and correcting itself, one line at a time.

Classifying

Deciding which kind of thing something new is, from examples.

  1. k-Nearest Neighbours: Judge by the Company: Classify a new fruit as an apple or a lemon by measuring how far it is from every known fruit and letting its nearest neighbours vote.
  2. Logistic Regression: From Score to Chance: Turn a straight line into a probability with the sigmoid curve, predict each student's chance of passing, then take one learning step.
  3. Decision Trees: Finding the Best Question: A decision tree learns by asking yes/no questions. Try four questions of the form "is the size above t?" and measure which one separates apples from lemons best, with Gini impurity.

Finding Groups

Finding structure in data when nobody gives the answers.

  1. k-Means: Finding Groups Without Answers: No labels this time: find two groups of shoppers by repeating two moves. Everyone joins the nearest centre, then each centre moves to the middle of its group.

Neural Networks

From one artificial neuron to networks of them.

  1. One Neuron Learns: The Perceptron: Watch a single artificial neuron learn the AND rule from four examples: weighted sum, fire or not, measure the mistake, nudge the weights.
  2. A Tiny Network Solves XOR: One neuron can draw only one straight line, so it cannot do XOR. Add two hidden neurons, each drawing its own line, and an output neuron that combines them.

Deep Learning

How deep networks learn their weights, bend their outputs, and see images.

  1. Backpropagation: Sharing Out the Blame: How does a network with a hidden layer learn? Work out how much each weight is to blame for the error, starting at the output and passing it back, one link at a time.
  2. Activation Functions: Bending the Line: Step, sigmoid, tanh and ReLU: four ways a neuron can bend its output. Without a bend, any number of layers adds up to one straight line.
  3. Convolution: How Networks See Edges: Slide a 3 × 3 filter over a tiny image and add up what it covers. Where dark meets bright, the numbers jump: the filter has found an edge.

Testing Models

Checking that a model works on data it has never seen.

  1. Train, Test, and Overfitting: A model that memorises its training data scores perfectly on it and badly on anything new. Hold some data back, and test on that.

Related courses