0

I am trying to reshape an array using numpy.reshape but always come across the index error

"IndexError: index 15484 is out of bounds for axis 0 with size 7231"

I then printed out the shape of the array which was

(7231,80,60,4)

My code is

X = np.array([i[0] for i in train]).reshape(-1,80,60,1)

(im trying to reshape all of my image to (-1,80,60,1))

I thought -1 autocompleted the dimensions, so i am confused as to why I am getting this error?

train is:

    train = train_data[:-500]

and train_data is an array with tuples of image pixels and labels

Can someone help me?

8
  • What is the actual code that create the issue? Isn't it [i[0] for i in train]? Commented Dec 9, 2018 at 20:44
  • Probably, since it is an index error, but how do I fix it? Commented Dec 9, 2018 at 20:45
  • Well, we don't know what train is, so no idea. Commented Dec 9, 2018 at 20:46
  • @MatthieuBrucher I added the context Commented Dec 9, 2018 at 20:51
  • You don't show the code that is indexing with 15484. Commented Dec 9, 2018 at 21:09

3 Answers 3

2

Be careful when reshaping. Even if it works, the arrangement of elements may not be what you want.

Start with a simple array that we can visualize:

In [805]: x = np.arange(24).reshape(3,2,4)
In [806]: x
Out[806]: 
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23]]])

reshape to (-1,2,1) - but lets drop the last 1 for a more compact display:

In [807]: x.reshape(-1,2)
Out[807]: 
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15],
       [16, 17],
       [18, 19],
       [20, 21],
       [22, 23]])

Notice how the original [0,1,2,3] line gets split into 2 lines.

Another way of redistributing the last dimension of size 4 is:

In [808]: np.vstack([x[...,i] for i in range(4)])
Out[808]: 
array([[ 0,  4],
       [ 8, 12],
       [16, 20],
       [ 1,  5],
       [ 9, 13],
       [17, 21],
       [ 2,  6],
       [10, 14],
       [18, 22],
       [ 3,  7],
       [11, 15],
       [19, 23]])

That may be clearer if we used np.stack and got (4,3,2) shape

array([[[ 0,  4],
        [ 8, 12],
        [16, 20]],
  ....

x.transpose(2,0,1) produces the same thing.

reshape preserves the ravelled/flattened order of elements. Transpose changes it.

In [812]: x.transpose(2,0,1).ravel()
Out[812]: 
array([ 0,  4,  8, 12, 16, 20,  1,  5,  9, 13, 17, 21,  2,  6, 10, 14,...])
In [813]: x.reshape(-2,2).ravel()
Out[813]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, ...])
Sign up to request clarification or add additional context in comments.

Comments

0

Reshaping an array of shape (7231,80,60,4) -> (-1,80,60,1) does "just work":

train = np.arange(np.prod((7231,80,60,4))).reshape(7231,80,60,4)
print(train.shape)

X = train.reshape(-1,80,60,1)
print(X.shape)

Output:

(7231, 80, 60, 4)
(28924, 80, 60, 1)

So the issues you're having must not directly stem from the reshape that you're trying to do. My guess is that your problem might relate to the form/contents of your train_data array (or the array you try to create from it with np.array([i[0] for i in train])). Of course, the problem could also be in a section of your code that you didn't post in your question. It would probably be helpful if you posted a bit more of your actual code.

In particular, when you got the error message:

IndexError: index 15484 is out of bounds for axis 0 with size 7231

it should have included a stack trace that pointed directly back to the problematic line in your code. Did the stack trace say that the error was raised from the line in which you create X:

X = np.array([i[0] for i in train]).reshape(-1,80,60,1)

or did it point to a different line in your code?

Comments

0

The code you have doesn't do what you think it does. i[0] gets the 0th element in the first axis, which is your 80, that's not what you want.

Anyway, what you really what is just to select the first slice in the fastest direction, so just do:

X = train[:,:,:,0:1]

If your data is not actually the size you say it is, then try:

X = np.array([i.reshape(80, 60, 4)[:,:,0:1] for i in train])

4 Comments

when I do this: X = np.array(train[:,:,:,0:1]).reshape(-1,80,60,1), it gives me this error: X = np.array(train[:,:,:,0:1]).reshape(-1,80,60,1) IndexError: too many indices for array
But why do you want to create a new array and reshape it?? And if train has the shape you say it has, then just use train[:,:,:,0:1].
From the OP's description, I'm pretty sure that train/train_data is really a 1D array of dtype O that contains tuples of (3D array, str). I don't think your 4D slice will work for them. Rather, their current list comprehension np.array([i[0] for i in train]) is probably what they actually want.
The OP very not specifically said that the array had that shape. You know, that's on them. At this point I wouldn't make too many strong assumption (or weak ones, for that matter).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.