Built with Tutify.io

    AIStudyLabs

    How to Transpose Matrix in NumPy

    How to Transpose Matrix in NumPy

    You can use either numpy.matrix.transpose() or numpy.transpose() function to get the permute or reserve the dimension of the input matrix. The transpose of a matrix is obtained by moving the columns data to the rows and rows data to the column.  These transpose() functions are mainly used to transpose the 2-dimension arrays. This does not show any effect on the one-D array, When you try transposing a 1-D array, it returns an unmodified view of the original array.

    In this article, I will explain the concept of the Python NumPy matrix.transpose() function and use this how to reverse the dimensions of the given matrix. If you want to transpose an array refer NumPy transpose() function.

    <a id="h-1-quick-examples-of-numpy-transpose-matrix"></a>

    1. Quick Examples of NumPy Transpose Matrix

    If you are in a hurry, below are some quick examples of how to transpose the NumPy matrix.

    
    # Quick examples of numpy transpose matrix
    
    # Example 1: Use matrix.transpose() method
    # Get the transpose of matrix
    arr = np.matrix('[4, 8; 1, 12]')
    arr2 = arr.transpose()
    
    # Example 2:  Use numpy.transpose() function 
    # Get the transpose of array
    arr = np.array
    arr2 = arr.transpose([[1, 2, 4, 3],[1, 3, 5, 6]])
    
    # Example 3: Use numpy.mutiply() function 
    arr2 = np.multiply(arr, arr1)
    

    <a id="h-2-syntax-of-numpy-matrix-transpose"></a>

    2. Syntax of NumPy matrix.transpose()

    Following is the syntax of matrix.transpose() function

    
    # Syntax of numpy.matrix.transpose() 
    matrix.transpose(a, axes)
    

    <a id="h-2-1-parameters-of-numpy-matrix-transpose"></a>

    2.1 Parameters of NumPy matrix.transpose()

    It takes two parameters

    <a id="h-2-2-return-value"></a>

    2.2 Return Value

    It returns a view of the array with axes transposed, the resultant array will have transposed array shape.

    <a id="h-3-usage-of-numpy-matrix-transpose"></a>

    3. Usage of NumPy matrix.transpose()

    This matrix.transpose() returns a NumPy array by interchanging (transposing) each row and the corresponding column. The new array is called the transpose of the given matrix. If you have a matrix of shape (X, Y) then the transpose of the matrix will have the shape(Y, X).

    The transpose of the matrix is obtained using the transpose() method in NumPy. For instance, arr is the original matrix created using np.matrix('[4, 8; 1, 12]'). arr.transpose() is used to obtain the transpose of the matrix. The resulting transposed matrix is stored in the variable arr2. Finally, the transposed matrix is printed.

    
    # Import numpy
    import numpy as np
    
    # Create matrix with numpy
    arr = np.matrix('[4, 8; 1, 12]')
    print("Original matrix:\n", arr)
    
    # Get the transpose of matrix
    arr2 = arr.transpose()
    print("Transposed Matrix:\n",arr2)
    

    Yields below output.

    <a id="h-4-use-numpy-transpose-function"></a>

    4. Use NumPy transpose() Function

    Alternatively, you can reverse the dimensions of a given array using numpy.transpose(). Let’s create NumPy array using numpy.array() function and run the transpose function to transform.

    Using the np.transpose() function with a NumPy array. This code creates a 2x4 array, prints the original array, then uses np.transpose() to obtain its transpose. Finally, it prints the transposed matrix. As mentioned earlier, the np.transpose() function reverses the dimensions of the array, effectively transposing the rows and columns.

    
    # Create a numpy array
    arr = np.array([[1, 2, 4, 3],[1, 3, 5, 6]])
    print("Original array:\n", arr)
    
    # Use numpy.transpose() function 
    arr2 = arr.transpose()
    print("Transposed Matrix:\n",arr2)
    
    # Output :
    # Original array:
    #  [[1 2 4 3]
    #  [1 3 5 6]]
    # Transposed Matrix:
    #  [[1 1]
    #  [2 3]
    #  [4 5]
    #  [3 6]]
    

    <a id="h-5-use-numpy-multiply-to-matrix-multiplication"></a>

    5. Use NumPy.multiply() to Matrix Multiplication

    The np.multiply() function in NumPy performs element-wise multiplication, not matrix multiplication. If you want to perform element-wise multiplication of two arrays. However, if you want to perform matrix multiplication, you should use np.dot(), @ operator, or np.matmul().

    
    import numpy as np
     
    # Create a numpy two dimensional arrays
    arr = np.array([[1, 2, 4, 3],[1, 3, 5, 6]])
    arr1 = np.array([[2, 3, 6, 5],[4, 6, 2, 1]])
                     
    # Use numpy.mutiply() function 
    arr2 = np.multiply(arr, arr1)
    print("Matrix Multiplication:\n",arr2)
    

    Yields below output.

    
    # Output:
    Matrix Multiplication:
     [[ 2  6 24 15]
     [ 4 18 10  6]]
    

    <a id="h-frequently-asked-questions"></a>

    Frequently Asked Questions

    Transposing a matrix is an operation in linear algebra that involves flipping the matrix over its diagonal. This operation switches the row and column indices of the matrix. If you have a matrix A with dimensions m x n (m rows and n columns), the transpose of A, denoted as A^T, will have dimensions n x m (n rows and m columns).

    You can use the np.transpose() function or the .T attribute of a NumPy array to transpose a matrix.

    Matrix transposition is closely related to matrix multiplication, particularly in the context of the dot product. When dealing with matrix multiplication, the rule is that for two matrices A and B to be multiplied (AB), the number of columns in A must be equal to the number of rows in B. If A is of shape (m x n) and B is of shape (n x p), then the resulting matrix C (AB) will be of shape (m x p).

    You can use the .T attribute to transpose a matrix in NumPy. The .T attribute is a convenient shorthand for the np.transpose() function and provides a more concise way to obtain the transposed matrix.

    You can absolutely transpose a non-square matrix in NumPy. The transposition operation is defined for matrices of any shape, not just square matrices.

    When you transpose a 1D array in NumPy, the result will be the same as the original array. The transposition of a 1D array doesn't change its shape.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to transpose the matrix using the matrix.transpose() function with examples.

    Happy Learning!!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References