NumPy transpose() Function:
The transpose() function of the NumPy module reverses or permutes the axes of an array and returns the changed array. The function returns matrix transpose for a 2-D array.
One of the most significant functions in matrix multiplication is numpy.transpose().
It converts row items to column elements and column elements back to row elements. This function returns a modified array of the original one.
Syntax:
numpy.transpose(array, axes=None)
Parameters
array: This is required. It is the input array to be transposed.
axes: This is optional. It is a tuple or list containing a permutation of [0,1,…, N-1], where N is the number of axes of a. The ith axis of the returned array will correspond to the input’s axis numbered axes[i]. If not given, range(a.ndim)[::-1] is used, which reverses the order of the axes.
Return Value:
An ndarray is returned. The source array’s axis is permuted in the output array. When possible, a view is returned.
- Python: numpy.ravel() function Tutorial with examples
- Python Programming – NumPy
- Program for Transpose a Matrix in Python & C++ Programming
NumPy transpose() Function in Python
Example1
Approach:
- Import numpy module using the import keyword.
- Pass some random list as an argument to the array() function of the numpy module to create an array.
- Store it in a variable.
- Print the above-given array.
- Pass the given array as an argument to the transpose()function to transpose the given array(interchange rows& columns).
- Store it in another variable.
- Print the above transposed Array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Pass some random list as an argument to the array() function # of the numpy module to create an array. # Store it in a variable. gvn_arry = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Print the above given array. print("The above given array is:") print(gvn_arry) # Pass the given array as an argument to the transpose()function to # transpose the given array(interchange rows& columns) # Store it in another variable. trnspos_arry = np.transpose(gvn_arry) # Print the above transposed Array print("The above transposed Array is:") print(trnspos_arry)
Output:
The above given array is: [[1 2 3] [4 5 6] [7 8 9]] The above transposed Array is: [[1 4 7] [2 5 8] [3 6 9]]
Example2
Approach:
- Import numpy module using the import keyword
Create an array using the ones() function by passing the 3-Dimensional
shape as an argument to it. - Store it in a variable.
- Pass the given array and tuple of permutations(axis parameter) as
arguments to transpose function and store it in a variable - Print the permuted Array.
- Print the shape of the above obtained permuted array using the shape attribute.
- The Exit of the Program.
Below is the implementation:
# Import numpy module using the import keyword import numpy as np # Create an array using the ones() function by passing the 3-Dimensional # shape as an argument to it. # Store it in a variable. gvn_arry = np.ones((2, 6, 3)) # Pass the given array and tuple of permutations(axis parameter) as # arguments to transpose function and store it in a variable permutd_arry = np.transpose(gvn_arry, (2, 0, 1)) # Print the permuted Array. print("The permuted Array is:\n", permutd_arry) print() # Print the shape of the above obtained permuted array using the shape attribute print("The shape of the above obtained permuted array:") print(permutd_arry.shape)
Output:
The permuted Array is: [[[1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1.]] [[1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1.]] [[1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1.]]] The shape of the above obtained permuted array: (3, 2, 6)