by Ultcyber · 5y ago · 0 comments
> The value of the axis argument is, as a matter of fact, the number of the index in question: The first index is axis=0, the second one is axis=1, and so on
If I'm not wrong - this statement is wrong - the axis does not represent index (or otherwise it would be axis 0 = row, axis 1 = column). The actual idea behind is a little unintuitive, but explained well here: https://aerinykim.medium.com/numpy-sum-axis-intuition-6eb949...
The axis argument gives the index of the axis along which your summate, and which therefore disappears after summation.
There might be a confusion about whether "axis 0" is rows or columns? Its length is the number of rows, but it "points" along columns.
I prefer just calling them axis 0, 1, 2, ... and avoid thinking about rows and columns in numpy, I've found that sometimes avoids confusion.
On the other hand einsum is Crystal clear and not prone to confusion.
> np.einsum("ij -> j", B) # sum along rows to create one column-like array
> np.einsum("ij -> i", B) # sum along columns to create one row-like array
Edit: More Einstein sum fun at https://stackoverflow.com/questions/26089893/understanding-n...
Both of them are of interest and after you were confused once about which one to supply to axis=..., there is no way back to clear the confusion. With einsum there is no confusion.