在堆叠的 np.array 上滑动窗口(Python)
我正在尝试跨堆栈 np 数组创建一个多维窗口,并找到每个窗口的最大值。下面的 res
是一个形状为 (3, 4, 4) 的堆叠数组。我想要一个通过轴的尺寸为 2x2 的窗口。例如,第一个窗口将为 (3,2,2),其值为:
ideal_result = np.array([[13, 45], [1, 2], [11, 22], [1, 2], [1, 2], [1, 7]])
然后 max 窗口将为: np.max(ideal_result) = 45
这将是整个窗口并构造一个 2x2使用 np.array([[45 67],[23 44]])
设置:
a = np.array([[13, 45, 67, 4], [1, 2, 3, 4], [2, 3, 4, 6], [1, 23, 44, 1]])
b = np.array([[11, 22, 33, 57], [1, 2, 3, 4], [2, 3, 94, 6], [1, 23, 44, 1]])
c = np.array([[1, 2, 3, 4], [1, 7, 8, 9], [2, 3, 4, 76], [1, 23, 44, 1]])
res = np.stack((a, b, c))
print(np.shape(res))
尝试的代码:
import numpy as np
v = np.lib.stride_tricks.as_strided(res, shape=(3, 2, 2), strides=(3, 2, 2))
I am trying to create a multi-dimensional window across a stack np array and find the max of each of the windows. Below res
is a stacked array with shape (3, 4, 4). I would like to have a window with 2x2 size through the axis. For example the first window will be (3,2,2) with values:
ideal_result = np.array([[13, 45], [1, 2], [11, 22], [1, 2], [1, 2], [1, 7]])
then max the window would be: np.max(ideal_result) = 45
This would be be the entire windows and construct a 2x2 with np.array([[45 67],[23 44]])
Set up:
a = np.array([[13, 45, 67, 4], [1, 2, 3, 4], [2, 3, 4, 6], [1, 23, 44, 1]])
b = np.array([[11, 22, 33, 57], [1, 2, 3, 4], [2, 3, 94, 6], [1, 23, 44, 1]])
c = np.array([[1, 2, 3, 4], [1, 7, 8, 9], [2, 3, 4, 76], [1, 23, 44, 1]])
res = np.stack((a, b, c))
print(np.shape(res))
Code Attempted:
import numpy as np
v = np.lib.stride_tricks.as_strided(res, shape=(3, 2, 2), strides=(3, 2, 2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常有助于思考展平数组看起来如何以获得正确的步幅:
输出:
数据类型为 int64,它是 8 个字节,因此每个连续元素之间的步幅是 8。我将标记哪个元素应该位于第一个窗口中,步幅为:
你能看到图案吗?
我们为每个维度设置以下值和步长:
输出:
这只是一个窗口,我们需要 4 个窗口,每个窗口之间的跳转在 x 方向上为 8*2 字节,在 y 方向上为 8*8 字节。
输出:
假设步幅与内核大小相同(如传统的 2D 最大池化)。
It usually helps to think about how the flattened array looks to get the strides right:
output:
the dtype is
int64
which is 8 bytes, so the stride between each consecutive element is 8. I'll mark which elements should be in the first window with the stride:Can you see the pattern?
We have the following values and strides for each dimension:
output:
This is just one window and we want 4 of them, the jump between each window is 8*2 bytes in x direction and 8*8 bytes in y direction.
output:
Assuming that the stride is the same as the kernel size (as in conventional 2D max pooling).