A taxi charges 3 for getting in and 2 for every kilometre. Tell me how far you go and I can tell you the price: 5 km costs 13. Machine learning does the same trick with far more complicated “prices”: tell the model a photo and it tells you “cat”. Both are functions, and everything else in this course is built on them.
This is the first math lesson. No background is needed beyond basic arithmetic. We go slowly, with small numbers.
A function is a rule: one input, one output
A function is a rule that takes an input and gives back exactly one output. Picture a machine: you drop a number in the top, and one number comes out of the bottom.
Here is a machine that doubles the input and then adds 1. We give it the name and write the rule as
Read “” as “f of x”: the output of when the input is . The letter stands for whatever input you choose. To use the rule, replace by a number:
A table of values lists several inputs next to their outputs:
| (input) | ||||||
|---|---|---|---|---|---|---|
| (output) |
The one rule that makes it a function
For each allowed input there must be exactly one output. Two things follow, and students often mix them up:
- One input giving two different outputs is not allowed. If a machine gave 5 one day and 6 the next for the same input, it would not be a function.
- Two different inputs giving the same output is fine. For both and equal 4. Many inputs may share an output; no input may have two.
| Table | Function? |
|---|---|
| Yes | |
| Yes (a constant function: same output every time) | |
| No: the input 1 has two outputs |
Where the “machine” picture breaks
The machine picture is useful, but be careful about three things.
- A function does not need a formula. A phone book that lists one number per name (name → number) or a spelling dictionary (word → meaning) is a function with no equation inside. A trained neural network has a formula, but it is a huge one that no human would write by hand. What makes something a function is the one-output-per-input rule, not an equation.
- A function always gives the same output for the same input. Anything with randomness (rolling a die, or a chatbot that samples a word at random) is not a function of its input alone. For a chatbot, the probabilities it computes are a function of the input; the random pick is a separate step.
- Real machines can jam; functions can too. Some inputs are simply not allowed. We come back to that under “domain”.
Several inputs, and vectors in
Many functions take more than one input. For example, takes two numbers and gives one: . A house-price model might take size, number of rooms and age, which is three inputs.
Machine learning bundles the inputs into one list called a vector and writes it as a single bold symbol . A function from a vector to a number is then just a weighted sum:
(The dot product is the subject of P1.B3.) In this lesson we mostly use one number in, one number out, because those functions can be drawn.
Functions can also output more than one number (for example, the probabilities a classifier gives for each class). Matrices as functions on vectors come in P1.B5.
Variables versus parameters
In the letter is the input: it changes every time you use the machine. The numbers 2 and 1 are parameters: settings that you choose once and then keep fixed while you try different inputs. Change them and you get a different function from the same family.
This split is the heart of machine learning. A model is a function of the input with parameters (written , or ). Training means adjusting the parameters, never the input.
Four ways to describe a function
The same function can be shown in four ways, and you should be able to move between them:
| Way | Example for |
|---|---|
| Words | “double the input, then add 1” |
| Formula | |
| Table | |
| Graph | a straight line (next section) |
Graphs: a picture of all the input–output pairs
To draw a graph, put the input on the horizontal axis () and the output on the vertical axis (often called , or ). Every row of the table becomes a point: input across, output up.
For the points are , , , , , . They lie on a straight line. Joining up all inputs, not just the whole numbers, gives the line itself. The graph of a function is the set of all points .
How to read a graph:
- To find : go to on the horizontal axis, go straight up or down to the curve, then read the height.
- To solve : draw a horizontal line at height and see where it meets the curve. Those crossings are the inputs that give output .
- The y-intercept is where the graph meets the vertical axis, . An x-intercept (or root) is where it meets the horizontal axis, an input with . For the root is , because .
The vertical line test
The one-output rule has a picture: a curve is the graph of a function exactly when every vertical line crosses it at most once. If some vertical line crosses twice, that input has two outputs.
A vertical line that crosses zero times is fine: that input is simply not allowed (it is outside the domain, next).
Domain and range
Not every number can be fed into every function.
- The domain is the set of allowed inputs.
- The range is the set of outputs that actually occur.
For every number works as an input and every number can come out, so both are “all real numbers” ( is the symbol for all real numbers, the whole number line). Other functions are pickier:
| Function | Domain (allowed ) | Range (outputs) | Why |
|---|---|---|---|
| all real numbers | all real numbers | nothing can go wrong | |
| all real numbers | or more | a square is never negative | |
| or more | or more | no real number squares to a negative | |
| every number except | every number except | you cannot divide by 0 | |
| greater than | all real numbers | a logarithm needs a positive input (P1.A2) | |
| all real numbers | greater than | a power of 2 is always positive | |
| (ReLU) | all real numbers | or more | it clips negatives to 0 (P4.4) |
| (sigmoid) | all real numbers | between and | squashes everything into a probability-like range (P1.A5) |
Why does this matter in practice? If you feed a negative number to a logarithm or a square root, or divide by zero, you get an error or nan (“not a number”) in code, and a training run can be ruined. Knowing the domain tells you where a formula is safe.
Interval notation writes domains and ranges compactly. A square bracket includes the end, a round bracket excludes it: means 0 or more (0 is allowed); means strictly more than 0 (0 is not allowed). The symbol (“infinity”) is not a number, so it always gets a round bracket. (P1.A4 covers more notation.)
A domain can also come from meaning, not only from arithmetic: a function that gives the price of a taxi ride from the distance in km only makes sense for .
Finding the domain of a formula: ask “is there any input that makes me divide by zero, take the square root of a negative number, or take the logarithm of something that is not positive?” Exclude exactly those. Example: needs , so the domain is (and itself is allowed, since ).
The shapes you will meet most
Five families cover most of what machine learning draws. Use the plotter: pick a family, move the sliders, and drag the pink marker to see the input turn into the output .
Lines: slope and intercept
A linear function has the form
Its graph is a straight line. The two parameters have plain meanings:
- , the slope, is the change in output per 1 step of input. The taxi example has slope 2: every extra kilometre adds 2 to the price.
- , the intercept, is the output when the input is 0, (the starting charge, 3 in the taxi).
Given two points and on a line, the slope is “rise over run”:
For and : , and then , so the line is again. A positive slope means the line goes up as you move right (increasing); a negative slope means it goes down; slope 0 is a flat, constant function.
A vertical line (like “”) is not a function of : it would give every output for one input. It fails the vertical line test.
In machine learning the same formula appears as , with the weight playing the role of the slope and the bias the intercept (P0.2, P3.A2).
Parabolas: the shape of squared error
The simplest quadratic function is :
The graph is a U-shaped curve called a parabola. It is mirror-symmetric (the output for equals the output for ), and its turning point at the origin is called the vertex.
Written in vertex form,
the parameters say where the vertex is and how the curve opens:
- the vertex is the point ,
- : opens upward, and the vertex is the lowest point (the minimum),
- : opens downward, and the vertex is the highest point (the maximum),
- a larger makes the parabola narrower and steeper.
Example: has its vertex at , opens upward, and its lowest output is 1, reached at .
Why parabolas matter for ML. Squared error is a parabola. If the true value is 5 and you predict 3, the error is 2 and the squared error is . Predict 1 and the error is 4, but the squared error is : doubling the error quadruples the penalty. Over- and under-shooting by the same amount cost the same, because . That is the mirror symmetry again.
Go deeper Vertex form versus the form ax² + bx + c
Multiply out . So a parabola is often written , where and . The vertex then sits at . Check with : here , , , and ✓. The graph crosses the horizontal axis (has real roots) 0, 1 or 2 times. For an upward parabola: no roots if the vertex is above the axis, one root if it is on the axis, two roots if it is below. For a downward parabola it is the other way round.
Exponentials: multiplying instead of adding
An exponential function has the form
Here the input sits in the exponent. Take :
The pattern: every +1 in the input multiplies the output by (here 2). Compare a line, where every +1 adds the slope.
- : exponential growth. The output grows slowly at first, then explodes.
- : exponential decay. For the outputs are : they shrink toward 0 but never reach it.
- is the starting value, the output at (since ).
- The base is required to be positive: a negative base like is not defined for fractional such as , so it is not a smooth curve.
- The output of with is always positive.
Exponentials appear in compound interest, doubling populations, radioactive decay and, in ML, inside the sigmoid (P1.A5) and softmax (P3.A8).
Absolute value, steps and piecewise functions
Some functions use different rules on different parts of the input. They are called piecewise functions, and ML loves them.
Absolute value is the distance of from 0, so it removes the sign:
Its graph is a V with a sharp corner. is the “size of the error, ignoring the sign”.
The ReLU function (rectified linear unit), , outputs 0 for negative inputs and the input itself otherwise: ReLU of is 0 and ReLU of 3 is 3. Written in pieces: if , and if . It is one of the most-used activation functions in neural networks (P4.4).
A step function jumps between fixed values at a threshold: output 0 below the threshold and 1 at or above it. Early artificial neurons, such as the perceptron, used a step (P4.1). In the plotter, the “Step” family has a threshold with a filled dot (the value at the jump) and an open dot (the value skipped). A step function is a valid function, even though its graph has a gap: the vertical line test still holds.
Intuition
Each family tells a different story about how the output reacts when you nudge the input:
- Line: always the same reaction. Each +1 in changes the output by the same amount.
- Parabola: the reaction grows the further you go from the vertex, in both directions.
- Exponential: the reaction is proportional to what you already have. Growth feeds on itself.
- Absolute value, ReLU, step: the rule changes at a corner or a jump. Away from that point they are simple (a line, or flat).
The “story” picture is only a guide: real functions can combine these pieces.
Math
| Family | Formula | Key parameters |
|---|---|---|
| Linear | slope , intercept | |
| Quadratic | vertex ; opens up if , down if | |
| Exponential | , | start value ; growth if , decay if |
| Absolute value | corner at | |
| Step | for , for | threshold |
Domain: all real numbers for every row. Range: lines (with ): all numbers; parabola with : ; exponential with : when ; step: just the two values.
Code
def line(x, m, b):
return m * x + b
def parabola(x, a, h, k):
return a * (x - h) ** 2 + k
def expo(x, a, b):
return a * b ** x
line(3, 2, 1) # 7
parabola(3, 1, 3, 1) # 1 (the vertex of (x-3)^2 + 1)
expo(3, 1, 2) # 8The parameters are just extra arguments. Training a model changes m and b, not x.
Increasing, decreasing, and the lowest point
A function is increasing on an interval if a bigger input gives a bigger output there (the graph climbs as you move right), and decreasing if a bigger input gives a smaller output (the graph falls).
- is increasing everywhere; is decreasing everywhere.
- is increasing everywhere; is decreasing everywhere.
- is decreasing for negative and increasing for positive . It turns around at .
A point where the output is the smallest is a minimum; the largest is a maximum. A function may have neither: the line never stops going up. has a minimum of 0 at and no maximum. The input where the minimum happens is called the argmin; the output is the minimum value. For the minimum value is 1 and the argmin is .
This is the bridge to optimization. In machine learning the loss measures how wrong a model is. Take a one-parameter model and one data point: input , true value . The prediction is and the squared error is a function of the parameter :
It is a parabola with its minimum at (the model predicts exactly). Training is the search for the input of the loss function where its output is as small as possible. (For this simple bowl there is exactly one lowest point; for real models, training usually finds a good low point rather than a guaranteed lowest one, P1.F2.) Which direction to step when you are not at the bottom is decided by the slope of the graph: that is what derivatives (P1.C1) measure and gradient descent (P1.F3) uses. Optimization is introduced in P1.F1.
Transforming graphs: shift, stretch, flip
Once you know a basic shape, a few small edits to the formula move or reshape it. Start with a base function and build
What each part does:
| Change | Looks like | What happens |
|---|---|---|
| outside | graph moves up by (down if is negative) | |
| outside | heights are multiplied by : stretched if , squashed if ; flips it upside down | |
| inside | graph moves right by (left if is negative) | |
| inside | graph is squeezed sideways if , stretched if ; mirrors it left to right (about the -axis; in the widget, about the line once you have shifted it) |
The rule of thumb. Changes made outside act on the output (the vertical direction) and do what they look like: goes up. Changes made inside act on the input (the horizontal direction) and feel backwards: moves the graph to the right, not the left.
Why backwards? Take and . The lowest point of happens where . For the lowest point needs , so : whatever did at 0, does at 2. And check: and . The whole picture slid 2 to the right.
This explains vertex form: is just stretched by , moved right by and up by .
Composition: one machine feeding the next
Composition means feeding the output of one function into another. Take two machines:
- (double it)
- (add three)
Write , read “ of of ”: do first (the inner function), then (the outer function). The order of work is from the inside out, which is the reverse of the order you read the letters in.
Same two machines, opposite orders, different answers: . In general . Here, but .
Some pairs happen to agree at one input (or for all inputs: and in either order give ), but the order matters in general. An easy example: “put on your socks, then your shoes” is not the same as “shoes, then socks”.
The domain of a composition needs both steps to work: must be allowed in the inner function, and its output must be allowed in the outer one. For and , the composition needs .
Neural networks are compositions. A network with two layers computes “layer 1, then layer 2”: . Each layer is a simple function; stacking dozens of them builds very complicated ones (P4.3, P4.5).
Inverse functions: undoing a function
An inverse function undoes what a function did. If turns 4 into , the inverse turns 11 back into 4. To find it, solve for :
In words: doubles and adds 3, so the inverse subtracts 3, then halves (the steps undone in reverse order, like taking off shoes before socks).
- Not every function has an inverse. sends both 2 and to 4, so given the output 4 you cannot tell which input it came from. An inverse needs each output to come from exactly one input. (Graph test: every horizontal line crosses at most once.) If you only allow , the inverse exists: it is .
- Famous pair: the inverse of is , because and (P1.A2).
- Notation warning: is not . For : but . The small “” means “inverse function”, not “to the power minus one”.
Try it in Python
Python’s def defines a function. NumPy applies it to many inputs at once. Run the cell, then change the numbers.
Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).
Challenge: define relu(x) as np.maximum(0, x) and print relu(xs - 1). Then define inverse_f(y) for f above and check that inverse_f(f(4)) gives back 4.
Putting it together
A model is a function from inputs to predictions. Its parameters are the knobs on the machine; the loss is a second function that takes those knobs and says how bad the predictions are. Learning is looking for the knob settings where the loss is lowest. Everything in this lesson (inputs and outputs, domains, shapes, shifting and stretching, composition) comes back, again and again, in the rest of the course.
Check your understanding
Which table can NOT describe a function of ?
The input 1 appears twice with different outputs (3 and 4). A function must give exactly one output for each input. In the table with outputs 5, 5, 5, different inputs share an output, which is allowed (a constant function).
What is the domain of (using real numbers)?
The square root needs a non-negative input, so , which means . The value is allowed because .
Let and . What is ?
Work from the inside out: , then . The value 5 is , the other order.
For , what happens to the output when the input goes up by 1?
. In an exponential , each +1 in multiplies the output by (here 2); the 3 is only the starting value at .
How does the graph of compare with the graph of ?
Whatever did at input 0, now does at input 3, so the picture slides right. Changes inside the brackets move the graph horizontally and feel backwards; changes outside () move it vertically.
Exercises
- For , find , and .
Show answer
, , . - A table gives the inputs and the outputs . Assuming it comes from a line, find its formula and .
Show answer
The output rises by 3 each step, so the slope is 3, and is the intercept: . So . - Is the set of pairs a function? And ?
Show answer
The first is not: the input 1 has two outputs (2 and 4). The second is: each input has exactly one output (the same one each time). - Find the domain of and of .
Show answer
needs , so . cannot divide by zero, so every number except . - For the parabola , give the vertex, say whether it opens up or down, and find .
Show answer
Write it as , so and : the vertex is . It opens upward (), so is the minimum. . - With and , compute and , and write formulas for and .
Show answer
and . The formulas are and . - Find the inverse of and check it at .
Show answer
Solve for : , so . Check: and ✓. - A culture has 100 bacteria and doubles every hour. Write the count after hours and find it for . How does it compare with a plan that adds 100 per hour?
Show answer
, so . Adding 100 per hour gives after 3 hours. The doubling plan pulls far ahead as grows. - Describe how to get the graph of from the graph of .
Show answer
Move right by 3, flip it upside down (the minus sign outside), and move it up by 2. The vertex is and, since it opens downward, 2 is the maximum. - Compute ReLU, ReLU, and the values of at and .
Show answer
ReLU and ReLU. For : at , ; at , . - In the plotter, choose the line family and set it to (slope 2, intercept 1). Move the marker until the output is 3 and read off the input. Check it by solving .
Show answer
The marker is at , and ✓. Algebra: , so .