what does numpy ndarray shape do?

后端 未结 4 1900
野的像风
野的像风 2020-12-04 14:10

I have a simple question about the .shape function, which confused me a lot.

a = np.array([1, 2, 3])   # Create a rank 1 array
print(type(a))            # P         


        
相关标签:
4条回答
  • 2020-12-04 14:48

    Unlike it's most popular commercial competitor, numpy pretty much from the outset is about "arbitrary-dimensional" arrays, that's why the core class is called ndarray. You can check the dimensionality of a numpy array using the .ndim property. The .shape property is a tuple of length .ndim containing the length of each dimensions. Currently, numpy can handle up to 32 dimensions:

    a = np.ones(32*(1,))
    a
    # array([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[ 1.]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]])
    a.shape
    # (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
    a.ndim
    # 32
    

    If a numpy array happens to be 2d like your second example, then it's appropriate to think about it in terms of rows and columns. But a 1d array in numpy is truly 1d, no rows or columns.

    If you want something like a row or column vector you can achieve this by creating a 2d array with one of its dimensions equal to 1.

    a = np.array([[1,2,3]]) # a 'row vector'
    b = np.array([[1],[2],[3]]) # a 'column vector'
    # or if you don't want to type so many brackets:
    b = np.array([[1,2,3]]).T
    
    0 讨论(0)
  • 2020-12-04 14:51

    .shape() gives the actual shape of your array in terms of no of elements in it, No of rows/No of Columns. The answer you get is in the form of tuples.

    For Example: 1D ARRAY:

    d=np.array([1,2,3,4])
    print(d)
    (1,)
    

    Output: (4,) ie the number4 denotes the no of elements in the 1D Array.

    2D Array:

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

    Output: (2,3) ie the number of rows and the number of columns.

    The number of elements in the final output will depend on the number of rows in the Array....it goes on increasing gradually.

    0 讨论(0)
  • 2020-12-04 15:02
    array([[1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]])
    

    0 讨论(0)
  • 2020-12-04 15:08

    yourarray.shape or np.shape() or np.ma.shape() returns the shape of your ndarray as a tuple; And you can get the (number of) dimensions of your array using yourarray.ndim or np.ndim(). (i.e. it gives the n of the ndarray since all arrays in NumPy are just n-dimensional arrays (shortly called as ndarrays))

    For a 1D array, the shape would be (n,) where n is the number of elements in your array.

    For a 2D array, the shape would be (n,m) where n is the number of rows and m is the number of columns in your array.

    Please note that in 1D case, the shape would simply be (n, ) instead of what you said as either (1, n) or (n, 1) for row and column vectors respectively.

    This is to follow the convention that:

    For 1D array, return a shape tuple with only 1 element   (i.e. (n,))
    For 2D array, return a shape tuple with only 2 elements (i.e. (n,m))
    For 3D array, return a shape tuple with only 3 elements (i.e. (n,m,k))
    For 4D array, return a shape tuple with only 4 elements (i.e. (n,m,k,j))

    and so on.

    Also, please see the example below to see how np.shape() or np.ma.shape() behaves with 1D arrays and scalars:

    # sample array
    In [10]: u = np.arange(10)
    
    # get its shape
    In [11]: np.shape(u)    # u.shape
    Out[11]: (10,)
    
    # get array dimension using `np.ndim`
    In [12]: np.ndim(u)
    Out[12]: 1
    
    In [13]: np.shape(np.mean(u))
    Out[13]: ()       # empty tuple (to indicate that a scalar is a 0D array).
    
    # check using `numpy.ndim`
    In [14]: np.ndim(np.mean(u))
    Out[14]: 0
    

    P.S.: So, the shape tuple is consistent with our understanding of dimensions of space, at least mathematically.

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