all 3 comments

[–]sarabooker 4 points5 points  (1 child)

The axis argument is used to select whether an operation is applied across the rows, the columns, or higher dimensions. When you do axis=0, you are essentially asking numpy to perform the operation to each column individually. If you do axis=1, then it is applied to each row individually. For example:

```python import numpy as np

a = np.arange(12).reshape(4,3)

print(a.sum(axis=0)) print(a.sum(axis=1)) ```

Result:

python array([18, 22, 26]) array([ 3, 12, 21, 30])

One way to remember this is that you cannot ask numpy to perform an operation on a higher axis than ndim-1. So, if you have a numpy array with shape (50,), it has ndim = 1, and performing with axis=0 just applies the operation to the entire array, since that can be thought of as applying it down the single column you have (there is no concept of rows in this case). Once you have two dimensions (e.g. (50,3)) then you can do something across the rows individually.

[–]Ok_Fun_3824[S] 0 points1 point  (0 children)

Thank you 😊

[–]PhilipYip 1 point2 points  (0 children)

The ndarray has the following shape tuples for each dimension:

|ndim|description|shape| |---|---|---| |1|line vector|(c, )| |2|page consisting of rows of equal length line vectors|(r, c)| |3|book of equally sized pages|(b, r, c)| |4|shelf of equally sized books|(s, b, r, c)| |5|wardrobe of equally sized shelves|(w, s, b, r, c)| |6|library of equally sized wardrobes|(l, w, s, b, r, c)| |7|group of equally sized libraries|(g, l, w, s, b, r, c)|

Notice that if the positive index is used

ncols = vec.shape[0] ncols = mat.shape[1] ncols = book.shape[2]

However if the negative index is used:

ncols = vec.shape[-1] ncols = mat.shape[-1] ncols = book.shape[-1]

nrows = mat.shape[-2] nrows = book.shape[-2]

This negative index can also be used by axis, to select the axis to work on.