A straight line is the simplest way one thing can depend on another: each step across adds the same amount up. Two numbers describe it completely, its slope and its intercept. The first machine learning model you'll ever train, linear regression, is exactly this: the weight is the slope and the bias is the intercept. This lesson is where algebra and models meet. You'll also learn to read a line from a table, from a graph and from its formula.
What you'll be able to do
Read y = mx + b: the slope as rise over run, and the intercept as y at x = 0
Find the line through two points, and tell when two lines are parallel
Work out slope and intercept in Python, print a table of a line and draw its graph
See a model's weight as a slope and its bias as an intercept, and predict from a line
Quick check
Have a guess before you start. A straight line goes through (0, 3) and (2, 7). What is y when x = 4?
15
14
9
11
Show the answer
11
From x = 0 to 2, y goes up 4: that's 2 for each step across. Two more steps to x = 4 add 4 more: 11.
The math: a constant climb
Purpose. Describe the simplest way one quantity can depend on another.
A straight line has the equation
y=mx+b
In plain words: m is the slope, how much y goes up for each step of 1 across. b is the intercept, where the line crosses the y-axis, the value of y when x = 0.
In y = 2x + 3, every step right climbs 2, and the line starts at 3 when x = 0. A negative slope goes downhill: y falls as x grows. A slope of 0 is flat.
Quick check
From an earlier lesson: f(x) = 2x + 3. What is f(0)?
3
2
5
0
Show the answer
3
f(0) = 2 × 0 + 3 = 3. Putting x = 0 into a line always gives its intercept.
Slope from two points. Pick any two points on the line. The rise is how far up you go from the first to the second; the run is how far across. Slope is rise over run. Then walk back along the line to x = 0 to find the intercept. Watch it for (1, 5) and (3, 9). The picture draws each step: the rise and the run become two sides of a right triangle, the line runs through both points, and last you follow it back to where it crosses x = 0.
Try it now
Then set the second point to 3, 1 and press Use these numbers: the rise is negative, so the line goes downhill.
Symbols and formula. For two points (x₁, y₁) and (x₂, y₂):
m=x2−x1y2−y1b=y1−mx1
In plain words: slope is the change in y over the change in x. The intercept is what's left of y₁ after taking away the climb from 0 to x₁.
The little numbers below, x₁ and x₂, are subscripts: they just name the first and second point.
Parallel lines have the same slope: y = 3x − 2 and y = 3x + 5 never meet, because they climb at the same rate from different starts.
Quick check
What's the slope of the line through (2, 1) and (6, 9)?
0.5
4
2
8
Show the answer
2
Rise 9 − 1 = 8, run 6 − 2 = 4, so the slope is 8 ÷ 4 = 2.
Quick check
Which line is parallel to y = 3x − 2?
y = 2x − 2
y = x/3 − 2
y = 3x + 5
y = −3x − 2
Show the answer
y = 3x + 5
Parallel lines share a slope: 3. The intercept can be anything.
In your own words
In your own words: what do the slope and the intercept each tell you about a line?
Write a sentence or two, then compare it with a good answer in the lesson.
In Python: a line from two points
Purpose. Turn two points into a line's numbers, then use the line.
It runs right here in your browser, with nothing to install.
Now step through the same two functions. This time slope names the rise and the run, so each shows up as its own variable: the same numbers as the cells in the flow above. Press Next and watch 1, 5, 3 and 9 go into slope.
With m and b known, the line is a function, and a loop prints its table:
for x inrange(0, 5):
print(x, 2 * x + 3)
Each row climbs by 2, the slope. That's the quickest way to recognize a straight line in a table: the same jump every step.
A named mistake: mixing up the order. In the slope formula, both differences must go the same way: second point minus first, on top and on the bottom. Taking y₂ − y₁ over x₁ − x₂ flips the sign, and an uphill line comes out downhill. If you swap the points, swap both, and the slope stays the same:
Both orders give 2.0: the slope belongs to the line, not to which point you call first.
Worked example
Worked example: Celsius to Fahrenheit is a line
Water freezes at (0 °C, 32 °F) and boils at (100 °C, 212 °F). Find the line through them and use it.
defslope(x1, y1, x2, y2):
return (y2 - y1) / (x2 - x1)
defintercept(x1, y1, m):
return y1 - m * x1
m = slope(0, 32, 100, 212)
b = intercept(0, 32, m)
print(m, b)
print(m * 25 + b)
In the lesson you can change this and run it.
The slope is 180 ÷ 100 = 1.8: each degree Celsius is 1.8 degrees Fahrenheit. The intercept is 32, because the first point already sits at x = 0. So F = 1.8C + 32, the same formula as lesson 17's 9C/5 + 32. Two points were enough to find it, and 25 °C comes out as 77.0 °F.
Rise −4 − 4 = −8, run 3 − (−1) = 4, so −8 ÷ 4 = −2.0.
Quick check
In a table of a straight line, what's the same in every row?
The jump in y from one row to the next
The intercept added each row
The value of y
The value of y divided by x
Show the answer
The jump in y from one row to the next
A line climbs the same amount for each equal step in x: that jump is the slope.
A line in a table, a graph and a formula. A straight line can be recognized three ways. In a table, equal steps in x give equal jumps in y. On a graph, the points sit along one straight edge. In a formula, x appears only to the first power: no x², no 1/x, and no x times another letter. Any one of these tells you the other two, and switching between them is how you'll read most charts in machine learning.
Here's the graph view of the table you printed above, drawn with the same Matplotlib lines as lesson 19:
import matplotlib.pyplot as plt
xs = [0, 1, 2, 3, 4]
ys = []
for x in xs:
ys.append(2 * x + 3)
plt.plot(xs, ys)
plt.xlabel("x")
plt.ylabel("y = 2x + 3")
plt.show()
The graph is one straight line, starting from 3 on the y-axis.
Quick check
Read the graph. How much does the line climb from x = 1 to x = 3?
2
8
4
6
Show the answer
4
From 5 up to 9 is a rise of 4, over a run of 2. Rise over run gives the slope, 4 ÷ 2 = 2, the number in front of x.
Drill
Slope and intercept from two points
Fix slope and intercept, then set m and b for the line through (2, 1) and (6, 9).
defslope(x1, y1, x2, y2):
return (y2 - y1) / (x1 - x2)
defintercept(x1, y1, m):
return y1 + m * x1
m = slope(2, 1, 6, 9)
b = intercept(2, 1, m)
print(m, b)
In the lesson, your code is checked as you write it.
In your own words
Why does it not matter which of two points you call the first?
Write a sentence or two, then compare it with a good answer in the lesson.
In AI and ML: the first model is a line
Purpose. Linear regression, the first model most people train, is y = mx + b with machine learning's letters.
In machine learning's letters, the line is
y^=wx+b
In plain words: the weight w is the slope, how much the prediction rises per unit of input. The bias b is the intercept, the prediction when the input is 0.
Take the tent days from lesson 19: (10, 22), (20, 41) and (30, 59). A line through the coolest and hottest days:
defslope(x1, y1, x2, y2):
return (y2 - y1) / (x2 - x1)
w = slope(10, 22, 30, 59)
b = 22 - w * 10print(w, b)
print(w * 20 + b, w * 25 + b)
print(41 - (w * 20 + b))
On the middle day, 20 degrees, the line predicts 40.5 rentals; the shop actually rented 41, so the line misses by 0.5. The picture shows the three days and the line, then zooms in on that miss.
Quick check
The model above is rentals = 1.85 × temperature + 3.5. What does the 1.85 mean?
It's the error on each day
It's the best temperature for rentals
Each extra degree adds about 1.85 rentals
At 0 degrees it predicts 1.85 rentals
Show the answer
Each extra degree adds about 1.85 rentals
1.85 is the weight, the slope: the predicted rentals rise 1.85 for each degree warmer.
One line, three points. The line hits the two days it was drawn through exactly, but misses the middle day by half a rental. With more days, a straight line can't usually hit every point, so a model finds the line with the smallest total miss instead. That search, for the best w and b, is what training means. Lesson 22 shows exactly why two points pin a line down and three usually can't.
In the real tools. In PyTorch, the library most models are built with, a line like this is a layer with one input and one output. Here is the model above, its weight set to 1.85 and its bias to 3.5, asked about a 20-degree day:
A slope has units. A weight of 1.85 is a rate: rentals per degree. A model's weights always carry the units of output per unit of input, which makes them readable. A house-price model with a weight of 150 on floor area says 150 dollars per square foot. Reading a weight with its units is the first check that a model makes sense: a negative weight on temperature for tent rentals would be a warning sign. Predict the model's answers at 0 and 10 degrees.
Quick check
What does this print?
w = 1.85
b = 3.5print(w * 0 + b, w * 10 + b)
0 18.5
3.5 22.0
3.5 18.5
1.85 22.0
Show the answer
3.5 22.0
At 0 degrees only the bias is left: 3.5. At 10 degrees: 18.5 + 3.5 = 22.0, which is the first data day exactly.
Drill
Read a model's line
A model's line passes through (15, 31) and (25, 51). Set w and b to its weight and bias, and pred to its prediction at 30 degrees.
w = (25 - 15) / (51 - 31)
b = 31
pred = w * 30print(w, b, pred)
In the lesson, your code is checked as you write it.
In your own words
What are a linear model's weight and bias, in the language of lines?
Write a sentence or two, then compare it with a good answer in the lesson.
31Formulas with fractions: clear them firstFreeClearing denominators, cross-multiplying, a letter in the bottom, and reciprocal formulas like 1/R = 1/R₁ + 1/R₂.35 min
37Inverse functions, lines and scalingFreeUndoing a function by making x the subject, domains that allow it, line forms, and unscaling predictions.40 min
38Sequences and sums: find n, a, d and rFreeEvery letter of the arithmetic and geometric formulas as the subject, and an average's window from its decay.40 min
40Review: rearrange anything (algebra)FreeBroken rearrangements to fix, mixed practice from the whole algebra block, a strategy chart, and its formula sheet.35 min
41Coordinates and distanceFreePoints as (x, y), Pythagoras, distance in any number of coordinates, and nearest neighbors.42 min
42Angles, degrees and radiansFreeMeasuring a turn, converting units, arcs, and the angle between two arrows.36 min
43Sine and cosineFreeAcross and up: right triangles, the unit circle, and directions and rotations.42 min
44Waves: height, speed and shiftFreeAmplitude, period and phase, sampling a wave, and position codes in transformers.37 min
68Vector equations: rearranging with arrowsFreeSolving for a vector or a scale, recovering a gradient from two weight snapshots, and an angle from a dot product.40 min
87Counting: choices, orders and groupsFreeThe counting principle, factorials, ordered picks and groups, and why attention's cost grows with the square of the length.43 min
88Probability: from counts to chancesFreeEqually likely outcomes, the complement, distributions, seeded simulation, and a next-word model.43 min
89Combining events: and, or, independentFreeThe or rule, independence, probability trees, drawing with and without replacement, and chains of steps.42 min
100Maximum likelihoodFreeLikelihoods as products, log-likelihoods as sums, grid search, underflow, and the negative log-likelihood loss.45 min
101Linear regression: the best lineFreeResiduals, least squares by formula, three roads to one line, the matrix form, and train versus test error.45 min
109One neuron, start to finishFreeWeighted sum, activation, loss, slopes and update, by hand and in code, and what one straight line can and can't split.45 min
111Training a small networkFreeThe backward pass in matrix form, gradient checks, a full training loop, and reading loss curves.45 min
112The same network in PyTorchFreenn.Sequential with the NumPy weights loaded, the same loss and slopes, then SGD and mini-batches.45 min
113Attention as matrix mathFreeScaled dot-product scores, the causal mask, a row-by-row softmax, and blending value vectors.45 min
114Residual connections and layer normFreeAdding the input back and why its slope keeps learning alive, layer norm by hand, and add and norm.40 min
115A transformer block, start to finishFreeA sentence through embeddings, attention, add and norm, a small network and next-word scores, every shape named.45 min
117Final test: transpose any formulaFreeSlips to fix from every topic, mixed practice of every kind of rearranging, the strategy chart and the full sheet.45 min
Learn the math behind AI from arithmetic up: numbers, algebra, vectors and matrices, calculus, probability and statistics, each shown as numbers moving and checked in Python, ending with a network you build and train from the formulas.
See the course →
Never trained an AI? Teach a small one to write, right in your browser. It takes about 20 seconds and needs no account. Teach it to write