So I was experimenting with numpy and I ran across a strange (?) behavior in the rollaxis method.
In [81]: a = np.ones((4, 3, 2))
In [82]: a.shape
Out[82]: (4, 3, 2)
In [83]: x = np.rollaxis(a, 2)
In [84]: x.shape
Out[84]: (2, 4, 3)
In [85]: np.rollaxis(x, -2).shape
Out[85]: (4, 2, 3)
Shouldn't the -2 reverse the rollaxis? What I'm trying to do is apply a matrix that can only be applied when the 2 coordinate is first. But then I want to put my array back into its original form. The only things which I have found to work are applying np.rollaxis(x, 2) twice, or applying np.rollaxis(x, 0, start=3). I just found these by guessing and I have no idea why they work. They also seem to be obscuring what I'm really trying to do. Could somebody please explain the way that I should 'reverse' a roll, or what I'm doing wrong?
(Is there a pythonic way to do this?)
np.rollaxisworks, but let me tell you - better just forget it and learnnp.moveaxis(a better version of this) immediately. For example:np.moveaxis(a, 0, 3)moves first dimension to fourth. This is the equivalent ofnp.rollaxis(a, 0, 4). The reverse operations are - correspondingly:np.moveaxis(a, 3, 0)andnp.rollaxis(a, 3).np.moveaxiscan change many dimensions at a time. - Tomasz Gandor