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.expon every element at once - Sum, average and find the largest value along
axis=0oraxis=1 - Say which axis disappears when you summarise along it
- Use
argmaxto 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:
X[:, 2] / 10turns 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.logandnp.sqrtwork on every element.np.exp(1)is , andnp.logundoes it: .
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.
(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:
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 over examples is the sum down column , divided by :
In words: for column , add the value in every row , from 1 to , and divide by . is the number in row , column . In NumPy, all the columns at once: X.mean(axis=0). The over (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 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:
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
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.
- NumPy: Universal functions (ufunc) basics: functions that work element by element
- NumPy: Mathematical functions: exp, log, sqrt, sum and the rest
- NumPy: Glossary, axis: what an axis is, and what reducing along one does
- NumPy: Statistics: mean, and the other summaries