繁体   English   中英

从 (a, b, c) 到 (a, c, b)

[英]Efficiently reshape/reorder numpy array from (a, b, c) to (a, c, b)

我有几个形状的 Numpy 3D arrays ( abc )。 abc的值未知。 但是,我想以有效的方式将每个 arrays 重塑为( acb )。

这是我正在做的事情:

for array in list_of_arrays:
    a, b, c = array.shape
    array = array.reshape(a, c, b)

有没有更有效的方法来做到这一点,可能在一行代码中? 我可以使用-1索引方法对 arrays 进行整形/重新排序吗?

谢谢你。

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:

(2, 6, 4)

也许np.transpose 它将所有维度交换到指定的顺序。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM