NumPy Matrix Multiplication - NumPy v1.24 Manual

Posted in /  

NumPy Matrix Multiplication - NumPy v1.24 Manual
vinaykhatri

Vinay Khatri
Last updated on November 23, 2024

    In Python, we have a very powerful third-party library, called NumPy, which stands for Numerical Python. In general, Python does not have support for standard array data structure like what we have in Java and C++. So, without a proper array, we cannot form a Matrix on which we can perform arithmetic operations. To overcome this issue, we use the NumPy library. In this article, we will discuss how we can use the NumPy library to perform Matrix Multiplication in Python .

    Why We Need the NumPy Library for Matrix Multiplication?

    In Python, we have the list data structure that acts like an array and we also have an inbuilt array module. However, both are not sufficient for realizing the mathematical matrix concept. As we know that matrices and arrays are the most powerful and important data structures required to build data science models, we just cannot ignore them in Python. That is why Python has introduced the NumPy library to ensure full support for the arrays similar to other high-level programming languages.

    NumPy is an open-source Python package , which is mostly used for data science because of its built-in support for many mathematical tools. It can also work along with SciPy and Matplotlib libraries to write powerful algorithms for data science models.

    Numpy dot() Matrix Multiplication

    As NumPy is known for supporting various mathematical tools, so to perform matrix multiplication, we do not need to write an algorithm. NumPy provides an inbuilt dot() method to multiply two matrices. NumPy Matrix Multiplication

    You can also read in details about Numpy Dot() Matrix Multiplications with this blog post .

    Example:

    import numpy as np
    
    Matrix_A = np.array( [[1,2,3],
                         [4,5,7],
                         [8,9,10]])
    
    Matrix_B = np.array([[11,12,13],
                         [14,15,16],
                         [17,18,19]])
    
    print("Matrix_A * Matrix_B is :")
    print(Matrix_A.dot(Matrix_B))
    
    print("\nMatrix_B * Matrix_A is: ")
    print(Matrix_B.dot(Matrix_A))

    Output:

    Matrix_A * Matrix_B is :
    [[ 90  96 102]
    [233 249 265]
    [384 411 438]]
    
    Matrix_B * Matrix_A is:
    [[163 199 247]
    [202 247 307]
    [241 295 367]]

    Matrix Multiplication using For Loop

    import numpy as np
    
    Matrix_A = np.array( [[1,2,3],
                         [4,5,7],
                        [8,9,10]])
    
    Matrix_B = np.array([[11,12,13],
                        [14,15,16],
                        [17,18,19]])
    
    out_1 = [[0 for c_1 in range(len(Matrix_A)) ] for r_1 in range(len(Matrix_B))]
    out_2 = [[0 for c_2 in range(len(Matrix_A)) ] for r_2 in range(len(Matrix_B))]
    
    print("Matrix_A * Matrix_B is :")
    for i in range(len(Matrix_A)):
        for j in range(len(Matrix_B[0])):
            for k in range(len(Matrix_B)):
                 out_1[i][j] += Matrix_A[i][k]*Matrix_B[k][j]
    
    print(np.array(out_1))
    
    print("\nMatrix_B * Matrix_A is: ")
    for i in range(len(Matrix_B)):
        for j in range(len(Matrix_A[0])):
            for k in range(len(Matrix_A)):
                out_2[i][j] += Matrix_B[i][k]*Matrix_A[k][j]
    
    print(np.array(out_2))

    Output:

    Matrix_A * Matrix_B is :
    [[ 90  96 102]
     [233 249 265]
     [384 411 438]]
    
    Matrix_B * Matrix_A is:
    [[163 199 247]
     [202 247 307]
     [241 295 367]]

    To Sum it Up

    We hope that this tutorial helped you understand how you can use the NumPy library to perform matrix manipulation in Python. If you have any issues or queries, feel free to share them with us in the comments section below.

    People are also reading:

    Leave a Comment on this Post

    0 Comments