A taxi charges ₹50 to start plus ₹15 per kilometre. Plot the fare against the distance, and you get a straight line. Two numbers describe that line completely: where it starts (₹50, before you've gone anywhere) and how steeply it climbs (₹15 for every extra kilometre). These are the line's intercept and slope.
Straight lines are the most important graphs in data science. Linear regression, the first model most people learn, is literally "find the best slope and intercept". And slope, the idea of "how much does the output change when the input changes by 1?", grows up to become the derivative in calculus and the gradient in machine learning.
This lesson explains the equation y = mx + c, what slope and intercept mean in real terms, how to find a line from two points, and how to fit a line to messy real data with NumPy.
What you need first: how to read any graph and solve linear equations.
Key terms
- Linear: forming a straight line.
- Slope (or gradient), m: how much y changes when x increases by 1. "Rise over run".
- Intercept (or y-intercept), c: the value of y when x = 0; where the line crosses the y-axis.
- x-intercept: where the line crosses the x-axis, where y = 0.
- Line of best fit: the straight line that comes closest to a set of data points.
The equation of a straight line
Every non-vertical straight line can be written as:
y = mx + c
- m is the slope.
- c is the intercept.
The taxi fare is y = 15x + 50: slope 15, intercept 50. (Some countries and textbooks write y = mx + b; it's the same thing. In ML, you'll see y = wx + b: w for weight, b for bias.)
Slope: rise over run
Slope measures steepness as a number:
slope = rise ÷ run = (change in y) ÷ (change in x)
Between any two points on a line, go across (the run) then up or down (the rise).
For the taxi: from 2 km (₹80) to 6 km (₹140): rise = 60, run = 4, slope = 60 ÷ 4 = 15. Each extra kilometre adds ₹15. Pick any two points on a straight line and you get the same slope; that's what makes it straight.
The formula for two points (x₁, y₁) and (x₂, y₂):
m = (y₂ − y₁) / (x₂ − x₁)
What the slope's sign tells you
| Slope | Line | Meaning | Example |
|---|---|---|---|
| Positive | rises left to right | more x, more y | study hours → score |
| Negative | falls left to right | more x, less y | car age → resale value |
| Zero | flat | y doesn't depend on x | a flat ₹99 subscription |
| Larger number (either sign) | steeper | y changes faster |
Slope always has units
The slope's units are y units per x unit. That makes it meaningful:
- Taxi: ₹15 per km.
- A car losing ₹40,000 of value per year: slope −40,000 ₹ per year.
- Speed is the slope of distance against time: km per hour.
Whenever you see "per", you're looking at a slope.
Intercept: the starting value
The intercept is the value of y when x = 0. For the taxi, it's ₹50: the fare for a 0 km ride, the flag-down charge.
Sometimes x = 0 makes no real sense. If a model predicts weight from height, the intercept is the weight of someone 0 cm tall, which is meaningless. The intercept still matters mathematically, because it positions the line, but don't always read meaning into it.
Drawing lines
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-4, 4, 50)
fig, ax = plt.subplots()
ax.plot(x, 2 * x + 1, label="y = 2x + 1 (slope 2)")
ax.plot(x, 2 * x - 2, ls="--", label="y = 2x − 2 (same slope: parallel)")
ax.plot(x, -0.5 * x + 3, label="y = −0.5x + 3 (negative slope)")
ax.plot(x, np.full_like(x, -3), label="y = −3 (zero slope)")
ax.axhline(0, color="grey", lw=0.8)
ax.axvline(0, color="grey", lw=0.8)
ax.plot([0, 0, 0], [1, -2, 3], "ko")
ax.set_ylim(-6, 8)
ax.set_title("Slope sets the steepness; intercept sets where it crosses x = 0")
ax.legend(fontsize=8)
plt.show()
Notice:
- Parallel lines have the same slope: y = 2x + 1 and y = 2x − 2 never meet. (That's why the simultaneous equations they form have no solution.)
- Each line crosses the y-axis at its intercept.
Finding the equation from two points
A line passes through (2, 5) and (6, 13). Find its equation.
- Slope: m = (13 − 5)/(6 − 2) = 8/4 = 2.
- So y = 2x + c. Substitute either point, say (2, 5): 5 = 2(2) + c, so c = 1.
- The line is y = 2x + 1.
- Check with the other point: 2(6) + 1 = 13 ✓.
def line_through(p1, p2):
(x1, y1), (x2, y2) = p1, p2
m = (y2 - y1) / (x2 - x1)
c = y1 - m * x1
return m, c
print(line_through((2, 5), (6, 13)))
print(line_through((0, 100), (5, 60)))
(2.0, 1.0)
(-8.0, 100.0)
The second line starts at 100 and loses 8 per step: like a battery losing 8% an hour.
Finding the x-intercept
Where does y = 2x − 6 cross the x-axis? There, y = 0, so solve 0 = 2x − 6: x = 3. In a real problem, this answers "when does it hit zero?": when does the battery die, when does the loan get paid off.
Fitting a line to real data
Real data is never perfectly straight. Here are five students' study hours and exam scores:
| Hours | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Score | 52 | 55 | 61 | 64 | 68 |
No single line passes through all five points. Instead, we find the line that comes closest to them, the line of best fit. NumPy's polyfit does it (the 1 means "a straight line"):
hours = np.array([1, 2, 3, 4, 5])
scores = np.array([52, 55, 61, 64, 68])
m, c = np.polyfit(hours, scores, 1)
print(round(m, 2), round(c, 2))
print(round(m * 6 + c, 1))
fig, ax = plt.subplots()
ax.scatter(hours, scores, s=60, label="students")
xs = np.linspace(0, 6, 50)
ax.plot(xs, m * xs + c, color="tab:orange", label=f"best fit: y = {m:.1f}x + {c:.1f}")
ax.set_xlabel("Hours studied")
ax.set_ylabel("Exam score")
ax.set_title("A line of best fit through real data")
ax.legend()
plt.show()
4.1 47.7
72.3
Reading the result like a data scientist:
- Slope 4.1: each extra hour of study goes with about 4.1 more marks.
- Intercept 47.7: the predicted score with zero study (here, slightly outside the data, so treat it with care).
- Prediction: 6 hours → 4.1 × 6 + 47.7 ≈ 72.3 marks.
That's linear regression. "Closest" here means the line with the smallest total of squared vertical gaps to the points, the same squared-error idea from quadratic equations. Linear regression builds it out with scikit-learn, and multiple linear regression extends it to many inputs, each with its own slope.
Slope beyond straight lines
A curve doesn't have one slope: it's steeper in some places than others. But zoom in far enough on any smooth curve and it looks straight, so it has a slope at each point. That point-by-point slope is the derivative, the core of the Calculus course and of gradient descent, where a model follows the slope downhill to reduce its error.
Practice
- What are the slope and intercept of y = −3x + 7? Does the line rise or fall?
Show the answer
Slope −3, intercept 7. It falls (negative slope).
- Find the slope between (1, 4) and (5, 16).
Show the answer
(16 − 4)/(5 − 1) = 12/4 = 3.
- Find the equation of the line through (0, 20) and (4, 0), and its x-intercept.
Show the answer
Slope (0 − 20)/(4 − 0) = −5, intercept 20: y = −5x + 20. The x-intercept is 4 (it passes through (4, 0)).
- A model predicts house price (₹ lakh) as 0.07 × area + 15. Interpret 0.07 and 15.
Show the answer
Each extra square foot adds about ₹0.07 lakh (₹7,000) to the price. 15 is the intercept, the predicted price at zero area, which mainly positions the line.
- Are y = 4x + 1 and y = 4x − 9 parallel? How about y = 4x + 1 and y = −4x + 1?
Show the answer
The first pair are parallel (same slope, 4). The second pair aren't: their slopes differ, and they cross at (0, 1).
Key takeaways
- A straight line is y = mx + c: m is the slope, c the intercept.
- Slope = rise ÷ run = (y₂ − y₁)/(x₂ − x₁): the change in y per 1 unit of x, with units.
- Positive slopes rise, negative slopes fall, zero is flat; parallel lines share a slope.
- The intercept is y when x = 0.
np.polyfit(x, y, 1)finds the line of best fit: that's linear regression.
Frequently asked questions
What is the slope of a line?
The amount y changes when x increases by 1, calculated as rise over run: (y₂ − y₁)/(x₂ − x₁). A positive slope rises, a negative slope falls, and zero is flat.
What is the y-intercept?
The value of y where the line crosses the y-axis, which is where x = 0. In y = mx + c, it's c.
What does slope mean in linear regression?
The predicted change in the target for a one-unit increase in the feature. A slope of 4.1 for study hours means each extra hour goes with about 4.1 more marks.
What to learn next
- Draw any function yourself in plot mathematical functions with Python.
- Revisit how to read any graph.