在Python中,是否可以一次返回多个3D阵列切片,而无需循环?

发布于 2025-01-23 04:31:34 字数 258 浏览 1 评论 0原文

这是示例代码。我想找到一次运行最后两行并立即以同一数组返回结果的方式,而无需串联,这是否可以吗?

import numpy as np
arr = np.ones((3,3,3))
arr[0:2,0:2,0:2]
arr[1:3,1:3,1:3]

结果命令应像是结果

arr[(0:1,2:3),(0:1,2:3),(0:1,2:3)]

,结果的维度为(2,2,2,2)。

Here is example code. I want to find the way to run the last two lines at once and return the results in the same array all at once without concatenation, is this possible?

import numpy as np
arr = np.ones((3,3,3))
arr[0:2,0:2,0:2]
arr[1:3,1:3,1:3]

The resulting command should be like

arr[(0:1,2:3),(0:1,2:3),(0:1,2:3)]

And the dimensionality of the results will be (2,2,2,2).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

涫野音 2025-01-30 04:31:35

您的数组和切片:

In [128]: arr = np.arange(27).reshape(3,3,3)
In [129]: a1=arr[0:2,0:2,0:2]
     ...: a2=arr[1:3,1:3,1:3]
In [130]: a1.shape
Out[130]: (2, 2, 2)
In [131]: a2.shape
Out[131]: (2, 2, 2)

a1a2views,与arr共享数据库。

在新维度上加入它们(np.stack也将执行此操作):

In [132]: a3 = np.array((a1,a2))
In [133]: a3.shape
Out[133]: (2, 2, 2, 2)
In [134]: a3
Out[134]: 
array([[[[ 0,  1],
         [ 3,  4]],

        [[ 9, 10],
         [12, 13]]],


       [[[13, 14],
         [16, 17]],

        [[22, 23],
         [25, 26]]]])

请注意,扁平值不是连续的(或其他常规模式)。因此,他们必须进行某种形式的副本:

In [135]: a3.ravel()
Out[135]: array([ 0,  1,  3,  4,  9, 10, 12, 13, 13, 14, 16, 17, 22, 23, 25, 26])

一种替代方法是构造索引,加入它们,然后进行一个索引。那个时候大约相同。在这种情况下,我认为这会更加复杂。

===

stride_tricks的另一种方式。我不会向速度保证任何事情。

In [147]: x = np.lib.stride_tricks.sliding_window_view(arr,(2,2,2))
In [148]: x.shape
Out[148]: (2, 2, 2, 2, 2, 2)
In [149]: x[0,0,0]
Out[149]: 
array([[[ 0,  1],
        [ 3,  4]],

       [[ 9, 10],
        [12, 13]]])
In [150]: x[1,1,1]
Out[150]: 
array([[[13, 14],
        [16, 17]],

       [[22, 23],
        [25, 26]]])
In [151]: x[[0,1],[0,1],[0,1]]
Out[151]: 
array([[[[ 0,  1],
         [ 3,  4]],

        [[ 9, 10],
         [12, 13]]],


       [[[13, 14],
         [16, 17]],

        [[22, 23],
         [25, 26]]]])

Your array and slices:

In [128]: arr = np.arange(27).reshape(3,3,3)
In [129]: a1=arr[0:2,0:2,0:2]
     ...: a2=arr[1:3,1:3,1:3]
In [130]: a1.shape
Out[130]: (2, 2, 2)
In [131]: a2.shape
Out[131]: (2, 2, 2)

a1 and a2 are views, sharing the databuffer with arr.

Joining them on a new dimension (np.stack will also do this):

In [132]: a3 = np.array((a1,a2))
In [133]: a3.shape
Out[133]: (2, 2, 2, 2)
In [134]: a3
Out[134]: 
array([[[[ 0,  1],
         [ 3,  4]],

        [[ 9, 10],
         [12, 13]]],


       [[[13, 14],
         [16, 17]],

        [[22, 23],
         [25, 26]]]])

Notice how the flattened values are not contiguous (or other regular pattern). So they have to a copy of some sort:

In [135]: a3.ravel()
Out[135]: array([ 0,  1,  3,  4,  9, 10, 12, 13, 13, 14, 16, 17, 22, 23, 25, 26])

An alternative is to construct the indices, join them, and then do one indexing. That times about the same. And in this case I think that would be more complicated.

===

Another way with stride_tricks. I won't promise anything about speed.

In [147]: x = np.lib.stride_tricks.sliding_window_view(arr,(2,2,2))
In [148]: x.shape
Out[148]: (2, 2, 2, 2, 2, 2)
In [149]: x[0,0,0]
Out[149]: 
array([[[ 0,  1],
        [ 3,  4]],

       [[ 9, 10],
        [12, 13]]])
In [150]: x[1,1,1]
Out[150]: 
array([[[13, 14],
        [16, 17]],

       [[22, 23],
        [25, 26]]])
In [151]: x[[0,1],[0,1],[0,1]]
Out[151]: 
array([[[[ 0,  1],
         [ 3,  4]],

        [[ 9, 10],
         [12, 13]]],


       [[[13, 14],
         [16, 17]],

        [[22, 23],
         [25, 26]]]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文