Quick Contact

    Python Tutorial
    Python Panda Tutorial
    Python Selenium
    Python Flask Tutorial
    Python Django
    Numpy
    Tensorflow
    Interview Questions & Answers

    Matrix Multiplication in NumPy

    Matrix Multiplication in NumPy is a python library used for scientific computing. Using this library, we can perform complex matrix operations like multiplication, dot product, multiplicative inverse, etc. in a single step. In this post, we will be learning about different types of matrix multiplication in the numpy library.

    Different Types of Matrix Multiplication

    There are primarily three different types of matrix multiplication:

    Function Description
    np.matmul(array a, array b) Returns matrix product of two given arrays
    np.multiply(array a, array b) Returns element-wise multiplication of two given arrays
    np.dot(array a, array b) Returns scalar or dot product of two given arrays
    1. Matrix product of two given arrays

      In order to find the matrix product of two given arrays, we can use the following function :

      np.matmul(array a, array b)

      Example #1

      Program to illustrate the matrix product of two given n-d arrays.

      Code:
      import numpy as np
      A = np.array([[1,2,3], [4,5,6]])
      B = np.array([[1,1,1], [0,1,0], [1,1,1]])
      print("Matrix A is:\n",A)
      print("Matrix A is:\n",B)
      C = np.matmul(A,B)
      print("Matrix multiplication of matrix A and B is:\n",C)
      
    2. The matrix product of the given arrays is calculated in the following ways:

    3. Element wise multiplication of two given arrays

      In order to find the element-wise product of two given arrays, we can use the following function.

      np.multiply(array a, array b)

      Example #2

      Program to illustrate element-wise multiplication of two given matrices

      Code:
      import numpy as np
      A = np.array([[1,2,3], [4,5,6]])
      B = np.array([[1,2,3], [4,5,6]])
      print("Matrix A is:\n",A)
      print("Matrix A is:\n",B)
      C = np.multiply(A,B)
      print("Matrix multiplication of matrix A and B is:\n",C)
      
    4. Scalar or Dot product of two given arrays

      The dot product of any two given matrices is basically their matrix product. The only difference is that in dot product we can have scalar values as well.

      Example #3

      A program to illustrate dot product of two given 1-D matrices

      Code:
      import numpy as np
      A = np.array([1,2,3])
      B = np.array([4,5,6])
      print("Matrix A is:\n",A)
      print("Matrix A is:\n",B)
      C = np.dot(A,B)
      print("Matrix multiplication of matrix A and B is:\n",C)
      
      Example #4

      A program to illustrate dot product of two given 2-D matrices

      Code:
      import numpy as np
      A = np.array([[1,2],[2,1]])
      B = np.array([[4,5],[4,5]])
      print("Matrix A is:\n",A)
      print("Matrix A is:\n",B)
      C = np.dot(A,B)
      print("Matrix multiplication of matrix A and B is:\n",C)
      
      Example #5

      A program to illustrate the dot product of a scalar value and a 2-D matrix

      Code:
      A = np.array([[1,1],[1,1]])
      print("Matrix A is:\n",A)
      C = np.dot(2,A)
      print("Matrix multiplication of matrix A and B is:\n",C)
      


    Copyright 1999- Ducat Creative, All rights reserved.

    Anda bisa mendapatkan server slot online resmi dan terpercaya tentu saja di sini. Sebagai salah satu provider yang menyediakan banyak pilihan permainan.