我正在尝试系统地访问 numpy 数组的轴。例如,假设我有一个数组
a = np.random.random((10, 10, 10, 10, 10, 10, 10))
# choosing 7:9 from axis 2
b = a[:, :, 7:9, ...]
# choosing 7:9 from axis 3
c = a[:, :, :, 7:9, ...]
Run Code Online (Sandbox Code Playgroud)
如果我有一个高维数组,输入冒号会变得非常重复。现在,我想要一些choose_from_axis这样的功能
# choosing 7:9 from axis 2
b = choose_from_axis(a, 2, 7, 9)
# choosing 7:9 from axis 3
c = choose_from_axis(a, 3, 7, 9)
Run Code Online (Sandbox Code Playgroud)
所以,基本上,我想访问一个带有数字的轴。我知道如何做到这一点的唯一方法是rollaxis来回使用,但我正在寻找一种更直接的方法来做到这一点。