numpy.swapaxes


此函数交换数组的两个轴。对于1.10之后的NumPy版本,将返回交换数组的视图。该函数采用以下参数。

numpy.swapaxes(arr, axis1, axis2)

这里

序号 参数和描述
1 arr 要交换轴的输入数组
2 axis1 对应于第一轴的int
3 axis2 对应于第二轴的int

# It creates a 3 dimensional ndarray
import numpy as np
a = np.arange(8).reshape(2,2,2)

print 'The original array:'
print a
print '\n'  
# now swap numbers between axis 0 (along depth) and axis 2 (along width)

print 'The array after applying the swapaxes function:'
print np.swapaxes(a, 2, 0)

其输出如下

The original array:
[[[0 1]
 [2 3]]

 [[4 5]
  [6 7]]]

The array after applying the swapaxes function:
[[[0 4]
 [2 6]]

 [[1 5]
  [3 7]]]