[英]Efficiently reshape/reorder numpy array from (a, b, c) to (a, c, b)
我有幾個形狀的 Numpy 3D arrays ( a
, b
, c
)。 a
、 b
和c
的值未知。 但是,我想以有效的方式將每個 arrays 重塑為( a
, c
, b
)。
這是我正在做的事情:
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.