size of NumPy array

前端 未结 2 420
天涯浪人
天涯浪人 2020-12-29 01:59

Is there an equivalent to the MATLAB

 size()

command in Numpy?

In MATLAB,

>>> a = zeros(2,5)
 0 0 0 0 0
         


        
相关标签:
2条回答
  • 2020-12-29 02:34

    This is called the "shape" in NumPy, and can be requested via the .shape attribute:

    >>> a = zeros((2, 5))
    >>> a.shape
    (2, 5)
    

    If you prefer a function, you could also use numpy.shape(a).

    0 讨论(0)
  • 2020-12-29 02:46

    Yes numpy has a size function, and shape and size are not quite the same.

    Input

    import numpy as np
    data = [[1, 2, 3, 4], [5, 6, 7, 8]]
    arrData = np.array(data)
    
    print(data)
    print(arrData.size)
    print(arrData.shape)
    

    Output

    [[1, 2, 3, 4], [5, 6, 7, 8]]

    8 # size

    (2, 4) # shape

    0 讨论(0)
提交回复
热议问题