16

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

>>> a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11]])
>>> a[:,2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: too many indices for array

Why so ?

3
  • 1
    @MatthewCurry That's intentional. Hence the question. Why should that matter ? Commented Dec 9, 2017 at 22:11
  • 4
    print(a) shows that a is interpreted differently - the first is a 2D numpy array, the second is a 1D array of three lists [list([1, 2, 3, 4]) list([5, 6, 7, 8]) list([9, 10, 11])]. Commented Dec 9, 2017 at 22:17
  • 1
    Numpy goes for performance, so types must be set. Haven’t used it in awhile, but my guess is that the type of the last array is different: length 3 instead of 4. Numpy can’t make it’s normal assumptions, and so throws an error Commented Dec 9, 2017 at 22:17

4 Answers 4

20

Numpy ndarrays are meant for all elements to have the same length. In this case, your second array doesn't contain lists of the same length, so it ends up being a 1-D array of lists, as opposed to a "proper" 2-D array.

From the Numpy docs on N-dimensional arrays:

An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size.

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

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

This discussion may be useful.

Sign up to request clarification or add additional context in comments.

1 Comment

I 'fixed' the length by using np.pad() but still I cannot index it. Any help?
4

The first array has shape (3,4) and the second has shape (3,). The second array is missing a second dimension. np.array is unable to use this input to construct a matrix (or array of similarly-lengthed arrays). It is only able to make an array of lists.

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

>>> print(a)
[[ 1  2  3  4]
 [ 5  6  7  8]
 [ 9 10 11 12]]

>>> print(type(a))
<class 'numpy.ndarray'>


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

>>> print(b)
[list([1, 2, 3, 4]) list([5, 6, 7, 8]) list([9, 10, 11])]

>>> print(type(b))
<class 'numpy.ndarray'>

So they are both Numpy arrays, but only the first can be treated as a matrix with two dimensions.

Comments

0

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.

Comments

0

I also faced this error message " IndexError: too many indices for array " but unfortunately I attempted mistake that, incorrect syntax: LA = np.array( 0 for i in range(20) )

Note: must place [] brackets like, correct syntax: LA = np.array ( [ 0 for i in range(20) ] )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.