Axes and broadcasting
The np.sum function with no arguments adds up every element of an array. For a feature table, that number is useless: an apartment's area gets added to its price. You need a sum or a mean for each column separately:
import numpy as np
X = np.array([[1.0, 20.0], [2.0, 35.0], [3.0, 50.0]])
print(np.sum(X)) # 111.0
print(np.sum(X, axis=0)) # [ 6. 105.]
print(X.mean(axis=0)) # [ 2. 35.]
print(X.mean(axis=1)) # [10.5 18.5 26.5]The axis argument
An axis is the number of an array dimension. In a two-dimensional array, axis 0 indexes the rows and axis 1 the columns. The axis argument names the axis along which the computation runs, and that axis disappears from the result.
X.mean(axis=0) averages the rows and gives one number per column, that is, the shape (2,). X.mean(axis=1) gives one number per row, the shape (3,). The .sum method and the np.sum and np.mean functions take axis the same way. Without axis, the computation runs over all elements at once, as in the first line of the example.
Broadcasting
The column means can be subtracted from the whole table in one line, even though the shapes differ: (3, 2) and (2,). This is broadcasting: numpy repeats the smaller array for each row of the larger one. Multiplying an array by a number, as in a * 1.2, is broadcasting too: the number is repeated for each element.
centered = X - X.mean(axis=0)
print(centered.shape) # (3, 2)
print(centered[:, 1]) # [-15. 0. 15.]
print(np.ones((4, 3)).shape) # (4, 3)Shapes are compared from the end. The lengths in each pair must match, or one of them must be 1. For (3, 2) and (2,) the last lengths are equal, so the subtraction works. The np.ones function creates an array of the given shape filled with ones—a handy way to check shapes.
After the means are subtracted, the mean of each column is zero. You can see it in centered[:, 1]: the values -15, 0 and 15 add up to zero. This brings features with different units to a common center.
Common mistake
Each row's mean is subtracted from that row:
row_means = X.mean(axis=1)
print(X - row_means) # ✗ValueError: operands could not be broadcast together with shapes (3,2) (3,)There are three means, but numpy compares the last lengths: 2 and 3 do not match. You need a column shaped (3, 1): X.mean(axis=1, keepdims=True) gives one, and then each mean is subtracted from its own row.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.