python-scipyHow do I use the NumPy transpose function in Python?
The NumPy transpose function is used to transpose a given array in Python. Transposing an array means to exchange the rows and columns of the array. It can be used by passing the array into the transpose() function.
import numpy as np
arr = np.array([[1,2,3], [4,5,6]])
print("Original array:")
print(arr)
transposed_arr = np.transpose(arr)
print("Transposed array:")
print(transposed_arr)
Output example
Original array:
[[1 2 3]
[4 5 6]]
Transposed array:
[[1 4]
[2 5]
[3 6]]
The code above consists of the following parts:
- import numpy as np: imports the NumPy library into the program.
- arr = np.array([[1,2,3], [4,5,6]]): creates an array with two rows and three columns.
- print("Original array:"): prints the text "Original array:" to the console.
- print(arr): prints the original array to the console.
- transposed_arr = np.transpose(arr): transposes the array.
- print("Transposed array:"): prints the text "Transposed array:" to the console.
- print(transposed_arr): prints the transposed array to the console.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How can I use Python and Numpy to parse XML data?
- How do I download a Python Scipy .whl file?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
- How do I use Python XlsxWriter to write a NumPy array to an Excel file?
- How do I use the scipy ttest_ind function in Python?
- How do I use Python Numpy to read and write Excel (.xlsx) files?
- How can I use Python and SciPy to read and write WAV files?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use scipy.optimize.curve_fit in Python?
See more codes...