當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python numpy moveaxis用法及代碼示例


本文簡要介紹 python 語言中 numpy.moveaxis 的用法。

用法:

numpy.moveaxis(a, source, destination)

將數組的軸移動到新位置。

其他軸保持原來的順序。

參數

a np.ndarray

軸應重新排序的數組。

source int 或 int 的序列

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

destination int 或 int 的序列

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

返回

result np.ndarray

帶有移動軸的數組。該數組是輸入數組的視圖。

例子

>>> 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)

相關用法


注:本文由純淨天空篩選整理自numpy.org大神的英文原創作品 numpy.moveaxis。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。