交换多维数组中的列

huangapple go评论64阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年7月17日 19:11:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703878.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定