The method rollaxis
def rollaxis(a, axis, start=0):
reallocates the chosen axis
at the start
"position"
Following your example:
a = np.ones((4, 3, 2))
x = np.rollaxis(a, 2)
# x.shape = (2, 4, 3)
Concerning shapes: rollaxis
will bring the number 2
, which is in your last axis=2
, to the the first position, since start=0
.
By using
x2 = np.rollaxis(x, -2)
# x2.shape = (4,2,3)
rollaxis
will bring the number 4, which is the second last axis, axis=-2
, and reallocate at the first position, since start=0
. That explains your result (4,2,3)
, instead of (4,3,2)
.
Following the same logic, this explains why applying rollaxis(a,2)
twice brings the array shape back to the initial one. np.rollaxis(x, 0, start=3)
also works because the first axis goes to the last one, in other words the number 2 in (2,4,3) goes to the last position resulting (4,3,2).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…