Math for Machine Learning Playground

The maths behind machine learning, one picture at a time. Move the controls, or press an example, and watch the result change. Your calculations don’t leave your machine.

Loading…

New to this? Start here

  1. Read “In plain words” at the top of each section. It explains the idea without the formula.
  2. Press an example button. It sets the controls for you, so you can see one case clearly.
  3. Then change the controls yourself. Move a slider, change a number, or paste your own points.

You do not need calculus to use this page. The formulas are shown for reference, and the words beside them say what each one means.

Words you will see (a quick glossary)
Derivative, or slope
How steeply a curve goes up or down at one point. Flat means 0.
Loss
A score for how wrong a model is. Smaller is better. Training tries to make it smaller.
Gradient
The slope for each setting of a model. It points uphill, so training moves the other way.
Learning rate
How big each training step is.
Activation
The bend a neuron puts on its input, such as squashing it between 0 and 1.
Softmax
Turns scores into percentages that add up to 100%.
Least squares
Finds the line closest to all the points, measuring the gaps and squaring them.
R²
How well a line fits. 1 means every point is on the line; 0 means no better than the average.
Principal component
A direction in which the data varies a lot. The first one varies the most.
Normal distribution
The bell curve. Many measurements cluster around the middle like this.
Bayes' theorem
How to update a chance after you see new evidence, such as a test result.

1. Derivatives: how steep is the curve?

In plain words: a derivative tells you how steeply a curve rises or falls at one point. Rising steeply gives a big positive number, flat gives 0, and falling gives a negative number. For machine learning this matters because training means going downhill, and the slope shows which way is down.

Try:

The orange line touches the curve at your x. Its steepness is the slope. The two ways of getting the slope (formula and numerical) should agree, which is a useful check.

f(x) = x²  →  f′(x) = 2x  (read: “the slope of f at x is 2x”)

2. Gradient descent: walking downhill in fog

In plain words: imagine standing on a hill in thick fog. You feel the slope under your feet, take one step downhill, and repeat. Each step is the learning rate times the slope. A small step is safe but slow. A big step can jump past the bottom, and then the walk can bounce around or run off.

Try:

Green dots are the steps taken, and the orange dot is where the walk ended. With two valleys, the start point decides which valley you reach, which is why real training uses several starts or extra tricks.

next x = current x − learning rate × slope

3. Activation functions: how a neuron bends its input

In plain words: a neuron adds up its inputs and then passes the total through a bend, called an activation. Without bends, a stack of layers would still be one simple straight-line rule. The bend is what lets a network learn curved patterns. The dashed line shows how sensitive the output is to the input.

Try:

ReLU is the most common choice in modern networks: it passes positive numbers through and blocks negative ones to 0. Its dashed line is 0 on the left and 1 on the right, which is why it is cheap to compute.

4. Softmax and loss: how wrong was the guess?

In plain words: a classifier gives each class a score. Softmax turns those scores into percentages that add up to 100%, with bigger scores getting bigger shares. The loss then asks one question: how much of the share went to the right answer? A big share gives a small loss. A tiny share gives a large loss, and that is what training tries to reduce.

Try:

share of class i = e^(score i) ÷ (total of all e^scores)    loss = −ln(share of the right class)

5. Least squares: the line closest to the dots

In plain words: given some dots, draw the straight line that stays as close to them as possible. For each dot, measure the vertical gap to the line, square it so that gaps above and below both count, and add them up. The best line is the one with the smallest total. R² then says how well that line really fits.

Try:

The “curved data” example shows the limit of a straight line: the best line is flat, and R² is 0. A line is only as good as the pattern in the data.

best line: y = slope × x + intercept    R² = 1 − (sum of squared gaps) ÷ (spread of y around its average)

6. PCA: the main directions of a cloud of dots

In plain words: picture a cloud of dots on a table. PCA finds the direction the cloud stretches furthest, which is the first principal component, and then the direction at right angles to it that stretches the most of what is left. If the cloud is long and thin, one direction carries nearly all the information. If it is round, no direction stands out.

Try:

Orange is the first component and green the second. Keeping only the first one turns two numbers per dot into one, and the share shown above tells you how much is kept. Machine learning uses this to shrink data with many columns.

7. Normal distribution and Bayes' theorem: chance and evidence

In plain words: many measurements cluster around a middle value, with fewer far away, which gives the bell curve. The chance of a value landing in a range is the shaded area under the curve. Bayes' theorem then answers a different question: after a test says “yes”, how likely is it that the condition is really there? The answer depends on how rare the condition is, not only on how good the test is.

Try:

Bayes: a positive test result

Try:

P(condition | positive) = P(positive | condition) × P(condition) ÷ P(positive)

About this playground

Machine learning rests on a few pieces of calculus, linear algebra and probability: derivatives tell a model which way to move, matrices hold the data, and probability says how sure the model should be. This page shows each piece with a picture you can change, so the formulas used in scikit-learn, PyTorch or TensorFlow become concrete.

Your calculations don’t leave your machine. Nothing is uploaded and nothing is sent to a server.

Questions

Do I need calculus to use this page?

No. Each section explains the idea in words first, and the examples let you see the result without doing any algebra. The formulas are there for reference.

What is a derivative in machine learning?

The slope of the loss with respect to a setting. Gradient descent uses the slope to decide which way to change the setting.

Why does learning rate matter?

It sets the size of each step. A small rate takes many steps; a rate too large can overshoot the valley and bounce or grow without end, as the second section shows.

What is the difference between softmax and sigmoid?

Sigmoid turns one score into a probability between 0 and 1. Softmax turns several scores into shares that add up to 100%.

Why is the Bayes answer often lower than I expect?

When the condition is rare, most positive results are false alarms, simply because there are so many healthy cases. The base rate is part of the answer.

Can I trust the numbers?

Yes for learning. Results are shown rounded, and each calculation is checked against known answers. Change the inputs and check the result by hand for a simple case, such as the slope of x² at x = 1.