Hacker News new | ask | show | jobs
by Ultcyber 1995 days ago
(in np.sum)

> 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...

3 comments

It's explained in more detail further down in that paragraph, I think the wording is fine.

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.

I could never grasp the conventions for "axis=". Everyone has some memory trick, some of them being confusing/wrong.

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...

Never could I. What helps me often is to consider the C language heritage of Python. There, the beginner also has the slightly confusing some_var[y][x]=some_value, caused by the computers memory model behind. Consequently, I'm always looking for the hierarchy of fastest changing indexes. This explains/justifies a lot of the design decisions made also for numpy.
It's simple. It's the axis that will disappear (or become length 1 with keepdims) after performing the operation.
It's not simple because there are two axis of interest: the axis along you sum, and the axis that are preserved (ie, that gives the dimension of the returned array).

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.

If you have a high-dimensional array then in your latter convention if you had to specify the “remaining” axes you’d need to provide a lengthy list. That’d be user-hostile.
The usual matrix notation is a_ij for the element in the ith row and the jth column. If you order axes in this way you get the numpy convention.
that's what I mean - in this sense, the axis 0 should be row, but instead it's column in numpy
The way I think about it is: as you change the 0th index you move up and down the column. Same with the 1st index and the row. That's how I remember it anyway!
Think about multidimensional arrays, not just matrices. How would your convention work there?