Reductions and axes
Quick answer
A reduction combines values along one or more axes, for example to compute a sum, mean or standard deviation. For a matrix of samples by features, axis=0 removes the sample axis and gives a result per feature; axis=1 removes the feature axis and gives a result per sample. With no axis, a mean or sum normally combines all elements.
Summarize a small feature table
Use the same shape convention throughout: rows are samples. keepdims=True preserves reduced axes with length one so the result can broadcast back against the original table.
import numpy as npx = np.array([[2, 10], [4, 20], [6, 30]], dtype=np.float64)print(x.mean(axis=0).tolist())print(x.sum(axis=1).tolist())print(x.mean())print(x.mean(axis=0, keepdims=True).shape)Expected output (local NumPy 2.3.5):
[4.0, 20.0][12.0, 24.0, 36.0]12.0(1, 2)Feature means retain two values; sample totals retain three. The global mean combines all six values.
Standardize with a constant-column policy
Fit feature means and population standard deviations (ddof=0) on training rows only. Standardization subtracts the mean and divides by the scale. Replace zero scales with 1, leaving constant training features at zero. np.where chooses between alternatives elementwise.
import numpy as nptrain = np.array([[2, 7], [6, 7]], dtype=np.float64)mu = train.mean(axis=0, keepdims=True)sd = train.std(axis=0, ddof=0, keepdims=True)scale = np.where(sd == 0, 1.0, sd)print(((train - mu) / scale).tolist())held_out = np.array([[8, 7]], dtype=np.float64)print(((held_out - mu) / scale).tolist())Expected output (local NumPy 2.3.5):
[[-1.0, 0.0], [1.0, 0.0]][[2.0, 0.0]]The held-out row uses the training mean 4 and scale 2 for feature one; it does not change them. The constant column uses the documented scale-one convention.
Common mistakes
Do not refit on validation or test rows. A zero-variance training feature might change in production; scale-one is a defined policy, not evidence the feature will remain constant. Real systems also need near-zero-scale checks, finite values and missing-data handling. Mean of an empty slice is undefined and NumPy may warn and return NaN; check row counts first. ddof=1 uses N−1 and requires at least two observations. Floating reductions have rounding error; consider float64 accumulation when appropriate.
Practice and next steps
For training rows [0, 9] and [4, 9], calculate feature means and scales by hand, then transform [6, 9] using training statistics. Next, use matrix multiplication for predictions.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reductions and axes — knowledge check
Apply array reasoning to new synthetic feature tables.
For [[1, 4], [3, 8]], what is the axis=0 mean?
Which axis gives one sum per sample in a sample-by-feature matrix?
What does keepdims=True do for axis=0 mean on (5, 2)?
What should be used to standardize a held-out row?
With ddof=0, what is the standard deviation of [4, 4]?