numpy array: IndexError: too many indices for array

前端 未结 3 1977
盖世英雄少女心
盖世英雄少女心 2021-01-04 17:47

This works:

>>> a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
>>> a[: , 2]
array([ 3,  7, 11])

This doesn\'t

3条回答
  •  北海茫月
    2021-01-04 18:28

    It's simple to see what the problem is. Try,

    >>> a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
    >>> a.shape
    

    and then

    >>>a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11]])
    >>> a.shape
    

    and you will see the problem yourself, that in case two, shape is (3,).Hence the too many indices.

提交回复
热议问题