How to do Matrix Multiplication in NumPy

NumPy matrix multiplication is a mathematical operation that accepts two matrices and gives a single matrix by multiplying rows of the first matrix to the column of the second matrix. To multiply two matrices NumPy provides three different functions.

  • numpy.multiply(arr1, arr2) – Element-wise matrix multiplication of two arrays
  • numpy.matmul(arr1, arr2) – Matrix product of two arrays
  • numpy.dot(arr1, arr2) – Scalar or dot product of two arrays

While doing matrix multiplication in NumPy make sure that the number of columns of the first matrix should be equal to the number of rows of the second matrix.

1. Quick Examples of Matrix Multiplication in NumPy

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


# Below are the quick examples

# Example 1: Use numpy.mutiply() function and 
# get the matrix multiplication
arr2 = np.multiply(arr, arr1)

# Example 2: Get the certain rows multiplication
arr2 = np.multiply(arr[ 0,: 2], arr1[ 1,: 2])

# Example 3: Get dot product of arrays
arr = np.array([[1, 3 ],
                [4, 1 ]])
arr1 = 2
arr2 = np.dot(arr,arr1)

# Example 4: Use numpy.dot() function
# Get the product of two arrays
arr2 = np.dot(arr,arr1)

# Example 5: # Use numpy.matmul() function
# Get the product 
arr2 = np.matmul(arr,arr1)

2. Use NumPy.multiply() Get Element-Wise Matrix Multiplication

Let’s Create NumPy arrays and use these to perform element-wise multiplication using NumPy.multiply() method. This Multiples every element of the first matrix by the equivalent element in the second matrix using element-wise multiplication, or Hadamard Product. Make sure that the dimensions of both matrices have the same in order to multiply.


import numpy as np
# Create a numpy two dimensional arrays
arr = np.array([[2, 4, 3, 1],[2, 3, 6, 1]])
arr1 = np.array([[2, 1, 5, 2],[4, 8, 3, 2]])
                 
# Use numpy.mutiply() function and 
# get the matrix multiplication
arr2 = np.multiply(arr, arr1)
print(arr2)

# Output :
# [[ 4  4 15  2]
# [ 8 24 18  2]]

To pass certain rows, columns, or submatrices to the numpy.multiply() method and get the multiplication of The certain rows, columns and submatrices. We should follow the same Sizes of the rows, columns, or submatrices that we pass as our operands. Let’s take an example,


# Get the certain rows multiplication
arr2 = np.multiply(arr[ 0,: 2], arr1[ 1,: 2])
print(arr2)

# Output :
# [ 5 12]

arr3 = np.multiply(arr[ 1,: 3], arr1[ 0,: 3])
print(arr3)

# Output :
# [ 2  8 18]

3. Use NumPy.dot() for Scalar Multiplication.

A simple form of matrix multiplication is scalar multiplication, we can do that by using the NumPy dot() function. In scalar multiplication, we can multiply a scalar by a matrix or multiply a matrix by a scalar. Every element in the matrix is multiplied by the scalar, which returns the same shape array as the original array.

When performing scalar multiplication, the order doesn’t matter. This returns the same output if we multiply the scalar by the matrix or the matrix by the scalar.


# Get dot product of arrays
arr = np.array([[1, 3 ],
                [4, 1 ]])
arr1 = 2
arr2 = np.dot(arr,arr1)
print(arr2)

# Output :
# [[2 6]
# [8 2]]

We can multiply a 2-dimensional matrix by another 2-dimensional matrix using np.dot(). when we multiply two matrices it should follow the order i.e matrix X multiplied by matrix Y is not the same as matrix Y multiplied by matrix X. Let’s create an image for better understanding.

NumPy Matrix Multiplication
numpy.dot()

# Create numpy arrays
arr = np.array([[1, 3],
                [4, 1]])

arr1 = np.array([[1, 2],
                 [2, 5]])
# Use numpy.dot() function
# Get the product of two arrays
arr2 = np.dot(arr,arr1)
print(arr2)

# Output :
# [[ 7 17]
# [ 6 13]]

4. Use matmul() – Multiplication of Two NumPy Arrays

The np.matmul() method is used to find out the matrix product of two arrays. The matmul() function takes arr1 and arr2 as arguments and returns the matrix multiplication of the input NumPy arrays. A scalar is produced only when both arr1 and arr2 are 1-dimensional vectors.


# Use numpy.matmul() function
# Get the product 
arr2 = np.matmul(arr,arr1)
print(arr2)

# Output :
# [[ 7 17]
# [ 6 13]]

5. Conclusion

In this article, I have explained the concept of Python NumPy matrix multiplication and how to use it by using numpy.multiply(), numpy.matmul() and numpy.dot() function with examples.

Happy Learning!!

You May Also Like

References

numpy matrix multiplication

Leave a Reply

You are currently viewing How to do Matrix Multiplication in NumPy