简体   繁体   English

numpy rollaxis - 它究竟是如何工作的?

[英]numpy rollaxis - how exactly does it work?

So I was experimenting with numpy and I ran across a strange (?) behavior in the rollaxis method. 所以我正在尝试numpy,我在rollaxis方法中遇到了一个奇怪的(?)行为。

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? -2不应该反转rolrolis? What I'm trying to do is apply a matrix that can only be applied when the 2 coordinate is first. 我要做的是应用一个只能在2坐标第一时应用的矩阵。 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) . 我发现的唯一的工作是应用np.rollaxis(x, 2)两次,或应用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?) (有这样的pythonic方法吗?)

The method rollaxis 方法rollaxis

def rollaxis(a, axis, start=0):

reallocates the chosen axis at the start "position" start “位置”重新分配所选axis

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 . 关于形状: rollaxis会将数字2 (最后一个axis=2 )带到第一个位置,因为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 . rollaxis将带来数字4,这是第二个最后一个轴, axis=-2 ,并在第一个位置重新分配,因为start=0 That explains your result (4,2,3) , instead of (4,3,2) . 这解释了你的结果(4,2,3) ,而不是(4,3,2)

Following the same logic, this explains why applying rollaxis(a,2) twice brings the array shape back to the initial one. 遵循相同的逻辑,这解释了为什么两次应用rollaxis(a,2)会使阵列形状回到初始状态。 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). np.rollaxis(x, 0, start=3)也有效,因为第一个轴到达最后一个轴,换句话说,(2,4,3)中的数字2到达最后一个位置(4,3,2) )。

np.rollaxis(tensor,axis,start) moves the axis specified by the axis parameter to the position before the axis that is located at start with no exceptions. np.rollaxis(tensor,axis,start)将轴参数指定的轴移动到位于start的轴之前的位置,没有异常。

Say the axes are (1, 2, 3, 4, 5, 6) if axis points to the 3, and start points to the 5, then after the roll, the 3 will be just before the 5. Since the 3 in my example is at position 2 of the dimensions tuple, axis=2. 假设轴是(1,2,3,4,5,6),如果轴指向3,并且开始指向5,那么在滚动之后,3将在5之前。因为3在我的示例位于维度元组的位置2,轴= 2。 Also, since the 5 is at position 4, start=4. 此外,由于5位于位置4,因此start = 4。

Like this: 像这样:

>>> a.shape

(1, 2, 3, 4, 5, 6)

>>> np.rollaxis(a, 2, 4).shape

(1, 2, 4, 3, 5, 6)

As you can see, the 3 is now right before the 5. NOTE: The 3 does not move to position 4, but rather to the position before the value originally at position 4 (which in this case turns out to be position 3). 正如您所看到的,3现在正好在5之前。注意:3不会移动到位置4,而是移动到最初位于第4位的值之前的位置(在这种情况下结果是位置3)。

Negative numbers specify positions just like they do for lists. 负数指定位置就像它们对列表一样。 In other words axis=-1 specifies the last position. 换句话说,axis = -1指定最后一个位置。 In my example above there is a 6 in the -1 position and a 5 in the -2 position. 在上面的例子中,在-1位置有一个6,在-2位置有一个5。 Both axis and start may be negative. 轴和起点都可以是负数。

You can do the same thing I did above with negative numbers like this: 您可以使用上面的负数做同样的事情:

>>> a.shape

(1, 2, 3, 4, 5, 6)

>>> np.rollaxis(a, -4, -2).shape

(1, 2, 4, 3, 5, 6)

If start is not specified, it defaults to 0 which is the first position. 如果未指定start,则默认为0,这是第一个位置。 That means that if start is not specified, the specified axis will always be moved to the beginning, which is before the 1 which was originally at position 0. 这意味着如果未指定start,则指定的轴将始终移动到开头,该开头位于最初位于位置0的1之前。

If this is confusing there is another explanation that might make more sense here: Reason why numpy rollaxis is so confusing? 如果这令人困惑,还有另一种解释可能会更有意义: 为什么numpy rollaxis如此令人困惑?

The basic idea is that it moves around the axis of the nd-array . 基本思想是它围绕nd阵列的轴移动 It takes the axis mentioned by axis parameter and puts it in the position mentioned by the start parameter; 它取axis参数提到的axis并将其置于start参数提到的位置 ; while this happens, the remaining axes in the following positions till the end will move towards right. 当发生这种情况时,以下位置的剩余轴直到结束将向右移动。 If you ignore the start parameter, it moves the mentioned axis to the first position (ie it will be moved as 0th axis) 如果忽略start参数,它会将提到的轴移动到第一个位置(即它将移动为第0个轴)

Let's understand it with an example: 让我们用一个例子来理解它:

In [21]: arr = np.ones((3,4,5,6))

In [22]: arr.shape
Out[22]: (3, 4, 5, 6)
# here 0th axis is 3, 1st axis is 4, 2nd axis is 5, 3rd axis is 6

# moving `3`rd axis as `1`st axis
In [27]: np.rollaxis(arr, 3, 1).shape

# see how `6` which was the third axis has been moved to location `1`
Out[27]: (3, 6, 4, 5)

While moving the axis (or rolling as NumPy calls it), the already existing axis in that position makes room for the incoming axis and that and the following axes move as a block towards right side. 在移动轴(或按NumPy调用它的方式滚动 )时,该位置中已存在的轴为进入的轴腾出空间,并且随后的轴向右侧移动。

If you ignore the start parameter, the axis in the axis parameter will be moved to the front (ie to the 0th position). 如果忽略start参数,则axis参数中的axis将移动到前面(即到第0位置)。

In [29]: a.shape
Out[29]: (3, 4, 5, 6)

# ignoring the `start` moves the axis to the very front position.
In [30]: np.rollaxis(arr, 3).shape
Out[30]: (6, 3, 4, 5)

Comparison with np.moveaxis np.moveaxis比较

In [38]: arr.shape
Out[38]: (3, 4, 5, 6)

In [39]: np.rollaxis(arr, 0, -1).shape
Out[39]: (4, 5, 3, 6)

In [40]: np.moveaxis(arr, 0, -1).shape
Out[40]: (4, 5, 6, 3)

Observe in the above example how np.moveaxis does circular shift while np.rollaxis just extends only towards right side. 观察在上面的例子中如何np.moveaxis确实循环移位而np.rollaxis只是仅延伸朝向右侧。


PS: Also, note that this rollaxis operation returns a view of the input array starting from NumPy 1.10.0 PS:另请注意,此rollaxis操作返回NumPy 1.10.0开始的输入数组视图

It will have the same result when axis==start and axis == start-1 当轴== start和axis == start-1时,它将具有相同的结果

>>>a=np.ones([1, 2, 3, 4, 5, 6])
(1, 2, 3, 4, 5, 6)
>>> np.rollaxis(a, axis=2, start=2).shape
(1, 2, 3, 4, 5, 6)
>>> np.rollaxis(a, axis=2, start=3).shape
(1, 2, 3, 4, 5, 6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM