How to do Matrix Multiplications in Numpy

Avatar

By squashlabs, Last Updated: October 2, 2023

How to do Matrix Multiplications in Numpy

Introduction to Matrix Multiplication

Matrix multiplication is a fundamental operation in linear algebra and is widely used in various fields such as computer graphics, machine learning, and scientific computing. In the context of Numpy, a powerful numerical computing library in Python, matrix multiplication is efficiently performed using the dot() and matmul() functions. In this chapter, we will explore the basics of matrix multiplication, the differences between the dot() and matmul() functions, and their practical applications.

Related Article: How To Exit Python Virtualenv

Code Snippet: Basic Matrix Multiplication

import numpy as np

# Create two matrices
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication
result = np.dot(matrix1, matrix2)

# Print the result
print(result)

Output:

[[19 22]
 [43 50]]

Code Snippet: Multiplying Two 3×3 Matrices

import numpy as np

# Create two 3x3 matrices
matrix1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
matrix2 = np.array([[9, 8, 7], [6, 5, 4], [3, 2, 1]])

# Perform matrix multiplication
result = np.dot(matrix1, matrix2)

# Print the result
print(result)

Output:

[[ 30  24  18]
 [ 84  69  54]
 [138 114  90]]

Setting Up the Numpy Environment

Before we dive into matrix multiplication in Numpy, let’s set up our environment by installing Numpy and importing the library. Numpy can be easily installed using pip, the Python package installer, by running the following command:

pip install numpy

Once Numpy is installed, we can import it into our Python script using the following line of code:

import numpy as np

With Numpy successfully installed and imported, we are now ready to perform matrix multiplication using its powerful functions.

Related Article: How to Integrate Python with MySQL for Database Queries

Code Snippet: Using the dot() Function for Matrix Multiplication

import numpy as np

# Create two matrices
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication using the dot() function
result = np.dot(matrix1, matrix2)

# Print the result
print(result)

Output:

[[19 22]
 [43 50]]

Code Snippet: Using the matmul() Function for Matrix Multiplication

import numpy as np

# Create two matrices
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])

# Perform matrix multiplication using the matmul() function
result = np.matmul(matrix1, matrix2)

# Print the result
print(result)

Output:

[[19 22]
 [43 50]]

(Note: The code snippets in this article assume that Numpy has been properly installed and imported in the Python environment.)

More Articles from the Python Tutorial: From Basics to Advanced Concepts series:

16 Amazing Python Libraries You Can Use Now

In this article, we will introduce you to 16 amazing Python libraries that are widely used by top software teams. These libraries are powerful tools that can enhance... read more

Database Query Optimization in Django: Boosting Performance for Your Web Apps

Optimizing database queries in Django is essential for boosting the performance of your web applications. This article explores best practices and strategies for... read more

Converting Integer Scalar Arrays To Scalar Index In Python

Convert integer scalar arrays to scalar index in Python to avoid the 'TypeError: Only integer scalar arrays can be converted to a scalar index with 1D' error. This... read more

How To Convert A Tensor To Numpy Array In Tensorflow

Tensorflow is a powerful framework for building and training machine learning models. In this article, we will guide you on how to convert a tensor to a numpy array... read more

How to Normalize a Numpy Array to a Unit Vector in Python

Normalizing a Numpy array to a unit vector in Python can be done using two methods: l2 norm and max norm. These methods provide a way to ensure that the array has a... read more

How to Adjust Font Size in a Matplotlib Plot

Adjusting font size in Matplotlib plots is a common requirement when creating visualizations in Python. This article provides two methods for adjusting font size: using... read more