Summing and Axes
Overview
In two dimensions, the question is always which way you are collapsing.
What You Will Learn in This Lesson
By the end of this lesson, you will be able to:
- Reduce a whole array to a single number.
- Say what
axis=0andaxis=1collapse, in words, before writing either. - Check an answer by its shape rather than by reading the numbers.
- Choose the axis from the question being asked.
Why This Matters
Getting the axis backwards is the most common error in this unit, and it is dangerous because it does not raise — you get a plausible-looking list of the wrong length. Learning to say "I am collapsing the rows, so I keep one value per column" out loud, and then checking the shape, is what stops it.
Step 1: Aggregating the whole array
sum, mean, min, max and std each reduce an array to one number when you do not say otherwise.
Everything at once
With no axis named, there is nothing left to keep, so the whole array collapses — including a two-dimensional one.
import numpy as np
table = np.array([[1, 2, 3],
[4, 5, 6]])
print(table.sum())
print(table.mean())
print(table.max())
Mini Practice #1: Say it before you write it
Try It YourselfThis table is students down, tests across. Print the average per test, then the average per student — and predict the length of each before you run it.
Step 2: axis picks the direction
The name is the confusing part. axis says which dimension is being removed, not which one is kept.
axis=0 collapses the rows
Running down through the rows leaves one value per column. For a table of students by test, that is one number per test.
import numpy as np
table = np.array([[1, 2, 3],
[4, 5, 6]])
print(table.sum(axis=0)) # one per column
print(table.sum(axis=0).shape)
axis=1 collapses the columns
Running across the columns leaves one value per row — one number per student.
import numpy as np
table = np.array([[1, 2, 3],
[4, 5, 6]])
print(table.sum(axis=1)) # one per row
print(table.sum(axis=1).shape)
Check the shape, not the numbers
A 2×3 table summed along the wrong axis gives three numbers where you wanted two. On a real table both are plausible lists, so the length is the check that catches it.
import numpy as np
table = np.array([[1, 2, 3],
[4, 5, 6]])
print("students:", table.shape[0], "tests:", table.shape[1])
print("per student:", table.sum(axis=1)) # length 2
print("per test: ", table.sum(axis=0)) # length 3
A sentence that always works: axis=0 is down, axis=1 is across. Say which you mean in words first, then write it.
Mini Practice #2: Best in each test
Try It YourselfPrint the highest mark in each test, and then which student got it. argmax takes an axis too.
Step 3: When to Use It
Totals per row or per column
axis=0 collapses rows to give one value per column; axis=1 gives one value per row.
per_student = marks.sum(axis=1)
Whole-table summaries
No axis gives one number for everything — a grand total or overall mean.
When Not to Use It
Do not guess the axis. Check the shape of the result: if you wanted one value per student and got one per test, the axis is the other one.
End-of-Lesson Exercises
Exercise 1: One total per student
A table has one row per student and one column per test. Write student_totals(table) returning each student’s total as a list.
One total per student means one per row, so the sum has to collapse across the columns. That is axis=1.
Exercise 2: The hardest test
Write hardest_test(table) that returns the position of the test with the lowest average mark, where the table is students down and tests across. Collapse the rows to get an average per test, then take the position of the smallest.
table.mean(axis=0) gives one mean per test. argmin on that gives the position of the smallest. Wrap it in int() so the answer is a plain number.