【发布时间】:2018-05-12 16:35:20
【问题描述】:
我正在尝试以下列方式转换一个 numpy ndarray。
这是我目前rot_F给出的数组:
# F is laid out to be human readable here
F = np.array(
[
[# Filter 0
[ # Depth 0
[1, -1],
[2, 0]
],
[ # Depth 1
[ 0, 0],
[-1, -1]
]
],
[# Filter 1
[ # Depth 0
[0, -1],
[3, 0]
],
[ # Depth 1
[ 1, 2],
[-1, -1]
]
]
]
)
F = np.moveaxis(F,1,3)
# rotate F by 180 degrees along axes (1,2)
rot_F = np.rot90(F,2,(1,2))
print(rot_F)
OUTPUT FROM print(rot_F):
[[[[ 0 -1]
[ 2 -1]]
[[-1 0]
[ 1 0]]]
[[[ 0 -1]
[ 3 -1]]
[[-1 2]
[ 0 1]]]]
现在我想把 rot_F 变成如下:
desired_filters = np.zeros_like(rot_F)
desired_filters[0,:,:,0] = np.array([[0,2],[-1,1]])
desired_filters[0,:,:,1] = np.array([[0,3],[-1,0]])
desired_filters[1,:,:,0] = np.array([[-1,-1],[0,0]])
desired_filters[1,:,:,1] = np.array([[-1,-1],[2,1]])
print(desired_filters)
OUTPUT FROM print(desired_filters):
[[[[ 0 0]
[ 2 3]]
[[-1 -1]
[ 1 0]]]
[[[-1 -1]
[-1 -1]]
[[ 0 2]
[ 0 1]]]]
基本上,我尝试将 rot_F 中第 0 个深度维度上的所有数组沿它们自己的深度维度堆叠,并将 rot_F 中第 1 个深度维度上的所有数组堆叠在它们自己的深度维度上,同时保留原始形状。
【问题讨论】:
-
确保您理解
np.rot90。在其代码中,我看到了transpose和flip的使用。flip是[::-1]索引。 -
有了这些初始的
F值,识别rot_F中发生了什么变化或所需的输出是乏味的。没有明显的规律。 -
在 rot_f 中,每个二维数组旋转 180 度。在所需的输出中,深度 0 处的所有二维数组现在都沿它们自己的深度轴堆叠,深度 1 也是如此。
标签: numpy