strides and broadcasting

Published 2025-05-07 • Updated 2025-05-08

I didn’t understand how numpy performs operations like this so efficiently:

arr = np.random.random((100,100))
arr -= np.median(arr, axis=1)

Apparently it uses a stride of 0 along axis 1 to effectively re-use the same byte when broadcasting across axis 1. I think it uses a temporary view of the median to get a shape of (100, 1), with strides (8, 0).

I’ve been trying to understand numpy more concretely lately, especially as I’ve been appreciating how clean it is.