In Python NumPy transpose()
is used to get the permute or reserve the dimension of the input array meaning it converts the row elements into column elements and the column elements into row elements. numpy.transpose()
is mainly used to transpose the 2-dimension arrays. This function does not show any effect on the one-D array, When you try transposing a 1-D array returns an unmodified view of the original array.
In this article, I will explain the Python NumPy transpose()
method syntax, parameters, and usage of how to reverse the dimensions of the given array with examples. To learn more examples on NumPy arrays refer NumPy Tutorial.
1. Quick Examples of transpose() Function
If you are in a hurry, below are some quick examples of how to use the numpy.transpose() function.
# Quick examples of transpose() function
import numpy as np
# Create a 2-D numpy array
arr = np.array([[12, 14,17],[13, 16, 24],[29,35,41]])
# Example 1: Get the transpose of an array
transposed_array = np.transpose(arr)
# Example 2: Use numpy.transpose() function
# Transposed array with custom axes
transposed_array = np.transpose(arr, axes=(1, 0))
# Example 3: Transpose the array using .T attribute
transposed_array = arr.T
# Example 4: Use numpy.transpose()
# To reposition elements
arr = np.ones((12,14,17,19))
arr1 = np.transpose(arr,(1,0,2,3)).shape
# Example 5: Use numpy.transpose()
# To reposition elements
arr1 = np.transpose(arr,(1,0,2,3)).shape
2. Syntax of NumPy transpose()
The following is the syntax of the numpy transpose() function.
# Syntax of transpose()
numpy.transpose(array, axes=None)
2.1 Parameters of transpose()
This function allows the following parameters.
array
– The input array to be transposed.axes
– List of ints or tuple of ints, optional corresponding to the dimensions. By default, the dimensions are reversed. If anyone wants to pass the parameter then you can but it’s not all required. But if you want then remember to only pass (0, 1) or (1, 0). Like you have an array of shapes (2, 3) to change it (3, 2) you should pass (1, 0) where 1 as 2 and 0 as 3.
2.2 Return Value of transpose()
It returns the input array with its permuted axes.
3. Usage of NumPy transpose() Function
The numpy.transpose()
function is a versatile tool in NumPy for rearranging the dimensions of an array. It’s useful when you need to perform operations that require swapping rows and columns, or when you want to change the shape of your array to suit a particular computation.
This returns a NumPy array by interchanging (transpose) each row and the corresponding column. The new array is called the transpose of the given array. If you have an array of shape (a, b) then the transpose of the array will have the shape (b, a). transpose()
function is used to return the transformed of the given array.
3.1 Get the Transpose Elements of the Array
To create a 2D NumPy array and then calculate its transpose using the numpy.transpose()
function. In the transposed array, the rows of the original array become columns, and the columns become rows. The np.transpose()
function effectively swaps the rows and columns of the input array.
# Import numpy
import numpy as np
# Create a 2-D numpy array
arr = np.array([[12, 14,17],[13, 16, 24],[29,35,41]])
print("Original Array:\n",arr)
# Get the transpose of an array
transposed_array = np.transpose(arr)
print("Transposed Array:\n",transposed_array)
Yields below output.
4. Get the transpose of Array along with Axis
Alternatively, if you want to specify custom axes for transposing a NumPy array, you can pass the axes
parameter to the numpy.transpose()
function. This allows you to rearrange the dimensions of the array based on the provided axis order.
In the below example, you have specified axes=(1,0)
when transposing the array. This means that the first axis (axis 0) of the original array becomes the second axis (axis 1) in the transposed array, and vice versa.
# Use numpy.transpose() function
# Transposed array with custom axes
transposed_array = np.transpose(arr, axes=(1, 0))
print("Transposed array with custom axes:\n",transposed_array)
Yields the same output as above.
5. Use T Object to Transpose Array
You can also use the .T
attribute of a NumPy array to obtain its transpose. When you use the .T
attribute, it effectively transposes the array, swapping the rows and columns. For example, the .T
attribute is used to obtain the transpose of the arr
array, resulting in the transposed array.
# Create a 2-D numpy array
arr = np.array([[12, 14,17],[13, 16, 24],[29,35,41]])
print("Original Array:\n",arr)
# Transpose the array using .T attribute
transposed_array = arr.T
print("Transposed Array using .T attribute:\n", transposed_array)
Yields the same output as above.
6. Use transpose() to Reposition the NumPy Array Elements
Create NumPy array using numpy.ones() function and reposition the array elements using numpy.transpose()
function along with shape
function.
# Create an array using ones()
arr = np.ones((12,14,17,19))
# Use numpy.transpose() to reposition elements
arr1 = np.transpose(arr,(1,0,2,3)).shape
print("Reposition transposed array:\n",arr1)
# Output:
# Reposition transposed array:
# (14, 12, 17, 19)
# Reposition the elements
arr2 = np.transpose(arr,(2,3,1,0)).shape
print("Reposition the elements:\n",arr2)
# Output:
# Reposition the elements:
# (17, 19, 14, 12)
Frequently Asked Questions
You can transpose a 2D NumPy array using the numpy.transpose()
function or by using the .T
attribute of the array.
You can transpose multi-dimensional arrays in NumPy. The numpy.transpose()
function allows you to rearrange the dimensions of an array based on your requirements. You can specify the order of dimensions using the axes
parameter.
There is no significant difference in functionality between numpy.transpose()
and the .T
attribute. Both methods transpose the array in the same way. The .T
attribute is a shorthand for numpy.transpose()
, providing a more convenient way to perform transposition, especially for 2D arrays.
You can customize the transpose operation by providing the axes
parameter to numpy.transpose()
. By specifying the order of dimensions in the axes
parameter, you can achieve the desired transposition.
The transpose operation does not modify the original array. It returns a new array with the transposed elements, leaving the original array unchanged.
You can transpose a 1D NumPy array, but the operation has no effect on its shape or elements. Transposing a 1D array results in the same array.
Conclusion
In this article, I have explained the concept of the Python NumPy transpose()
function and using this function how to transpose the given array with examples. Transpose converts the row elements into column elements and the column elements into row elements.
Happy Learning!!
Related Articles
- NumPy Norm of Vector
- Get NumPy Array Length
- Get ceil values of an array
- How to use NumPy vstack() in Python
- How to Use NumPy stack() in Python
- How to Transpose Matrix in NumPy
- NumPy Inverse Matrix in Python
- How do I get the floor value of an array?
- How to get square value of an array?
- NumPy Empty Array With Examples
- How to do Matrix Multiplication in NumPy
- How to Use NumPy random seed() in Python