Sub topic 8
Mean, median, and standard deviation along an axis
This chapter assumes you can create arrays with np.array, read an array's shape, and know that axis=0 runs down the rows (one result per column) while axis=1 runs across the columns (one result per row). Here the same axis rule is applied to statistics.
The same rule, different summaries
mean, median, and std are reductions, like sum. They take an axis argument, and the axis you name disappears from the result's shape. For an array of shape (2, 4), axis=0 gives shape (4,) and axis=1 gives shape (2,).
| Statistic | Function | Method on the array |
|---|---|---|
| Mean | `np.mean(a, axis=...)` | `a.mean(axis=...)` |
| Median | `np.median(a, axis=...)` | none |
| Standard deviation | `np.std(a, axis=...)` | `a.std(axis=...)` |
np.median exists only as a function. Calling a.median() raises an AttributeError.
Worked example
The array below holds two rows of four values. Its eight values are 2, 4, 4, 4, 5, 5, 7, and 9, so the whole-array mean is 5.0 and the standard deviation is 2.0.
import numpy as np
a = np.array([[2, 4, 4, 4],
[5, 5, 7, 9]])
print(a.mean(), np.median(a), a.std()) # 5.0 4.5 2.0
print(a.mean(axis=0)) # [3.5 4.5 5.5 6.5]
print(np.median(a, axis=0)) # [3.5 4.5 5.5 6.5]
print(a.std(axis=0)) # [1.5 0.5 1.5 2.5]
print(a.mean(axis=1)) # [3.5 6.5]
print(np.median(a, axis=1)) # [4. 6.]
print(a.std(axis=1).round(3)) # [0.866 1.658]Each column holds only two values. For two values the mean and the median are the same number, so the first two column results match. The row medians differ from the row means because the rows have four values and the values are unevenly spread.
The mean of an integer array is a float. The median can also be a float, because for an even count it averages the two middle values.
Population and sample standard deviation
std divides by the number of values N by default. That is the population standard deviation. For the sample standard deviation, which divides by N - 1, pass ddof=1.
import numpy as np
a = np.array([[2, 4, 4, 4],
[5, 5, 7, 9]])
print(a.std(axis=1, ddof=1).round(3)) # [1. 1.915]The same ddof argument works for var.
Standardizing rows and columns
A z-score subtracts the mean and divides by the standard deviation. To standardize each column, the reduced arrays have shape (4,). They broadcast against (2, 4) without any extra step. To standardize each row, the reduced arrays have shape (2,), which does not line up with (2, 4). Use keepdims=True so they have shape (2, 1).
import numpy as np
a = np.array([[2, 4, 4, 4],
[5, 5, 7, 9]])
z_cols = (a - a.mean(axis=0)) / a.std(axis=0)
z_rows = (a - a.mean(axis=1, keepdims=True)) / a.std(axis=1, keepdims=True)
print(z_rows.round(2))
# [[-1.73 0.58 0.58 0.58]
# [-0.9 -0.9 0.3 1.51]]Common errors
- Expecting
axis=0to mean "per row". It gives one result per column. - Calling
a.median(). Usenp.median(a). - Forgetting that
stdis the population version unless you setddof=1. - Running these functions on data with
NaN. The result isNaN. The next chapters cover the NaN-aware variants.
Exercise
For b = np.array([[1, 3, 5], [2, 4, 12]]), what are np.median(b, axis=1) and b.mean(axis=0), and what are their shapes?
Show answer
np.median(b, axis=1) is [3. 4.] with shape (2,), because the middle value of each row is taken. b.mean(axis=0) is [1.5 3.5 8.5] with shape (3,), because each column is averaged.