Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the fastest way to put the first axis behind the last one? [numpy]

Tags:

python

numpy

Let us look at this stupid example with a N0 x N1 x ... x Nm dimensional array, such as

import numpy as np
x = np.random.random([N0,N1,...,Nm]) 

Now I want the N0 dimension to be behind the last one, i.e.

np.swapaxis np.swapaxis(np.swapaxis(x, 0,1),1,2) ... # swapping m times

I tried with np.rollaxis, but this seems to be the wrong function. In the end, I want to have an array with shape (N1,N2,N3,...,Nm,N0).

Any nice ideas?

like image 343
varantir Avatar asked Nov 18 '25 15:11

varantir


1 Answers

I tried with np.rollaxis, but this seems to be the wrong function.

No, it's the right function.

x = numpy.rollaxis(x, 0, x.ndim)
like image 161
user2357112 supports Monica Avatar answered Nov 20 '25 04:11

user2357112 supports Monica



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!