JChat
JChat

Reputation: 814

How to reshape a 4 dimensional Numpy array into different dimensions?

I have a 4 dimensional Numpy array, of (8, 1, 1, 102). Now, for instance, I simply want to ignore the middle two dimensions and have an array of shape (8,102), what may be the suitable way to accomplish this?

Upvotes: 0

Views: 880

Answers (2)

Gaurav Rai
Gaurav Rai

Reputation: 11

np.squeeze will collapse all the dimensions having length 1, or you can use the reshape function

Upvotes: 1

FHTMitchell
FHTMitchell

Reputation: 12157

You can't simply "ignore" the first two dimensions. You have an array of size 8 * 1 * 1 * 102 == 816 but you want an array of size 1 * 102 so you will have to choose which values to drop.

For example, if you want the first 102 you can do

array[0, 0]

which will have shape (1, 102)

Edit

If you want dimensions (8, 102) then, as the other user who deleted their answer said, you want np.squeeze.

x = np.random.random((8, 1, 1, 102))
y = np.squeeze(x)
print(y.shape)  # (8, 102)

Upvotes: 2

Related Questions