Average along an axis, and that axis is gone
Two questions about the same table. How many points did each test hand out? How many did each student score in all?
# down the rows: one number per test
print(scores.sum(axis=0))
# across the columns: one number per student
print(scores.sum(axis=1))
[120 180 150]
[240 210]
axis=0 runs down the rows, so it squashes the two students together and leaves one number per test. axis=1 runs across the columns, so it squashes the three tests together and leaves one number per student.
Pick an axis and watch which numbers get squashed together. Try max and mean too.
scores.sum(axis=0)(2, 3)→(3,)the 2 students are goneOne rule covers it: the axis you name is the axis that disappears. (2, 3) summed over axis 0 loses the 2 and becomes (3,). Summed over axis 1 it loses the 3 and becomes (2,).
mean, max, min and var, the variance, work the same way. scores.max(axis=1) is each student's best score: [ 90 100].
In the editor, scores is the same table. Fill in test_avgs, one average per test, and student_avgs, one average per student. Each line prints the result and its shape, so you can see which axis went away.