NumPy numpy.transpose()
transposes a 2-dimensional array by moving the rows data to the column and the columns data to the rows.
Here is an example of how numpy.transpose()
works.
import numpy as np
# 👇️ create a numpy array
example_array = np.array(
[ [1, 2, 3],
[4, 5, 6] ]
)
print(example_array)
>> [ [1, 2, 3],
[4, 5, 6] ]
# 👇️ this is the transposed array
transposed_array = example_array.transpose()
print(transposed_array)
>> [ [1, 4]
[2, 5]
[3, 6] ]
A 1-dimensional NumPy array cannot be transposed. If you try this, you will get not an error, but the array will stay the same.
import numpy as np
# 👇️ create a 1D numpy array
example_1d_array = np.array([1, 2, 3, 4, 5])
print(example_1d_array)
>> [1, 2, 3, 4, 5]
# 👇️ the transposed array is the same as the original array
transposed_1d_array = example_1d_array.transpose()
print(transposed_1d_array)
>> [1, 2, 3, 4, 5]
NumPy numpy.transpose()
transposes a 2-dimensional array by moving the rows data to the column and the columns data to the rows.
Related tutorials curated for you
How to use numpy.round() in Python
How to use numpy.linspace() in Python
NumPy: np.arrange()
How to use numpy.zeros() in Python
How to use numpy.clip() in Python
How to use numpy.exp() in Python
How to use numpy.vstack() in Python
How to use numpy.ravel() in Python
What does numpy.arrange() do?
How to use numpy.ndarray.flatten() in Python
ModuleNotFoundError: No module named 'numpy' in Python
How to use numpy.percentile() in Python