英文:
swap columns from multidimensional array
问题
我有这个数组:
my_array = np.arange(1216800).reshape(2, 100, 78, 78)
现在的形状是:(2, 100, 78, 78)
,我想重新排列成:(100, 78, 78, 2)
。
我尝试了以下方法:
my_array[:, :, 2, :], my_array[:, :, :, 3] = my_array[:, :, :, 3], my_array[:, :, 2, :].copy()
来交换这些列,但我得到的仍然是相同的数组。
我看到了这个,但无论我尝试什么,我都得到相同的数组。
英文:
I have this array:
my_array = np.arange(1216800).reshape(2, 100, 78, 78)
The shape now is: (2, 100, 78, 78)
and I want to reorder to : (100, 78, 78, 2)
.
I tried something like:
my_array[:, :, 2, :], my_array[:, :, :, 3] = my_array[:, :, :, 3], my_array[:, :, 2, :].copy()
to swap first those columns, but I am receiving the same array.
I saw this but whatever I try, I am having the same array.
答案1
得分: 4
使用moveaxis
将第一个轴(0
)移到最后(-1
):
out = np.moveaxis(my_array, 0, -1)
out.shape
# (100, 78, 78, 2)
英文:
Use moveaxis
to move the first axis (0
) to the end (-1
):
out = np.moveaxis(my_array, 0, -1)
out.shape
# (100, 78, 78, 2)
答案2
得分: 3
以下是您想要的代码:
import numpy as np
my_array = np.arange(1216800).reshape(2, 100, 78, 78)
reordered_array = np.transpose(my_array, (1, 2, 3, 0))
print(reordered_array.shape) # 输出: (100, 78, 78, 2)
英文:
Here is the code that you want:
import numpy as np
my_array = np.arange(1216800).reshape(2, 100, 78, 78)
reordered_array = np.transpose(my_array, (1, 2, 3, 0))
print(reordered_array.shape) # Output: (100, 78, 78, 2)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论