Reputation: 633
I want to change the ordering of this numpy images array to channel_last training_data : (2387, 1, 350, 350) to (2387,350,350,1) validation_data : (298, 1, 350, 350) to (298, 350, 350, 1) testing_data : (301, 1, 350, 350) to (301, 350, 350, 1)
I tried this but it is not working
np.rollaxis(training_data,0,3).shape
np.rollaxis(validation_data,0,3).shape
np.rollaxis(testing_data,0,3).shape
Upvotes: 4
Views: 17607
Reputation: 20472
You need the np.transpose
method like this:
training_data = np.transpose(training_data, (0,2,3,1))
The same can be done for the other ones
Upvotes: 11
Reputation: 11
Do it like this:
np.rollaxis(array_here,axis_to_roll,to_where)
For your case you want to roll axis 0 to last position (beyond 3) so you have to put to_where as 4 (beyond 3).
This will work(personally tested):
np.rollaxis(train_data,0,4)
Upvotes: 0
Reputation: 53029
If the axis you are moving has length 1
a simple reshape will do:
a = np.arange(24).reshape(2, 1, 3, 4)
# Three different methods:
b1 = a.reshape(2, 3, 4, 1)
b2 = np.moveaxis(a, 1, 3)
b3 = a.transpose(0, 2, 3, 1)
# All give the same result:
np.all(b1 == b2) and np.all(b2 == b3)
# True
Upvotes: 1