[英]Efficiently reshape/reorder numpy array from (a, b, c) to (a, c, b)
I have several Numpy 3D arrays of the shape ( a
, b
, c
).我有几个形状的 Numpy 3D arrays (
a
, b
, c
)。 The values of a
, b
, and c
are unknown. a
、 b
和c
的值未知。 However, I want to reshape each of the arrays to ( a
, c
, b
) in an efficient way.但是,我想以有效的方式将每个 arrays 重塑为(
a
, c
, b
)。
Here is what I am doing:这是我正在做的事情:
for array in list_of_arrays:
a, b, c = array.shape
array = array.reshape(a, c, b)
Is there a more efficient way to do this, possibly in one line of code?有没有更有效的方法来做到这一点,可能在一行代码中? Can I use the
-1
indexing method to reshape/reorder the arrays?我可以使用
-1
索引方法对 arrays 进行整形/重新排序吗?
Thank you.谢谢你。
import numpy as np
# Example array with shape (2, 4, 6)
array = np.arange(48).reshape((2, 4, 6))
# Swap axis in the 1st and 2nd dimension and print out its shape
np.swapaxis(array, 1, 2).shape
Output: Output:
(2, 6, 4)
Maybe np.transpose
?也许
np.transpose
? It swaps all dimensions to the specified order.它将所有维度交换到指定的顺序。
x = np.random.randint(0, 256, (100, 80, 3))
np.transpose(x, (1, 0, 2))
(80, 100, 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.