获取numpy数组的反对角线

lh80um4z  于 2023-05-07  发布在  其他
关注(0)|答案(3)|浏览(180)

因此,在numpy数组中,有一个内置的函数用于获取对角线索引,但我似乎无法弄清楚如何从右上角而不是左上角开始获取对角线。
这是从左上角开始的正常代码:

>>> import numpy as np
>>> array = np.arange(25).reshape(5,5)
>>> diagonal = np.diag_indices(5)
>>> array
array([[ 0,  1,  2,  3,  4],
   [ 5,  6,  7,  8,  9],
   [10, 11, 12, 13, 14],
   [15, 16, 17, 18, 19],
   [20, 21, 22, 23, 24]])
>>> array[diagonal]
array([ 0,  6, 12, 18, 24])

如果我想让它返回,我应该使用什么:

array([ 4,  8, 12, 16, 20])
kiayqfof

kiayqfof1#

In [47]: np.diag(np.fliplr(array))
Out[47]: array([ 4,  8, 12, 16, 20])

In [48]: np.diag(np.rot90(array))
Out[48]: array([ 4,  8, 12, 16, 20])

其中,np.diag(np.fliplr(array))更快:

In [50]: %timeit np.diag(np.fliplr(array))
100000 loops, best of 3: 4.29 us per loop

In [51]: %timeit np.diag(np.rot90(array))
100000 loops, best of 3: 6.09 us per loop
km0tfn4u

km0tfn4u2#

下面是使用numpy切片的简单方法。我个人觉得这对眼睛来说并不太难,但同意fliplr更有描述性。
为了突出这个例子对现有答案的贡献,我运行了相同的简单基准测试:

In [1]: import numpy as np

In [3]: X = np.random.randint(0, 10, (5, 5))

In [4]: X
Out[4]: 
array([[7, 2, 7, 3, 7],
       [8, 4, 5, 9, 6],
       [0, 2, 9, 0, 4],
       [8, 2, 1, 0, 3],
       [3, 1, 0, 7, 0]])

In [5]: Y = X[:, ::-1]

In [6]: Z1 = np.diag(Y)

In [7]: Z1
Out[7]: array([7, 9, 9, 2, 3])

与最快替代方案的奇偶校验比较:

In [8]: step = len(X) - 1

In [9]: Z2 = np.take(X, np.arange(step, X.size-1, step))

In [10]: Z2
Out[10]: array([7, 9, 9, 2, 3])

In [11]: np.array_equal(Z1, Z2)
Out[11]: True

基准测试

In [12]: %timeit np.diag(X[:, ::-1])
1.92 µs ± 29.5 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [13]: %timeit step = len(X) - 1; np.take(X, np.arange(step, X.size-1, step))
2.21 µs ± 246 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

初步比较表明,我的解决方案在复杂性上是线性的,而使用第二个“步骤”解决方案则不是:

In [14]: big_X = np.random.randint(0, 10, (10000, 10000))

In [15]: %timeit np.diag(big_X[:, ::-1])
2.15 µs ± 96.3 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [16]: %timeit step = len(big_X) - 1; np.take(big_X, np.arange(step, big_X.size-1, step))
100 µs ± 1.85 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

我通常使用这种方法来翻转图像(镜像它们),或者在opencv的格式 (通道,高度,宽度)matplotlib的格式 (高度,宽度,通道) 之间进行转换。所以对于一个三维图像,它就是flipped = image[:, :, ::-1]。当然,您可以通过将::-1部分放置在所需的维度中,将其概括为沿着任何维度翻转。

ccrfmcuu

ccrfmcuu3#

这里有两个想法:

step = len(array) - 1

# This will make a copy
array.flat[step:-step:step]

# This will make a veiw
array.ravel()[step:-step:step]

相关问题