3 ms·
(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,
by Ultcyber 6y 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-6eb94926a5d1 https://aerinykim.medium.com/numpy-sum-axis-intuition-6eb949...
- Schiphol 6y agoThe 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.
- Ultcyber 6y agothat's what I mean - in this sense, the axis 0 should be row, but instead it's column in numpy
- svara 6y agoIt'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.
- throwawayiionqz 6y agoI 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-numpys-einsum https://stackoverflow.com/questions/26089893/understanding-n...
- bonoboTP 6y agoIt's simple. It's the axis that will disappear (or become length 1 with keepdims) after performing the operation.
- throwawayiionqz 6y agoIt'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.
- evanb 6y agoIf 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.
- mbeex 6y agoNever 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.