NumPy Matrix Multiplication: A Beginner's Guide
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:
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:
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:
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:
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:
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 inmatmul()
, 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:
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:
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:
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.
If you want to learn more about data science and machine learning topics, check out the article hub on Codecademy.
Author
The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.
Meet the full teamRelated articles
- Article
Introduction to Pandas and NumPy
pandas and NumPy are very useful libraries in Python. Let's learn how to use them! - Article
Setting Up a Backend
The backend is an important part of any website. It's where long-term data is stored, and where code is executed to handle interactions between your website and it's visitors. Wix has conveniently added the ability to control certain aspects of the backend with web modules.