Broadcasting Row Statistics
Introduction
Welcome back to Broadcasting Array Shapes in NumPy. You’ve finished three of the four lessons in this course, including how to predict whether shapes can broadcast. In this final lesson, we’ll use that knowledge with statistics calculated from a table itself. We’ll center scores by column and by row, find each product’s share of its store’s sales, and scale weekly steps by each person’s best week. The key is to give each statistic a shape that matches the direction in which it should be applied.
Column Means Are Already the Right Shape
Let’s start with a familiar (n, m) table: four students by three subjects. Calling mean(axis=0) collapses the rows and leaves one mean for each column, so the result has shape (m,), or (3,). As you may recall from earlier lessons, a 1D array of length three lines up with the three columns directly. We can subtract these means from every row without reshaping.
The first output line confirms that the statistic has one value per subject. Subtracting it centers each subject’s scores around that subject’s mean. Notice that the first student’s centered scores are [-5, 5, 7.5].
Each centered column sums to zero, apart from possible tiny rounding differences. This is a useful check that we subtracted the correct column means.
Row Means Have the Wrong Shape
Now we want to center each student’s scores around their own average. mean(axis=1) collapses the columns, leaving one mean per row. Its shape is (4,), but a 1D array aligns with the last dimension of a table. Here, that means NumPy tries to match four row means with three columns, so direct subtraction fails. In a square table, the shapes might happen to match and silently subtract across columns instead, so matching dimensions alone is not enough: we must also check alignment.
The shape is (4,), as expected: one value for each student. Comparing (4, 3) and (4,) from the right pairs 3 with 4, which are neither equal nor 1. The next step is to represent those four means as a column, so each one can line up with its student’s row.

