# NumPy Matrix Multiplication: A Beginner's Guide

Learn how to perform matrix multiplication in NumPy using dot(), matmul(), and multiply() functions. A beginner-friendly guide with examples.

## Getting Started with NumPy and Matrix Operations

Before we dive into NumPy matrix multiplication techniques, it’s essential to understand the basics of matrix operations and why NumPy is the preferred library for such computations.

NumPy is a powerful scientific computing library in Python that is popular in data science and machine learning. It provides a comprehensive set of functions and data structures specifically designed for working with numerical data. One of its core strengths lies in its handling of large datasets and performing complex numerical computations efficiently.

Using NumPy is also beneficial in terms of CPU and memory usage. It can apply an operation to every element in an array simultaneously. This reduces computational overhead as this method is significantly faster than applying it to one element at a time.

Additionally, since NumPy arrays store elements of the same data type, it allows NumPy to store arrays compactly, i.e., allocate a fixed amount of memory for each element, bypassing the overhead of storing type information for every value and minimizing memory usage. This combination makes NumPy the preferred choice for handling large, multi-dimensional arrays and matrices seamlessly.

In this guide, we will navigate through the various functions that enable us to perform matrix multiplication in NumPy. These functions include:

• dot()
• matmul()
• multiply()

## Understanding NumPy Matrices for Multiplication

In Python, a matrix is represented as a 2D array. Since matrices are always two-dimensional, unlike a regular array, we can use NumPy to easily perform matrix-specific operations without getting confused about the different dimensions of the matrix.

Here is an example of a matrix:

$\left[ \begin{matrix} 1 & 0\\ 0 & 1 \end{matrix} \right]$

In NumPy, we define a matrix using the matrix() function, which takes a two-dimensional list as its input and returns a matrix object:

# Import the NumPy libraryimport numpy as np
# Define a NumPy matrixnewMatrix = np.matrix([[12, 24], [36, 48]])
# Print the matrixprint(newMatrix)


The above code produces the following output:

$\left[ \begin{matrix} 12 & 24\\ 36 & 48 \end{matrix} \right]$

Having a good understanding of the structure and properties of a NumPy matrix will help us carry out complex matrix operations with ease.

In the next section, we will check out the different NumPy functions that we can use to perform matrix multiplication.

## Essential NumPy Functions for Matrix Multiplication

Matrix multiplication is a key operation in various cases, including solving linear equations, transforming data, and performing complex calculations in machine learning models.

In this section, we will discuss the various functions to perform matrix multiplication in NumPy.

### How to Use NumPy’s dot() Function for Matrix Multiplication?

In NumPy, the dot() function calculates the dot product of two vectors. However, when both are 2D arrays, it treats them as matrices and calculates the dot product of them:

$\left[ \begin{matrix} 1 & 2\\ 3 & 4 \end{matrix} \right]\times \left[ \begin{matrix} 5 & 6\\ 7 & 8 \end{matrix} \right]= \left[ \begin{matrix} (1\times5)+(2\times7) & (1\times6)+(2\times8)\\ (3\times5)+(4\times7) & (3\times6)+(4\times8) \end{matrix} \right]$

The following example uses the dot() function to compute the dot product of two matrices in NumPy:

# Import the NumPy libraryimport numpy as np
# Define two NumPy matricesmat1 = np.matrix([[1, 2], [3, 4]])mat2 = np.matrix([[5, 6], [7, 8]])
# Using the dot() functiondot = np.dot(mat1, mat2)
# Print the matrixprint(dot)


Below is the output for the above code:

$\left[ \begin{matrix} 19 & 22\\ 43 & 50 \end{matrix} \right]$

Let’s move on to the next function, i.e., matmul().

### What is the Difference Between dot() and matmul() Function in NumPy?

We can also use the matmul() function for NumPy matrix multiplication. It calculates the matrix product of two matrices:

$\left[ \begin{matrix} 1 & 2\\ 3 & 4 \end{matrix} \right]\times \left[ \begin{matrix} 5 & 6\\ 7 & 8 \end{matrix} \right]= \left[ \begin{matrix} (1\times5)+(2\times7) & (1\times6)+(2\times8)\\ (3\times5)+(4\times7) & (3\times6)+(4\times8) \end{matrix} \right]$

The differences between dot() and matmul() function in NumPy are following:

• dot() calculates the dot product of two vectors. However, when both are 2D arrays, it takes them as matrices and computes the dot product of them. On the other hand, matmul() calculates the matrix product of two matrices.
• In dot(), the input values can be scalar, whereas in matmul(), the input values need to be matrices.

Here is an example that uses the matmul() function to multiply two NumPy matrices:

# Import the NumPy libraryimport numpy as np
# Define two NumPy matricesmat1 = np.matrix([[1, 2], [3, 4]])mat2 = np.matrix([[5, 6], [7, 8]])
# Using the matmul() functionmatmul = np.matmul(mat1, mat2)
# Print the matrixprint(matmul)


The output is following:

$\left[ \begin{matrix} 19 & 22\\ 43 & 50 \end{matrix} \right]$

Next, let’s discuss the multiply() function in NumPy.

### When to Use NumPy’s multiply() for Element-wise Matrix Operations?

The multiply() function in NumPy performs element-wise matrix multiplication of two matrices:

$\left[ \begin{matrix} 1 & 2\\ 3 & 4 \end{matrix} \right]\times \left[ \begin{matrix} 5 & 6\\ 7 & 8 \end{matrix} \right]= \left[ \begin{matrix} 1\times5 & 2\times6\\ 3\times7 & 4\times8 \end{matrix} \right]$

Let’s see an example using the multiply() function for NumPy matrix multiplication:

# Import the NumPy libraryimport numpy as np
# Define two NumPy matricesmat1 = np.matrix([[1, 2], [3, 4]])mat2 = np.matrix([[5, 6], [7, 8]])
# Using the multiply() functionmultiply = np.multiply(mat1, mat2)
# Print the matrixprint(multiply)


The above code results in the following output:

$\left[ \begin{matrix} 5 & 12\\ 21 & 32 \end{matrix} \right]$

This is how we can use the multiply() function to multiply matrices in NumPy.

## Conclusion

Throughout this tutorial, we discussed some important topics:

• What a matrix is and how matrix multiplication helps
• Defining a NumPy matrix
• The various NumPy functions for performing matrix multiplication

Understanding these functions will enable us to optimize matrix calculations with NumPy functions.