python numpy transpose
时间: 2023-10-04 16:12:35 浏览: 90
numpy.transpose对三维数组的转置方法
NumPy transpose function is used to reverse or flip the array along its diagonals. It returns a view or copy of the original array and does not modify the original array. The syntax for numpy transpose is as follows:
```python
numpy.transpose(arr, axes=None)
```
Here, `arr` is the input array that needs to be transposed, and `axes` is a tuple of integers that defines the order of the axes after transposing. If `axes` is not specified, the order of the axes is reversed.
Example:
```python
import numpy as np
# create a 2D array
arr = np.array([[1, 2, 3], [4, 5, 6]])
# transpose the array
arr_transposed = np.transpose(arr)
print("Original array:\n", arr)
print("Transposed array:\n", arr_transposed)
```
Output:
```
Original array:
[[1 2 3]
[4 5 6]]
Transposed array:
[[1 4]
[2 5]
[3 6]]
```
In this example, the input array is a 2D array with two rows and three columns. After transposing, the rows become columns and vice versa, resulting in a 3x2 array.
阅读全文