Lesson 7 of 12 · Functions and Graphs
Linear Functions
Understand linear functions y = mx + b: slope as rise over run, the y-intercept, finding a line through two points, and how this is exactly linear regression's model. Interactive graphs and Python.
- 8 min read
- Beginner
- Reviewed
- What a linear function is
- What slope and intercept mean, and how to read them from a graph
- How to work out the slope from two points
- How to find the equation of a line through two points
- Why the straight line is the first model every data scientist learns
Straight lines
The two numbers in y = mx + b control everything about the line:
Move the sliders and watch what each number does:
What you should notice:
- A positive slope goes up from left to right. A negative slope goes down.
- A bigger slope (ignoring the sign) means a steeper line.
- A slope of 0 gives a flat, horizontal line: y never changes.
- Changing
bmoves the whole line up or down without tilting it.
Slope as rise over run
change in y y₂ - y₁
slope m = ----------- = -------
change in x x₂ - x₁For the points (1, 3) and (4, 9): the rise is 9 - 3 = 6, the run is 4 - 1 = 3, so the slope is 6 ÷ 3 = 2. For every 1 step to the right, the line goes up 2.
def slope(x1, y1, x2, y2):
return (y2 - y1) / (x2 - x1)
print(slope(1, 3, 4, 9))
print(slope(0, 5, 2, 1)) # going down: negative slope
print(slope(-1, 4, 3, 4)) # flat: slope 0The line through two points
Once you know the slope, you can find b by putting one point into y = mx + b and solving:
y = mx + b
3 = 2 × 1 + b use the point (1, 3) and m = 2
b = 1So the line is y = 2x + 1.
def line_through(x1, y1, x2, y2):
m = (y2 - y1) / (x2 - x1)
b = y1 - m * x1
return m, b
m, b = line_through(1, 3, 4, 9)
print(f"y = {m}x + {b}")
# Check: does the line really pass through both points?
print(m * 1 + b, m * 4 + b)A line is a model
This is the first model in machine learning: linear regression. It assumes the output changes at a steady rate with the input.
# Ice cream sales against temperature, roughly following a line
temperatures = [20, 24, 28, 32]
sales = [110, 150, 190, 230]
m = (sales[1] - sales[0]) / (temperatures[1] - temperatures[0])
b = sales[0] - m * temperatures[0]
print(f"Model: sales = {m} x temperature - {-b}")
for t in [26, 35]:
print(f"At {t} °C, predicted sales: {m * t + b:.0f}")The slope has a real meaning: every extra degree sells about 10 more ice creams. Real data never sits perfectly on a line, so linear regression finds the line that is closest to all the points at once. You will learn how in the machine learning courses.
Common mistakes
Find a line from two points, then use it as a prediction model.
# A taxi company charges a fixed amount plus a price per km. # A 2 km trip costs 74 and a 6 km trip costs 122. # 1. Find the slope (price per km) and the intercept (fixed charge). # 2. Predict the cost of a 10 km trip. # Output must be exactly: # Price per km: 12.0 # Fixed charge: 50.0 # 10 km costs: 170.0
In y = -2x + 5, what are the slope and the y-intercept?
Summary
- A linear function
y = mx + bhas a straight-line graph. - The slope m is how much y changes for each step in x. The y-intercept b is where it crosses the y-axis.
- Work out the slope with rise over run:
(y₂ - y₁) / (x₂ - x₁). - Two points are enough to find the whole line.
- Linear regression fits the best line to data. Its slope and intercept are called the weight and the bias.
Next: curves, and the special functions that let neural networks learn.
Finished reading? Tick it off to track your progress.