How does the axis parameter from NumPy work?

前端 未结 6 1302
野的像风
野的像风 2020-12-02 08:49

Can someone explain exactly what the axis parameter in NumPy does?

I am terribly confused.

I\'m trying to use the function myArray.sum(ax

6条回答
  •  攒了一身酷
    2020-12-02 09:20

    Both 1st and 2nd reply is great for understanding ndarray concept in numpy. I am giving a simple example.

    And according to this image by @debaonline4u

    https://i.stack.imgur.com/O5hBF.jpg

    Suppose , you have an 2D array - [1, 2, 3] [4, 5, 6]

    In, numpy format it will be -

    c = np.array([[1, 2, 3], 
                  [4, 5, 6]])  
    

    Now,

    c.ndim = 2 (rows/axis=0)
    c.shape = (2,3) (axis0, axis1)
    c.sum(axis=0) = [1+4, 2+5, 3+6] = [5, 7, 9] (sum of the 1st elements of each rows, so along axis0)
    c.sum(axis=1) = [1+2+3, 4+5+6] = [6, 15]    (sum of the elements in a row, so along axis1)
    

    So for your 3D array,

提交回复
热议问题