DS Linear Functions
Much of data science rests on a very old idea: a function that maps an input to an output. The simplest useful kind is the linear function — a straight line — and it is the heart of linear regression, which you'll meet in the DS Advanced section.
What is a function?
A function takes an input (often called x) and produces exactly one output (y or f(x)). Riverside Bikes' price list is a function: the price of a ride depends on how long it lasts.
Unlocking a bike costs €1.50, then every minute costs €0.20.
Written as a formula:
price(m) = 0.20 × m + 1.50In Python a mathematical function becomes a Python function:
def price(minutes):
return 0.20 * minutes + 1.50
for m in [0, 10, 25, 60]:
print(f"{m:>3} min -> EUR {price(m):.2f}") 0 min -> EUR 1.50
10 min -> EUR 3.50
25 min -> EUR 6.50
60 min -> EUR 13.50
The general form
Every linear function can be written as
f(x) = a × x + b| Part | Name | In the price example |
|---|---|---|
x |
input, independent variable | ride length in minutes |
f(x) |
output, dependent variable | price |
a |
slope — change in output per one unit of input | €0.20 per minute |
b |
intercept — output when x = 0 |
€1.50 unlock fee |
Its defining property: equal steps in x always give equal steps in y. Ten extra minutes always add €2.00, no matter whether the ride was 5 or 50 minutes long.
import numpy as np
minutes = np.arange(0, 61, 10)
prices = 0.20 * minutes + 1.50
print("minutes:", minutes)
print("prices: ", prices.round(2))
print("steps: ", np.diff(prices).round(2))minutes: [ 0 10 20 30 40 50 60]
prices: [ 1.5 3.5 5.5 7.5 9.5 11.5 13.5]
steps: [2. 2. 2. 2. 2. 2.]
np.diff computes the difference between neighbouring values. All steps are equal (2.0), which is exactly what "linear" means. Notice that NumPy evaluates the formula for all inputs at once.
Linear versus non-linear
Not every relationship is a straight line. Compare a flat-rate "day pass" plan and a hypothetical surge plan where the per-minute price rises the longer you ride:
import numpy as np
minutes = np.array([0, 15, 30, 45, 60])
linear = 0.20 * minutes + 1.50
surge = 0.004 * minutes ** 2 + 1.50 # quadratic, not linear
day_pass = np.full(len(minutes), 9.00) # constant: slope 0
print("linear: ", linear, " steps", np.diff(linear))
print("surge: ", surge, " steps", np.diff(surge))
print("day pass:", day_pass, " steps", np.diff(day_pass))linear: [ 1.5 4.5 7.5 10.5 13.5] steps [3. 3. 3. 3.]
surge: [ 1.5 2.4 5.1 9.6 15.9] steps [0.9 2.7 4.5 6.3]
day pass: [9. 9. 9. 9. 9.] steps [0. 0. 0. 0.]
- The linear plan has constant steps.
- The surge plan's steps keep growing — it curves upward.
- The day pass is linear with slope
0: a horizontal line.
Several inputs
A linear function may have more than one input. The manager's rough rule of thumb for daily rentals might be:
rides = 150 + 12 × temp − 8 × rainEach input has its own slope: +12 rides per degree, −8 rides per millimetre of rain. This is the form of multiple linear regression, where the computer estimates those slopes from data.
def expected_rides(temp_c, rain_mm):
return 150 + 12 * temp_c - 8 * rain_mm
print(expected_rides(20, 0)) # a mild, dry day
print(expected_rides(20, 10)) # same temperature, wet
print(expected_rides(25, 0) - expected_rides(24, 0)) # effect of +1 degree390
310
12
The last line shows how to read a slope: hold everything else fixed, change one input by one unit, and the output changes by the slope.
Why data scientists care
Linear functions are simple, but that is their strength:
- Their parameters have a plain-language meaning ("+12 rides per degree").
- They are easy to compute and to explain to non-specialists.
- Many real relationships are roughly linear over the range you care about.
The next lesson draws them; the one after shows how to find slope and intercept from data.
Test yourself
In f(x) = 3x + 7, what is the intercept?
- 3
- 7
- 10
- x
For the price function 0.20 · m + 1.50, how much more does a 40-minute ride cost than a 30-minute ride?
- €0.20
- €1.50
- €2.00
- €3.50
Which function is NOT linear?
y = 5y = -2x + 1y = x² + 1y = 0.5x