ram*_*793 5 python arrays numpy
我有以下多维数组:
windows = array([[[[[[0., 0.],
[1., 0.]],
[[0., 0.],
[1., 0.]],
[[0., 0.],
[1., 0.]]],
[[[0., 1.],
[0., 0.]],
[[0., 1.],
[0., 0.]],
[[1., 0.],
[0., 0.]]],
[[[1., 0.],
[0., 0.]],
[[0., 1.],
[0., 0.]],
[[0., 1.],
[0., 0.]]]]]])
print(windows.shape)
(1, 1, 3, 3, 2, 2) # (n, d, a, b, c, c) w = a * (c*c), h = b * (c*c)
Run Code Online (Sandbox Code Playgroud)
我想得到下一个结果数组:
mask = array([[
[[0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0.],
[0., 1., 0., 1., 1., 0.],
[0., 0., 0., 0., 0., 0.],
[1., 0., 0., 1., 0., 1.],
[0., 0., 0., 0., 0., 0.]]
]], dtype=np.float32)
print(mask.shape)
(1, 1, 6, 6) # (n, d, w, h)
Run Code Online (Sandbox Code Playgroud)
基本上,我想将最后 4 个维度压缩成二维矩阵,以便最终形状变为 (n, d, w, h),在本例中为 (1, 1, 6, 6)。
我尝试过np.concatenate(windows, axis = 2),但它没有沿着第二维连接并首先由于某种原因减少(尽管我设置 axis = 2)“n”维。
附加信息:
Windows 是以下代码片段的结果
windows = np.lib.stride_tricks.sliding_window_view(arr, (c, c), axis (-2,-1), writeable = True) # arr.shape == mask.shape
windows = windows[:, :, ::c, ::c] # these are non-overlapping windows of arr with size (c,c)
windows = ... # some modifications of windows
Run Code Online (Sandbox Code Playgroud)
现在我想从这些形状为 的窗口数组中构建arr.shape,该数组mask在上面的示例中调用。简单的重塑不起作用,因为它返回的元素顺序错误。
IIUC,您想要合并维度 2+4 和 3+5,一个简单的方法是合并维度swapaxes4 和 5(或 -3 和 -2),以及reshape合并维度(1,1,6,6):
windows.swapaxes(-2,-3).reshape(1,1,6,6)
Run Code Online (Sandbox Code Playgroud)
输出:
array([[[[0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 1., 0.],
[0., 1., 0., 1., 1., 0.],
[0., 0., 0., 0., 0., 0.],
[1., 0., 0., 1., 0., 1.],
[0., 0., 0., 0., 0., 0.]]]])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
107 次 |
| 最近记录: |