我们先看看numpy的文档对这几个操作的解释,如果你已经对这几个操作已经很熟悉,请直接跳到最后一个部分
函数用途
在NumPy v1.15 Manual中,这四个函数是这样解释的
numpy.transpose
numpy.
transpose
(a, axes=None)[source]
Permute the dimensions of an array.
Parameters: | a : array_like Input array. axes : list of ints, optional By default, reverse the dimensions, otherwise permute the axes according to the values given. |
---|---|
Returns: | p : ndarray a with its axes permuted. A view is returned whenever possible. |
See also
Notes
Use transpose(a, argsort(axes)) to invert the transposition of tensors when using the axes keyword argument.
Transposing a 1-D array returns an unchanged view of the original array.
中文略解
此函数用于对换数组的维度
arr
:要操作的数组
axes
:整数列表,表示对应维度
Examples
>>>
>>> x = np.arange(4).reshape((2,2))
>>> x
array([[0, 1],
[2, 3]])
>>>
>>> np.transpose(x)
array([[0, 2],
[1, 3]])
>>>
>>> x = np.ones((1, 2, 3))
>>> np.transpose(x, (1, 0, 2)).shape
(2, 1, 3)
numpy.rollaxis
numpy.
rollaxis
(a, axis, start=0)[source]
Roll the specified axis backwards, until it lies in a given position.
This function continues to be supported for backward compatibility, but you should prefer moveaxis
. The moveaxis
function was added in NumPy 1.11.
Parameters: | a : ndarray Input array. axis : int The axis to roll backwards. The positions of the other axes do not change relative to one another. start : int, optional The axis is rolled until it lies before this position. The default, 0, results in a “complete” roll. |
---|---|
Returns: | res : ndarray For NumPy >= 1.10.0 a view of a is always returned. For earlier NumPy versions a view of a is returned only if the order of the axes is changed, otherwise the input array is returned. |
See also
Move array axes to new positions.
Roll the elements of an array by a number of positions along a given axis.
中文略解
此函数用于向后滚动特定的轴到一个特定位置
arr
:所操作的数组
axis
:要向后滚动的轴,其它轴的相对位置不会改变
start
:默认为零,表示完整的滚动。会滚动到特定位置。
Examples
>>>
>>> a = np.ones((3,4,5,6))
>>> np.rollaxis(a, 3, 1).shape
(3, 6, 4, 5)
>>> np.rollaxis(a, 2).shape
(5, 3, 4, 6)
>>> np.rollaxis(a, 1, 4).shape
(3, 5, 6, 4)
numpy.swapaxes
numpy.
swapaxes
(a, axis1, axis2)[source]
Interchange two axes of an array.
Parameters: | a : array_like Input array. axis1 : int First axis. axis2 : int Second axis. |
---|---|
Returns: | a_swapped : ndarray For NumPy >= 1.10.0, if a is an ndarray, then a view of a is returned; otherwise a new array is created. For earlier NumPy versions a view of a is returned only if the order of the axes is changed, otherwise the input array is returned. |
中文略解
此函数用于交换数组的两个轴
arr
:所操作的数组
axis1
:对应的第一个轴编号
axis2
:对应的第二个轴编号
Examples
>>>
>>> x = np.array([[1,2,3]])
>>> np.swapaxes(x,0,1)
array([[1],
[2],
[3]])
>>>
>>> x = np.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> x
array([[[0, 1],
[2, 3]],
[[4, 5],
[6, 7]]])
>>>
>>> np.swapaxes(x,0,2)
array([[[0, 4],
[2, 6]],
[[1, 5],
[3, 7]]])
numpy.ndarray.T
ndarray.
T
Same as self.transpose(), except that self is returned if self.ndim < 2.
中文略解
求转置(是线性代数中的一种概念)
Examples
>>>
>>> x = np.array([[1.,2.],[3.,4.]])
>>> x
array([[ 1., 2.],
[ 3., 4.]])
>>> x.T
array([[ 1., 3.],
[ 2., 4.]])
>>> x = np.array([1.,2.,3.,4.])
>>> x
array([ 1., 2., 3., 4.])
>>> x.T
array([ 1., 2., 3., 4.])
四个函数之间的联系
numpy.ndarray.T相当于numpy.transpose在二维矩阵情况下的一种简便表达
numpy.rollaxis是numpy.swapaxes的特殊情况,numpy.swapaxes(a,0,1)相当于numpy.rollaxis(a,1)
numpy.rollaxis大家可以将其看作0轴与某轴互换
numpy轴的理解
翻转维度可以可以理解成将a[i][j][k]=10翻转0,2轴得a[k][j][i]=10
可以用正方体理解它,笔者比较懒,就没有画图了,可以看作是将正方体转了一下
另外还有一些排序,比较大小的函数,可以将其看作其中两个下标固定,沿着选定的轴操作。
比如a[i固定][j固定][k]可以理解为按k02轴操作