Python Numpy - numpy axis performance Python Numpy - numpy axis performance numpy numpy

Python Numpy - numpy axis performance


You don't compute the same thing.

The first two commands only compute one row/column out of the entire array.

a[0, :].sum().shape   # sums just the first row only()

The second two commands, sum the entire contents of the 2D array, but along a certain axis. That way, you don't get a single result (as in the first two commands), but an 1D array of sums.

a.sum(axis=0).shape   # computes the row-wise sum for each column(5000,)

In summary, the two sets of commands do different things.


aarray([[1, 6, 9, 1, 6],       [5, 6, 9, 1, 3],       [5, 0, 3, 5, 7],       [2, 8, 3, 8, 6],       [3, 4, 8, 5, 0]])a[0, :]array([1, 6, 9, 1, 6])a[0, :].sum()23

a.sum(axis=0)array([16, 24, 32, 20, 22])