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

前端 未结 6 769
攒了一身酷
攒了一身酷 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:45

    The shape attribute for numpy arrays returns the dimensions of the array. If Y has n rows and m columns, then Y.shape is (n,m). So Y.shape[0] is n.

    In [46]: Y = np.arange(12).reshape(3,4)
    
    In [47]: Y
    Out[47]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    
    In [48]: Y.shape
    Out[48]: (3, 4)
    
    In [49]: Y.shape[0]
    Out[49]: 3
    

提交回复
热议问题