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


Python numpy.swapaxes()用法及代碼示例

numpy.swapaxes()函數互換數組的兩個軸。

用法: numpy.swapaxes(arr, axis1, axis2)
參數:
arr :[array_like] input array.
axis1 :[int] First axis.
axis2 :[int] Second axis.
Return :[ndarray] In earlier NumPy versions, a view of arr is returned only if the order of the axes is changed, otherwise the input array is returned. For NumPy >= 1.10.0, if arr is an ndarray, then a view of arr is returned; otherwise a new array is created.

代碼1:

# Python program explaining 
# numpy.swapaxes() function 
  
# importing numpy as geek  
import numpy as geek 
  
arr = geek.array([[2, 4, 6]]) 
  
gfg = geek.swapaxes(arr, 0, 1) 
  
print (gfg)

輸出:

[[2]
 [4]
 [6]]


代碼2:

# Python program explaining 
# numpy.swapaxes() function 
  
# importing numpy as geek  
import numpy as geek 
  
arr = geek.array([[[0, 1], [2, 3]], [[4, 5], [6, 7]]]) 
  
gfg = geek.swapaxes(arr, 0, 2) 
  
print (gfg)

輸出:

[[[0 4]
  [2 6]]
 [[1 5]
  [3 7]]]

相關用法


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