Python for data · lesson 4 of 11
Parts in this lesson
AX[:, 2] / 10maths on every element
Bnp.exp(z)a function on every element
CX.mean(axis=0)one result per column
Dscores.argmax(axis=1)the position of the biggest, in each row

Maths on whole arrays

Last updated

Every model in this course is a few lines of maths done on whole arrays: multiply, add, take an exponential, average, pick the biggest. In this lesson you'll do element-by-element maths without a loop, summarise an array along one direction with an axis, and use argmax to turn a model's scores into an answer.

By the end of this lesson you can

  • Do arithmetic and functions like np.exp on every element at once
  • Sum, average and find the largest value along axis=0 or axis=1
  • Say which axis disappears when you summarise along it
  • Use argmax to pick the highest-scoring class for each example

Element by element

Arithmetic between an array and a number, or between two arrays of the same shape, works element by element: the first with the first, the second with the second. So do NumPy's maths functions. Run each cell:

elementwise.ipynb
  • X[:, 2] / 10 turns every flipper length from millimetres to centimetres.
  • [1, 2, 3] * [10, 20, 30] multiplies in pairs: 10, 40, 90. It doesn't do anything clever with rows and columns; that's @, in lesson 0.6.
  • np.exp, np.log and np.sqrt work on every element. np.exp(1) is e≈2.718, and np.log undoes it: log⁡e=1.

You'll meet exp and log constantly. The sigmoid function that turns a score into a probability (Stage 3) is 1 / (1 + np.exp(-z)), and the loss used to train almost every classifier in the course is built from np.log. Both are one line, for a whole array, because of what you just ran.

Summaries along an axis

sum, mean and max can work along one direction of a 2-D array. Step through the picture first: sales from three stores over four days.

Summing along axis 0 and axis 1. Kilos sold by 3 stores (rows) over 4 days (columns). Shape (3, 4): axis 0 runs down the rows, axis 1 across the columns.423845403533313729303428sales: 3 stores × 4 daysMonTueWedThuBatangasLipaTanauan
1 of 4 Kilos sold by 3 stores (rows) over 4 days (columns). Shape (3, 4): axis 0 runs down the rows, axis 1 across the columns.

The rule: the axis you name is the one that disappears. Now the penguins:

axis.ipynb
  • X.mean(axis=0) collapses the rows: one mean per feature. The average bill length is 44.24 mm, bill depth 16.7 mm, flipper length 200 mm. Shape (5, 3) becomes (3,).
  • X.max(axis=0) is the largest value of each feature.
  • X.sum(axis=1) collapses the columns: one total per penguin. Shape (5,). (Adding millimetres of bill to millimetres of flipper doesn't mean much; it's here to show the direction.)

The mean of feature j over n examples is the sum down column j, divided by n:

x¯j=1n∑i=1nXij

In words: for column j, add the value in every row i, from 1 to n, and divide by n. Xij is the number in row i, column j. In NumPy, all the columns at once: X.mean(axis=0). The ∑ over i (the rows) is axis=0.

axis=0, one result per feature, is the one you'll use most: to find each feature's mean and spread before training (lesson 0.5), and to see whether one feature is in kilograms while another is in millimetres.

Picking a winner with argmax

A classifier doesn't output "Gentoo". It outputs a score for every class, and the answer is the class with the highest score. Here are scores for 4 penguins and 3 species. Run it:

argmax.ipynb

argmax gives the position of the largest value, not the value. With axis=1 it looks along each row, so you get one position per penguin: 0, 2, 1, 2. Row 0's largest score (2.1) is in column 0, row 1's (3.0) in column 2, and so on.

The second cell uses those positions to pick names from an array of species, the list-of-positions trick from lesson 0.3. That line, classes[scores.argmax(axis=1)], turns a model's output into its answers, and you'll write it in nearly every classifier in this course.

Wrong way: forgetting the axis

You want each feature's mean, and you type X.mean(). Run it:

axis.ipynb

One number: 86.98. With no axis, NumPy averages every number in the array: bill lengths, bill depths and flipper lengths, all mixed together. No error, and 86.98 looks like it could be a measurement. It isn't one of anything.

This mistake travels. If you subtract that one number from your data to "centre" it, every feature is shifted by the same wrong amount, and the model trains on it without complaint. When you summarise a 2-D array, always say which axis, and check the shape of the answer: per feature from (n, d) should be (d,).

Check what you learned

X has shape (200, 5). What shape is X.max(axis=0)?
scores = np.array([[0.1, 0.7, 0.2], [0.6, 0.3, 0.1]]). What is scores.argmax(axis=1)?
Predict, then run it in step 1’s notebook: what is np.log(np.exp(3.0))?

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