Article directory
flip array
The flipping of the array is also one of the more important operations of the array, which belongs to the operation of changing shape or dimension. It can be achieved by the following methods, which have methods belonging to ndarray and methods belonging to numpy. In addition, the property of the array (ndarray.T) is transposed, which can also realize the function of flipping the rows and columns of the array.
method | illustrate |
---|---|
ndarray.transpose () | Returns an array view of the transposed axes |
ndarray.T | array transpose |
numpy.swapaxes() | Returns an array view with axis1 and axis2 swapped. |
numpy.moveaxis() | Move the axis of the array to a new position. |
numpy.rollaxis() | Scrolls the specified axis backwards until it is at the given position. |
array method
ndarray.transpose ()
Returns an array view of the transposed axes.
ndarray.transpose(*axes)
Parameter Description:
- axes: optional None, a tuple of integers or n integers.
- None or no arguments: Reverse the order of the axes.
- Integer array: i at the jth position in the tuple means that the ith axis of a becomes the jth axis of a.transpose().
- n integers: This form is functionally identical to the tuple form.
return value:
- view of the array.
If you don't understand the parameters, please see the example:
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
[3, 4]])
>>> a.transpose() # 无参数
array([[1, 3],
[2, 4]])
>>> a.transpose((1, 0)) # 二维数组只有两个轴,0表示原数组的行,1表示原数组的列,将原来的列放在元组索引为0的位置变成行,将原来的行放在元组索引为1的位置变为列。
array([[1, 3],
[2, 4]])
>>> a.transpose(1, 0) # 与元组功能相同,比较方便的一种写法。
array([[1, 3],
[2, 4]])
At the same time, you can also refer to numpy.transpose(), the function is the same as this function, but the parameters are slightly different.
array property
There is an attribute (ndarray.T) in the array, which means transpose, which exchanges the rows and columns of the array. The example is as follows:
>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
[3, 4]])
>>> a.T
array([[1, 3],
[2, 4]])
numpy method
numpy.transpose()
numpy.transpose(a, axes=None)
Parameter Description:
- a: array_like, input array
- axes: tuple of integers or list of integers
return value:
- ndarray, view
Example:
>>> x = np.arange(4).reshape((2,2))
>>> x
array([[0, 1],
[2, 3]])
>>> np.transpose(x)
array([[0, 2],
[1, 3]])
numpy.swapaxes()
numpy.swapaxes(a, axis1, axis2)
Swap the two axes of the array.
Parameter Description:
- a: Receive array_like, input array.
- axis1: receives an integer, the first axis.
- axis2: receives an integer, the second axis.
return value:
- ndarray after swapping axes
Example:
>>> x = np.array([[1,2,3]])
>>> x
array([[1, 2, 3]])
>>> np.swapaxes(x,0,1)
array([[1],
[2],
[3]])
ndarray.moveaxis ()
Move the axis of the array to a new position.
numpy.moveaxis(a, source, destination)
Parameter Description:
- a: Receive ndarray, the array that needs to rotate the axis.
- source: Receives an integer or a sequence of integers. The original position of the axis to be moved, must be unique.
- destination: Receives an integer or a sequence of integers. The target position for each original axis must also be unique.
return value:
- ndarray
Example:
>>> x = np.arange(1,61).reshape(3,4,5)
>>> x.shape
(3,4,5)
>>> x
array([[[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]],
[[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35],
[36, 37, 38, 39, 40]],
[[41, 42, 43, 44, 45],
[46, 47, 48, 49, 50],
[51, 52, 53, 54, 55],
[56, 57, 58, 59, 60]]])
>>> y = np.moveaxis(x, 0, -1) # 将原来0轴(3页)中的3移动到最后一个轴(列),也就是变成3列。
>>> y
array([[[ 1, 21, 41],
[ 2, 22, 42],
[ 3, 23, 43],
[ 4, 24, 44],
[ 5, 25, 45]],
[[ 6, 26, 46],
[ 7, 27, 47],
[ 8, 28, 48],
[ 9, 29, 49],
[10, 30, 50]],
[[11, 31, 51],
[12, 32, 52],
[13, 33, 53],
[14, 34, 54],
[15, 35, 55]],
[[16, 36, 56],
[17, 37, 57],
[18, 38, 58],
[19, 39, 59],
[20, 40, 60]]])
>>> y.shape # 移动后的形状成4页,5行,3列
(4, 5, 3)
numpy.rollaxis()
Scrolls the specified axis backwards until it is at the given position.
numpy.rollaxis(a, axis, start=0)
Parameter Description:
- a: Receive ndarray, input array.
- axis: receives an integer, the axis that needs to be scrolled.
- start: optional, receives an integer.
return value:
- A view of the ndarray.
Example:
>>> a = np.ones((4,5,6)) # axis=0表示页,axis=1表示行,axis=2表示列
>>> np.rollaxis(a, 2, 1).shape # 将axis=2也就是列数6滚动到行axis=1也就是行
(4, 6, 5)