为什么numpy rollaxis如此令人困惑? [英] Reason why numpy rollaxis is so confusing?

查看:102
本文介绍了为什么numpy rollaxis如此令人困惑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy rollaxis函数的行为使我感到困惑. 文档说:

The behavior of the numpy rollaxis function confuses me. The documentation says:

向后滚动指定的轴,直到它位于给定的位置.

Roll the specified axis backwards, until it lies in a given position.

对于start参数:

轴将滚动直到其位于该位置之前.

The axis is rolled until it lies before this position.

对我来说,这已经有些矛盾了.

To me, this is already somehow inconsistent.

好的,直接的示例(来自文档):

Ok, straight forward example (from the documentation):

>>> a = np.ones((3,4,5,6))
>>> np.rollaxis(a, 1, 4).shape
(3, 5, 6, 4)

索引1(4)的轴向后滚动,直到它位于索引4之前.

The axis at index 1 (4) is rolled backward till it lies before index 4.

现在,当start索引小于axis索引时,我们具有以下行为:

Now, when the start index is smaller than the axis index, we have this behavior:

>>> np.rollaxis(a, 3, 1).shape
(3, 6, 4, 5)

不是将轴移到索引1之前的索引3,而是结束于1.

Instead of shifting the axis at index 3 before index 1, it ends up at 1.

那是为什么?为什么轴不总是滚动到给定的start索引?

Why is that? Why isn't the axis always rolled to the given start index?

推荐答案

很多困惑是由于人类的直觉导致的-我们如何考虑移动轴.我们可以指定多个滚动步骤(前后两个步骤),或者指定最终形状元组中的位置,或者相对于原始形状的位置.

Much of the confusion results from our human intuition - how we think about moving an axis. We could specify a number of roll steps (back or forth 2 steps), or a location in the final shape tuple, or location relative to the original shape.

我认为理解rollaxis的关键是着眼于原始形状的插槽.我能想到的最笼统的说法是:

I think the key to understanding rollaxis is to focus on the slots in the original shape. The most general statement that I can come up with is:

滚动a.shape[axis]a.shape[start]

before的含义与列表insert()中的相同.因此可以在末尾插入.

before in this context means the same as in list insert(). So it is possible to insert before the end.

rollaxis的基本操作是:

axes = list(range(0, n))
axes.remove(axis)
axes.insert(start, axis)
return a.transpose(axes)

如果为axis<start,则为start-=1,以说明remove动作.

If axis<start, then start-=1 to account for the remove action.

负值得到+=n,因此rollaxis(a,-2,-3)np.rollaxis(a,2,1)相同.例如a.shape[-3]==a.shape[1].列表insert也允许插入位置为负数,但rollaxis并未使用该功能.

Negative values get +=n, so rollaxis(a,-2,-3) is the same as np.rollaxis(a,2,1). e.g. a.shape[-3]==a.shape[1]. List insert also allows a negative insert position, but rollaxis doesn't make use of that feature.

因此,关键在于理解remove/insert对动作,并理解transpose(x).

So the keys are understanding that remove/insert pair of actions, and understanding transpose(x).

我怀疑rollaxis旨在成为transpose的更直观版本.是否达到目标是另一个问题.

I suspect rollaxis is intended to be a more intuitive version of transpose. Whether it achieves that or not is another question.

您建议省略start-=1或全面申请

省略它不会更改您的2个示例.它仅影响rollaxis(a,1,4)的情况,并且当axes[0,2,3]时,axes.insert(4,1)axes.insert(3,1)相同. 1仍放在末尾.稍微更改一下测试:

Omitting it doesn't change your 2 examples. It only affects the rollaxis(a,1,4) case, and axes.insert(4,1) is the same as axes.insert(3,1) when axes is [0,2,3]. The 1 is still placed at the end. Changing that test a bit:

np.rollaxis(a,1,3).shape
# (3, 5, 4, 6)   # a.shape[1](4) placed before a.shape[3](6)

没有-=1

# transpose axes == [0, 2, 3, 1]
# (3, 5, 6, 4)  # the 4 is placed at the end, after 6

如果相反,-=1始终适用

np.rollaxis(a,3,1).shape
#  (3, 6, 4, 5)

成为

(6, 3, 4, 5)

现在63之前,而3是原始的a.shape[0].滚动3之后是a.shape[1].但这是不同的roll规范.

now the 6 is before the 3, which was the original a.shape[0]. After the roll 3 is the the a.shape[1]. But that's a different roll specification.

这取决于start的定义方式.是原始顺序中的位置,还是返回顺序中的位置?

It comes down to how start is defined. Is a postion in the original order, or a position in the returned order?

如果您更愿意将start视为最终形状中的索引位置,那么放下before部分并说将axis移动到dest插槽"会更简单吗? /p>

If you prefer to think of start as an index position in the final shape, wouldn't it be simpler to drop the before part and just say 'move axis to dest slot'?

myroll(a, axis=3, dest=0) => (np.transpose(a,[3,0,1,2])
myroll(a, axis=1, dest=3) => (np.transpose(a,[0,2,3,1])

只需放弃-=1测试就可以解决问题(忽略负数和边界的处理)

Simply dropping the -=1 test might do the trick (omiting the handling of negative numbers and boundaries)

def myroll(a,axis,dest):
    x=list(range(a.ndim))
    x.remove(axis)
    x.insert(dest,axis)
    return a.transpose(x)

这篇关于为什么numpy rollaxis如此令人困惑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆