前言
本篇总结、介绍Numpy数组(ndarray)的基本操作之一——类转置操作 [1]。
1. moveaxis
numpy.moveaxis(a, source, destination):将数组(a)的轴(source)移动到新的位置(destination),其他轴维持原有顺序不变
- a:类数组
- source:整数或整数序列。待移动的轴的原始位置
- destination:整数或整数序列。待移动的轴要移动到的位置
>>> arr = np.arange(24).reshape(2,3,4)
>>> arr
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> np.moveaxis(arr,0,2)
array([[[ 0, 12],
[ 1, 13],
[ 2, 14],
[ 3, 15]],
[[ 4, 16],
[ 5, 17],
[ 6, 18],
[ 7, 19]],
[[ 8, 20],
[ 9, 21],
[10, 22],
[11, 23]]])
>>> np.moveaxis(arr,0,2).shape
(3, 4, 2)
>>> np.moveaxis(arr,0,1)
array([[[ 0, 1, 2, 3],
[12, 13, 14, 15]],
[[ 4, 5, 6, 7],
[16, 17, 18, 19]],
[[ 8, 9, 10, 11],
[20, 21, 22, 23]]])
>>> np.moveaxis(arr,0,1).shape
(3, 2, 4)
>>> np.moveaxis(arr,1,2)
array([[[ 0, 4, 8],
[ 1, 5, 9],
[ 2, 6, 10],
[ 3, 7, 11]],
[[12, 16, 20],
[13, 17, 21],
[14, 18, 22],
[15, 19, 23]]])
>>> np.moveaxis(arr,1,2).shape
(2, 4, 3)
>>> np.moveaxis(arr,(0,1),(1,2))
array([[[ 0, 4, 8],
[12, 16, 20]],
[[ 1, 5, 9],
[13, 17, 21]],
[[ 2, 6, 10],
[14, 18, 22]],
[[ 3, 7, 11],
[15, 19, 23]]])
>>> np.moveaxis(arr,(0,1),(1,2)).shape
(4, 2, 3)
2. rollaxis
numpy.rollaxis(a, axis, start=0):向后滚动特定的轴到一个特定位置
- a:类数组。输入数组
- axis:整数。要向后滚动的轴,其它轴的相对位置不会改变
- start:整数,可选参数。指示上述轴要滚动到的特定位置
- 如果start <= axis:axis指定的轴滚动到start指定的位置
- 如果start > axis:axis指定的轴滚动到start指定的前一个位置
>>> arr = np.arange(24).reshape(1,2,3,4)
>>> arr
array([[[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]])
>>> np.rollaxis(arr,1,3)
array([[[[ 0, 1, 2, 3],
[12, 13, 14, 15]],
[[ 4, 5, 6, 7],
[16, 17, 18, 19]],
[[ 8, 9, 10, 11],
[20, 21, 22, 23]]]])
>>> np.rollaxis(arr,1,3).shape
(1, 3, 2, 4)
>>> np.rollaxis(arr,3,1)
array([[[[ 0, 4, 8],
[12, 16, 20]],
[[ 1, 5, 9],
[13, 17, 21]],
[[ 2, 6, 10],
[14, 18, 22]],
[[ 3, 7, 11],
[15, 19, 23]]]])
>>> np.rollaxis(arr,3,1).shape
(1, 4, 2, 3)
3. swapaxes
numpy.swapaxes(a, axis1, axis2):交换数组的两个轴
- a:类数组。输入数组
- axis1:整数。第一个轴
- axis2:整数。第二个轴
>>> arr = np.arange(24).reshape(1,2,3,4)
>>> arr
array([[[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]])
>>> np.swapaxes(arr,0,1)
array([[[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]]],
[[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]])
>>> np.swapaxes(arr,0,2)
array([[[[ 0, 1, 2, 3]],
[[12, 13, 14, 15]]],
[[[ 4, 5, 6, 7]],
[[16, 17, 18, 19]]],
[[[ 8, 9, 10, 11]],
[[20, 21, 22, 23]]]])
>>> np.swapaxes(arr,0,3)
array([[[[ 0],
[ 4],
[ 8]],
[[12],
[16],
[20]]],
[[[ 1],
[ 5],
[ 9]],
[[13],
[17],
[21]]],
[[[ 2],
[ 6],
[10]],
[[14],
[18],
[22]]],
[[[ 3],
[ 7],
[11]],
[[15],
[19],
[23]]]])
4. transpose
numpy.transpose(a, axes=None):反转或置换数组的轴,返回修改后的数组
- a:类数组。输入数组
- 一维数组:返回原始数组形状未变的视图
- axes:整数序列,可选参数
- 如果指定:axes应该是[0,1,…,a.ndim-1]的一个排列,表示置换后数组的每个轴对应原始数组的哪个轴。例如,[2,0,1]表示置换后数组的第一个轴对应原始数组的第三个轴,置换后数组的第二个轴对应原始数组的第一个轴,置换后数组的第三个轴对应原始数组的第二个轴。假设数组a的形状为(2,3,4),那么numpy.transpose(a,axes=[2,0,1])返回的数组形状为(4,2,3)
- 如果未指定:axes默认为[a.ndim-1,…,1,0],表示翻转数组的轴的顺序
>>> arr = np.arange(24).reshape(2,3,4)
>>> arr
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]])
>>> np.transpose(arr)
array([[[ 0, 12],
[ 4, 16],
[ 8, 20]],
[[ 1, 13],
[ 5, 17],
[ 9, 21]],
[[ 2, 14],
[ 6, 18],
[10, 22]],
[[ 3, 15],
[ 7, 19],
[11, 23]]])
>>> np.transpose(arr).shape
(4, 3, 2)
>>> np.transpose(arr,axes=[2,0,1])
array([[[ 0, 4, 8],
[12, 16, 20]],
[[ 1, 5, 9],
[13, 17, 21]],
[[ 2, 6, 10],
[14, 18, 22]],
[[ 3, 7, 11],
[15, 19, 23]]])
>>> np.transpose(arr,axes=[2,0,1]).shape
(4, 2, 3)