Reputation: 1
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?
Upvotes: 0
Views: 900
Reputation: 231728
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, ...])
Upvotes: 2
Reputation: 22043
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])
Upvotes: 0
Reputation: 13997
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?
Upvotes: 0