one-dimensional array shapes (length,) vs. (length,1) vs. (length)

前端 未结 4 1765
天涯浪人
天涯浪人 2020-12-31 05:25

When I check the shape of an array using numpy.shape(), I sometimes get (length,1) and sometimes (length,). It looks like the differe

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-31 05:50

    The (length,) array is an array where each element is a number and there are length elements in the array. The (length, 1) array is an array which also has length elements, but each element itself is an array with a single element. For example, the following uses length=3.

    >>> import numpy as np
    >>> a = np.array( [[1],[2],[3]] )
    >>> a.shape
    >>> (3, 1)
    >>> b = np.array( [1,2,3] )
    >>> b.shape
    >>> (3,)
    

提交回复
热议问题