numpy.moveaxis#

numpy.moveaxis(a, source, destination)[原始碼]#

將陣列的軸移動到新位置。

其他軸保持其原始順序。

參數:
anp.ndarray

軸應重新排序的陣列。

sourceint 或 int 序列

要移動的軸的原始位置。 這些位置必須是唯一的。

destinationint 或 int 序列

每個原始軸的目的位置。 這些位置也必須是唯一的。

返回:
resultnp.ndarray

具有移動軸的陣列。 此陣列是輸入陣列的視圖。

參見

transpose

置換陣列的維度。

swapaxes

交換陣列的兩個軸。

範例

>>> import numpy as np
>>> x = np.zeros((3, 4, 5))
>>> np.moveaxis(x, 0, -1).shape
(4, 5, 3)
>>> np.moveaxis(x, -1, 0).shape
(5, 3, 4)

這些都達到相同的結果

>>> np.transpose(x).shape
(5, 4, 3)
>>> np.swapaxes(x, 0, -1).shape
(5, 4, 3)
>>> np.moveaxis(x, [0, 1], [-1, -2]).shape
(5, 4, 3)
>>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape
(5, 4, 3)