The maths you need · lesson 1 of 12
Parts in this lesson
Adef f(x): return 2 * x + 1a function, as code
Bnp.exp(x), np.log(x)the exponential and natural log, on whole arrays
C1 / (1 + np.exp(-z))the sigmoid
Dnp.log(probs).sum()a product, done as a sum of logs

Functions and graphs

Last updated

A model is a function: numbers go in, a number comes out. This stage teaches the maths the rest of the course is written in, and it starts here, with what a function is and the four you'll meet most: straight lines, the exponential, the logarithm and the sigmoid. Each one gets a graph you can change, then a sentence, then the symbols, then NumPy.

By the end of this lesson you can

  • Read f(x) notation and work out a function's value by hand and in NumPy
  • Say what the slope and intercept of a line do to its graph
  • Describe the shapes of eˣ, ln x and the sigmoid, and where each is used
  • Explain why adding logs is safer than multiplying many small probabilities

What a function is

A function takes an input and gives back one output, by a fixed rule. In maths it's written f(x), said "f of x": f is the rule's name and x is the input. For example:

f(x)=2x+1

In words: double the input, then add 1. So f(3)=2×3+1=7. In Python it's a def, and with NumPy the same function works on a whole array of inputs at once (lesson 0.4):

function.ipynb

Every model in this course is a function like this, just with more inputs and a more complicated rule. A penguin's measurements go in, a probability of "Gentoo" comes out. Training is choosing the numbers inside the rule (here, the 2 and the 1) so the outputs come out right.

Lines: y = mx + b

The simplest useful function is a straight line:

y=mx+b

In words: multiply the input by m, then add b. m is the slope: how much y goes up for each step of 1 in x. b is the intercept: the value of y where the line crosses the y-axis, at x=0. In NumPy: y = m * x + b.

Change them and watch:

Function plottery = 1x + 0
−3−2−1123−2−11234

At x = 1, the line is 1. Each step of 1 to the right changes y by the slope, 1.

A negative slope makes the line fall; a slope of 0 makes it flat. Changing b slides the line up and down without turning it.

Stage 3's first model is exactly this line, fitted through data: m and b are found by training. With many inputs it becomes yˆ=w⋅x+b, the weighted sum from lesson 0.6.

Powers, eˣ and ln x, and the sigmoid

Three more functions turn up all through the course. Use the buttons on the plotter above to see each, then run these:

functions.ipynb
  • The exponential, ex, is the number e≈2.718 raised to the power x. It's always above 0, it's 1 at x=0, and it grows faster and faster. Softmax (Stage 3), which turns scores into probabilities, is built from it.
  • The natural logarithm, ln⁡x (NumPy's np.log), undoes the exponential: ln⁡x is the power you'd raise e to, to get x. So ln⁡1=0, ln⁡e=1, and ln⁡100≈4.605 because e4.605≈100. It only works for x>0. Every loss for classifiers in this course is built from it.
  • The sigmoid, written σ (the Greek letter sigma), squeezes any number into the range 0 to 1:
σ(z)=11+e−z

In words: take e to the power minus z, add 1, and divide 1 by that. In NumPy: 1 / (1 + np.exp(-z)). A big positive z gives nearly 1, a big negative one nearly 0, and z=0 gives exactly 0.5. That's why it turns a score into a probability: logistic regression (Stage 3) and the first neural networks use it.

Here are all three drawn with matplotlib:

functions.ipynb

Why ln turns products into sums

The log has one property the course depends on: the log of a product is the sum of the logs.

ln⁡(ab)=ln⁡a+ln⁡b

In words: multiplying two numbers and then taking the log gives the same answer as taking each log and adding. In NumPy: np.log(a * b) equals np.log(a) + np.log(b). Run it:

logs.ipynb

Why it matters: training a model often means multiplying many probabilities together, one for each example (lesson 1.10). Each is below 1, so the product shrinks fast.

  • 1,000 probabilities of 0.5 multiply to about 9.3×10−302 (the e-302 means "times 10 to the −302").
  • 2,000 of them give exactly 0.0. The true answer isn't 0, but it's smaller than the smallest number a float64 can hold, so it underflows to 0. Every model would look equally bad.
  • Adding the logs instead gives −1386.29, an ordinary number with no trouble at all.

So the course almost never multiplies probabilities. It adds their logs. You'll see "log-likelihood" and "log-probability" everywhere from Stage 3 on, and this is the reason.

Wrong way: the log of 0

ln⁡x has no value at x=0: no power of e gives 0. Here's what happens if you ask anyway:

lnzero.ipynb
  • NumPy's np.log(0.0) gives -inf, minus infinity, and carries on. On your own computer it also prints a RuntimeWarning: divide by zero encountered in log, which is easy to miss in a long notebook.
  • Python's own math.log(0) stops with a ValueError.

NumPy's quiet -inf is the dangerous one. It spreads: -inf plus any number is -inf, and 0 * -inf is nan (not a number). One probability of exactly 0 in a model's output can turn the whole loss into nan, and training stops making sense. Lesson 1.11 shows the standard fix.

Check what you learned

For f(x)=3x−2, what is f(4)?
What is σ(0), the sigmoid of 0?
Predict, then check in step 4’s notebook: is np.log(0.2 * 0.5) the same as np.log(0.2) + np.log(0.5)?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning