How to transpose() NumPy Array in Python?

  • Post author:
  • Post category:NumPy / Python
  • Post last modified:January 29, 2023
Spread the love

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 concept of the Python NumPy transpose() function and use this how to reverse the dimensions of the given array. 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.


# Below are the quick examples

# Example 1: Create a 2-D numpy array
arr = np.array([[12, 14,17, 19],[13, 16, 24, 27],[29,35,41,45]])

# Get the transpose of an array           
arr1 = np.transpose(arr)

# Example 2: Use numpy.transpose() with axis
arr1 = np.transpose(arr, (1,0))  

# Example 3: Use ndarray.T object get transpose
arr1 = arr.T 

# Example 4: 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

# Example 5: Reposition the elements
arr2 = np.transpose(arr,(2,3,1,0)).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 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

This returns an numpy array by interchanging (transpose) each row and the corresponding column. The new array is called the transpose of the given array. If we 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 Array

Reverse the dimensions of a given array using numpy.transpose(). Let’s initialize the 2-D NumPy array using numpy.array() function and run the transpose function to transform.


# Create a 2-D numpy array
arr = np.array([[12, 14,17, 19],[13, 16, 24, 27],[29,35,41,45]])

# Get the transpose of an array           
arr1 = np.transpose(arr)
print(arr1)

# Output :
# [[12 13 29]
# [14 16 35]
# [17 24 41]
# [19 27 45]]

4. Get the transpose of Array along with Axis

Alternatively, we can find the transpose of a given array using numpy.transpose() function along with the axis parameter. It will interchange each row and corresponding column and return the transposed array.


# Use numpy.transpose() with axis
arr1 = np.transpose(arr, (1,0))  
print(arr1)

# Output :
# [[12 13 29]
# [14 16 35]
# [17 24 41]
# [19 27 45]]

5. Use T Object to Transpose Array

Also, ndarray.T property is used to transpose rows and columns of the array. The property T is related to transpose() function.


# Use ndarray.T object get transpose
arr1 = arr.T
print(arr1)

# Output :
# [[12 13 29]
# [14 16 35]
# [17 24 41]
# [19 27 45]]

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(arr1)

# Output:
# (14, 12, 17, 19)

# Reposition the elements
arr2 = np.transpose(arr,(2,3,1,0)).shape
print(arr2)

# Output:
# (17, 19, 14, 12)

7. Conclusion

In this article, I have explained the concept of 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!!

References

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing How to transpose() NumPy Array in Python?