What does .shape[] do in “for i in range(Y.shape[0])”?

前端 未结 6 768
攒了一身酷
攒了一身酷 2020-12-22 18:01

I\'m trying to break down a program line by line. Y is a matrix of data but I can\'t find any concrete data on what .shape[0] does exactly.

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-22 18:51

    shape is a tuple that gives you an indication of the number of dimensions in the array. So in your case, since the index value of Y.shape[0] is 0, your are working along the first dimension of your array.

    From http://www.scipy.org/Tentative_NumPy_Tutorial#head-62ef2d3c0a5b4b7d6fdc48e4a60fe48b1ffe5006

     An array has a shape given by the number of elements along each axis:
     >>> a = floor(10*random.random((3,4)))
    
     >>> a
     array([[ 7.,  5.,  9.,  3.],
            [ 7.,  2.,  7.,  8.],
            [ 6.,  8.,  3.,  2.]])
    
     >>> a.shape
     (3, 4)
    

    and http://www.scipy.org/Numpy_Example_List#shape has some more examples.

提交回复
热议问题