how is axis indexed in numpy's array?

后端 未结 4 1257
轮回少年
轮回少年 2020-11-28 18:07

From Numpy\'s tutorial, axis can be indexed with integers, like 0 is for column, 1 is for row, but I don\'t grasp why they are indexed this way? An

4条回答
  •  北海茫月
    2020-11-28 18:34

    You can grasp axis in this way:

    >>> a = np.array([[[1,2,3],[2,2,3]],[[2,4,5],[1,3,6]],[[1,2,4],[2,3,4]],[[1,2,4],[1,2,6]]])
    array([[[1, 2, 3],
        [2, 2, 3]],
    
       [[2, 4, 5],
        [1, 3, 6]],
    
       [[1, 2, 4],
        [2, 3, 4]],
    
       [[1, 2, 4],
        [1, 2, 6]]])
    >>> a.shape
    (4,2,3)
    

    I created an array of a shape with different values(4,2,3) so that you can tell the structure clearly. Different axis means different 'layer'.

    That is, axis = 0 index the first dimension of shape (4,2,3). It refers to the arrays in the first []. There are 4 elements in it, so its shape is 4:

      array[[1, 2, 3],
            [2, 2, 3]],
    
      array[[2, 4, 5],
            [1, 3, 6]],
    
      array[[1, 2, 4],
            [2, 3, 4]],
    
      array[[1, 2, 4],
            [1, 2, 6]]
    

    axis = 1 index the second dimension in shape(4,2,3). There are 2 elements in each array of the layer: axis = 0,e.c. In the array of

     array[[1, 2, 3],
           [2, 2, 3]]
    

    . The two elements are:

    array[1, 2, 3]
    
    array[2, 2, 3]
    

    And the third shape value means there are 3 elements in each array element of layer: axis = 2. e.c. There are 3 elements in array[1, 2, 3]. That is explicit.

    And also, you can tell the axis/dimensions from the number of [] at the beginning or in the end. In this case, the number is 3([[[), so you can choose axis from axis = 0, axis = 1 and axis = 2.

提交回复
热议问题