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

时间:2014-03-22 21:26:36

标签: python numpy

所以我正在尝试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)

不应该-2反转戒律吗?我想要做的是应用一个只能在2坐标第一时应用的矩阵。但后来我想把我的阵列恢复原状。我发现唯一可行的方法是应用np.rollaxis(x, 2)两次,或应用np.rollaxis(x, 0, start=3)。我只是通过猜测发现了这些,我不知道它们为什么会起作用。它们似乎也掩盖了我真正想做的事情。有人可以解释一下我应该如何反转'滚动,或者我做错了什么?

(有一种pythonic方式吗?)

4 个答案:

答案 0 :(得分:12)

方法rollaxis

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

axis“位置”

重新分配所选的start

按照你的例子:

a = np.ones((4, 3, 2))
x = np.rollaxis(a, 2)
# x.shape = (2, 4, 3)

关于形状:rollaxis会将2中的数字axis=2带到start=0以来的第一个位置。

使用

x2 = np.rollaxis(x, -2)
# x2.shape = (4,2,3)

rollaxis将带来数字4,即第二个最后一个轴axis=-2,并在第一个位置重新分配,因为start=0。这解释了您的结果(4,2,3),而不是(4,3,2)

遵循相同的逻辑,这解释了为什么两次应用rollaxis(a,2)会使数组形状回到初始状态。 np.rollaxis(x, 0, start=3)也有效,因为第一个轴到达最后一个轴,换句话说,(2,4,3)中的数字2到达最后一个位置(4,3,2)。

答案 1 :(得分:6)

np.rollaxis(tensor,axis,start)将轴参数指定的轴移动到位于start的轴之前的位置,没有例外。

假设轴是(1,2,3,4,5,6),如果轴指向3,并且开始指向5,那么在滚动之后,3将在5之前。在我的例子中,3位于维度元组的位置2,轴= 2。此外,由于5位于第4位,因此start = 4。

像这样:

>>> a.shape

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

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

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

如您所见,3现在正好在5之前。 注意:3不会移动到位置4,而是移动到最初位于第4位的值之前的位置(在这种情况下,结果是位置3)。

负数指定位置就像它们对列表一样。换句话说,axis = -1指定最后一个位置。在上面的例子中,在-1位置有一个6,在-2位置有一个5。轴和开始都可能是负的。

你可以像我这样用负数做同样的事情:

>>> a.shape

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

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

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

如果未指定start,则默认为0,这是第一个位置。这意味着如果未指定start,则指定的轴将始终移动到开头,该开头位于最初位于位置0的1之前。

如果这令人困惑,还有另一种解释可能会更有意义: Reason why numpy rollaxis is so confusing?

答案 2 :(得分:0)

基本思想是围绕nd-array轴移动。 它采用start参数提到的轴,并将其放在start参数提到的位置中;当发生这种情况时,以下位置的剩余轴直到结束将向右移动。如果忽略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) 参数,它会将提到的轴移动到第一个位置(即它将被移动为第0个轴)

让我们用一个例子来理解它:

start

在移动轴(或滚动作为NumPy调用它)时,该位置中已存在的轴为入射轴腾出空间,并且随后的轴向右移动。

如果忽略axis参数,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) 参数中的轴将移到前面(即到第0位)。

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)

进行比较
np.moveaxis

在上面的示例中观察np.rollaxis如何循环移位node_env: production loggingEnabled: true devErrorsEnabled: true logDirectory: iisnode debuggingEnabled: true maxLogFileSizeInKB: 1048 maxLogFiles: 50 只是扩展仅向右侧。

PS:另请注意,此 rollaxis 操作会从 NumPy 1.10.0

开始返回输入数组的视图

答案 3 :(得分:0)

在axis == 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)