numpy transpose function methods include: 1. transpose function, which can accept an integer tuple representing the order of dimensions as a parameter, or use default parameters to exchange all dimensions of the array; 2. T attribute, which can directly Perform the transpose operation; 3. The swapaxes function accepts two integers representing the axes as parameters and returns the swapped array; 4. The rollaxis function is used to scroll the specified axis to the specified position and accepts two integers representing the axes. Takes an integer as a parameter and returns an array after the scroll axis.
Operating system for this tutorial: Windows 10 system, Python version 3.11.4, Dell G3 computer.
NumPy is a Python library for numerical calculations that provides a powerful multidimensional array object and a series of functions for processing these arrays. In NumPy, transposition refers to exchanging the rows and columns of an array, that is, changing the rows of the array into columns and the columns of the array into rows.
NumPy provides different methods for transposing arrays. The following are some commonly used NumPy transpose function methods:
transpose function:
The transpose function is used to exchange the dimension order of the array. It can accept as argument a tuple of integers representing the order of the dimensions, or use default arguments to swap all dimensions of the array. For example, for a two-dimensional array, the transpose function will swap its rows and columns. The sample code is as follows:
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) arr_transposed = np.transpose(arr) print(arr_transposed)
The output result is:
[[1 4] [2 5] [3 6]]
T attribute:
NumPy’s ndarray object provides a T attribute that can be directly processed Transpose operation. The T attribute is a shortcut for the transpose function, which returns the transpose of an array. The sample code is as follows:
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) arr_transposed = arr.T print(arr_transposed)
The output result is:
[[1 4] [2 5] [3 6]]
swapaxes function:
swapaxes function is used to swap the two axes of the array. It accepts two integers representing the axes as arguments and returns the swapped array. The sample code is as follows:
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) arr_swapped = np.swapaxes(arr, 0, 1) print(arr_swapped)
The output result is:
[[1 4] [2 5] [3 6]]
rollaxis function:
The rollaxis function is used to roll the specified axis to the specified position . It accepts two integers representing the axes as parameters and returns an array after scrolling the axes. The sample code is as follows:
import numpy as np arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]]) arr_rolled = np.rollaxis(arr, 2, 0) print(arr_rolled)
The output result is:
[[[ 1 4] [ 7 10]] [[ 2 5] [ 8 11]] [[ 3 6] [ 9 12]]]
These are commonly used transpose function methods in NumPy. By using these methods, you can easily transpose an array.
The above is the detailed content of What are the numpy transpose function methods?. For more information, please follow other related articles on the PHP Chinese website!