gogoWebsite

Numpy: Flip operation of array (Ndarray) operation

Updated to 1 hour ago

Article Directory

  • Flip the array
    • Array method
    • Array properties
    • numpy method


Flip the array

Flip of arrays is also one of the more important operations of arrays, and is an operation of shape changes or dimension changes. It can be implemented through the following methods, including methods belonging to ndarray and methods belonging to numpy. In addition, the array attribute () transposes the array can also implement the function of array row and column flip.

method illustrate
() Returns the array view of the transposed axis
Array Transposition
() Returns an array view with axis1 and axis2 swapped.
() Move the axis of the array to a new position.
() Scroll backwards the specified axis until it is in the given position.

Array method

()

Returns the array view of the transposed axis.

ndarray.transpose(*axes)

Parameter description:

  • axes: optional None, integer tuple or n integers.
    • None or No Parameters: Invert the order of the axis.
    • Integer array: i at the jth position in the tuple indicates that the i-th axis of a becomes the jth axis of ().
    • n integers: This form functions the same as the tuple form.

Return value:

  • A view of the array.

If you can't understand the parameters, please see the example:

>>> a = np.array([[1, 2], [3, 4]])
>>> a
array([[1, 2],
       [3, 4]])
>>> a.transpose() # No parameters
array([[1, 3],
       [2, 4]])
>>> a.transpose((1, 0)) # There are only two axes in a two-dimensional array, 0 represents the row of the original array, and 1 represents the column of the original array. Put the original column at the position where the tuple index is 0 and turn the original row at the position where the tuple index is 1 into a column.
array([[1, 3],
       [2, 4]])
>>> a.transpose(1, 0) # A relatively convenient way to write the same function as tuple.
array([[1, 3],
       [2, 4]])

You can also refer to (). The function is the same as this function and the parameters are slightly different.

Array properties

Having an attribute () in an array means transpose, and the array is exchanged in rows and columns. 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(a, axes=None)

Parameter description:

  • a: array_like, enter array
  • axes: integer tuple 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(a, axis1, axis2)

Swap the two axes of the array.

Parameter description:

  • a: Receive array_like and enter the array.
  • axis1: receives integer, first axis.
  • axis2: receives integer, 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]])

()

Move the axis of the array to a new position.

numpy.moveaxis(a, source, destination)

Parameter description:

  • a: Receive an array of ndarray, requiring rotation of the axis.
  • source: Receive integers or sequences of integers. The original position of the axis to be moved must be unique.
  • destination: Receive integers or sequences of integers. The target position of 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) # Move 3 of the original 0 axis (3 pages) to the last axis (column), that is, it becomes 3 columns.
>>> 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 # The moved shape is 4 pages, 5 rows, 3 columns
(4, 5, 3)

()

Scroll backwards the specified axis until it is in the given position.

numpy.rollaxis(a, axis, start=0)

Parameter description:

  • a: Receive ndarray and enter the array.
  • axis: receives an integer, the axis that needs to be scrolled.
  • start: optional, receives integers.

Return value:

  • View of ndarray.

Example:

>>> a = np.ones((4,5,6)) # axis=0 represents page, axis=1 represents row, axis=2 represents column
>>> np.rollaxis(a, 2, 1).shape # Scroll axis=2, that is, column number 6 to row axis=1, that is, row
(4, 6, 5)