np.transpose
时间: 2023-09-07 20:17:12 浏览: 35
The np.transpose function in NumPy returns the transpose of a given array. The transpose of an array is obtained by interchanging the rows and columns of the array.
For example, if we have a 2D array A with shape (m, n), then the transpose of A would be a new array with shape (n, m), where the rows and columns of A are swapped.
Here's an example of how to use np.transpose:
```
import numpy as np
A = np.array([[1, 2], [3, 4], [5, 6]])
print(A)
# Output:
# [[1 2]
# [3 4]
# [5 6]]
A_transpose = np.transpose(A)
print(A_transpose)
# Output:
# [[1 3 5]
# [2 4 6]]
```
In this example, we first create a 2D array A with shape (3, 2). We then use np.transpose to obtain the transpose of A, which is a new array with shape (2, 3). The rows and columns of A are swapped, so the first row of A becomes the first column of A_transpose, the second row of A becomes the second column of A_transpose, and so on.