• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:15 mins read
You are currently viewing 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.

Advertisements
  • 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

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


# Quick examples of matrix multiplication

# Example 1: Use numpy.mutiply() function 
# Get the matrix multiplication
arr = np.array([[2, 4, 3, 1],[2, 3, 6, 1]])
arr1 = np.array([[2, 1, 5, 2],[4, 8, 3, 2]])
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
arr = np.array([[1, 3],[4, 1]])
arr1 = np.array([[1, 2],[2, 5]])
arr2 = np.dot(arr,arr1)

# Example 5: Use numpy.matmul() function
# Get the product 
arr = np.array([[1, 3],[4, 1]])
arr1 = np.array([[1, 2],[2, 5]])
arr2 = np.matmul(arr,arr1)

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

Let’s Create NumPy arrays and use these to perform element-wise multiplication using NumPy.multiply() method. This multiplies 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.

In the below example, np.multiply(arr, arr1) performs element-wise multiplication, meaning it multiplies corresponding elements from arr and arr1. The resulting arr2 will contain the element-wise multiplication of the input arrays.


# Import numpy 
import numpy as np

# Create a numpy two dimensional arrays
arr = np.array([[2, 4, 3, 1],[2, 3, 6, 1]])
print("First array:\n", arr)
arr1 = np.array([[2, 1, 5, 2],[4, 8, 3, 2]])
print("Second array:\n", arr1)

# Use numpy.mutiply() function
# Get the matrix multiplication
arr2 = np.multiply(arr, arr1)
print("After getting the matrix multiplication:\n",arr2)

Yields below output.

NumPy Matrix Multiplication

To pass certain rows, columns, or submatrices to the numpy.multiply() method and get the multiplication of The certain rows, columns, and submatrices. You 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("After getting the certain rows multiplication:\n",arr2)

# Output :
# After getting the certain rows multiplication:
#  [ 8 32]

# Get the certain columns multiplication
arr3 = np.multiply(arr[ 1,: 3], arr1[ 0,: 3])
print("After getting the certain columns multiplication:\n",arr3)

# Output :
# After getting the certain columns multiplication:
#  [ 4  3 30]

3. Use NumPy.dot() for Scalar Multiplication

A simple form of matrix multiplication is scalar multiplication, you can do that by using the NumPy dot() function. In scalar multiplication, you 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.


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

# Get dot product of arrays
arr2 = np.dot(arr,arr1)
print("After getting the dot product of arrays:\n",arr2)
                 
# Output:
# After getting the dot product of arrays:
#  [[2 6]
#  [8 2]]

You can multiply a 2-dimensional matrix by another 2-dimensional matrix using np.dot(). When you 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 a 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 dot product of two arrays
arr2 = np.dot(arr,arr1)
print("After getting the dot product of two arrays:\n",arr2)
                 
# Output:
# After getting the dot product of two arrays:
#  [[ 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 arr and arr1 as arguments and returns the matrix multiplication of the input NumPy arrays. A scalar is produced only when both arr and arr1 are 1-dimensional vectors.


# Create numpy arrays
arr = np.array([[1, 3],[4, 1]])
arr1 = np.array([[1, 2],[2, 5]])
                 
# Use numpy.matmul() function
# Get the product 
arr2 = np.matmul(arr,arr1)
print("After getting the multiplication of two numpy arrays:\n",arr2)
              
# Output:
# After getting the multiplication of two numpy arrays:
#  [[ 7 17]
#  [ 6 13]]

Frequently Asked Questions

What is matrix multiplication in NumPy?

Matrix multiplication in NumPy refers to the process of multiplying two matrices to produce a new matrix. It follows specific rules, where the number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication to be valid.

What is element-wise multiplication in NumPy?

Element-wise multiplication in NumPy involves multiplying the corresponding elements of two matrices or arrays. It is performed using the * operator or the numpy.multiply() function.

How can I multiply specific rows, columns, or submatrices in NumPy?

You can multiply specific rows, columns, or submatrices in NumPy by selecting the desired elements using array slicing or indexing, and then applying matrix multiplication or element-wise multiplication accordingly.

What is the difference between numpy.dot() and element-wise multiplication?

numpy.dot() performs matrix multiplication, following the rules of matrix algebra, where corresponding elements are multiplied and summed. Element-wise multiplication, on the other hand, multiplies the corresponding elements of two matrices without summing them.

Can I multiply matrices of different shapes in NumPy?

You cannot multiply matrices of arbitrary shapes in NumPy. For matrix multiplication to be valid, the number of columns in the first matrix must be equal to the number of rows in the second matrix. In mathematical terms, if you have a matrix A with dimensions (m x n) and a matrix B with dimensions (p x q), the number of columns in A (n) must be equal to the number of rows in B (p) in order to perform matrix multiplication (A × B).

How can I perform matrix multiplication in NumPy?

To perform matrix multiplication in NumPy, you can use the numpy.dot() function or the @ operator (introduced in Python 3.5) for matrix multiplication.

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!!

References