Weedjo
Weedjo

Reputation: 335

Numpy Array Change indices

I have an numpy-array with 32 x 32 x 3 pictures with X_train.shape: (32, 32, 3, 73257). However, I would like to have the following array-shape: (73257, 32, 32, 3).

How can I accomplish this?

Upvotes: 0

Views: 5710

Answers (3)

Shijo
Shijo

Reputation: 9721

I think you want to do a transpose

>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose()
array([[1, 3],
       [2, 4]])
>>> a.transpose((1, 0))
array([[1, 3],
       [2, 4]])
>>> a.transpose(1, 0)
array([[1, 3],
       [2, 4]])

Upvotes: 1

bouletta
bouletta

Reputation: 525

np.reshape(X_train, (73257, 32, 32, 3))

https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.reshape.html

Upvotes: 1

Thomas Pinetz
Thomas Pinetz

Reputation: 7148

There are two ways to archive this either np.reshape(x, ndims) or np.transpose(x, dims).

For pictures I propose np.transpose(x, dims) which can be applied using X_train = np.transpose(X_train, (3,0,1,2)).

Upvotes: 2

Related Questions