How to do Matrix Multiplications in Numpy

Avatar

By squashlabs, Last Updated: Oct. 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 Add a Gitignore File for Python Projects

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 3x3 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 Manually Raise An Exception In Python

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:

How to Convert String to Bytes in Python 3

Learn how to convert a string to bytes in Python 3 using simple code examples. Discover how to use the encode() method and the bytes() function effec… read more

How To Iterate Over Rows In Pandas Dataframe

Data analysis is a fundamental part of many projects, and pandas is a powerful library in Python that makes working with data incredibly efficient. W… read more

How To Filter Dataframe Rows Based On Column Values

Learn how to select rows from a dataframe based on their column values using Python's pandas library. Explore two methods, Boolean Indexing and the Q… read more

Optimizing FastAPI Applications: Modular Design, Logging, and Testing

Learn best practices in FastAPI for designing modular applications, logging, and testing. This article dives into the key aspects of optimizing FastA… read more

How to Replace Strings in Python using re.sub

Learn how to work with Python's re.sub function for string substitution. This article covers practical use-cases, syntax, and best practices for text… read more

How to Create Multiline Comments in Python

Creating multiline comments in Python can be a simple and way to add explanatory notes to your code. There are different methods you can use, such as… read more

How to Export a Python Data Frame to SQL Files

This article provides a step-by-step guide to exporting Python data frames to SQL files. It covers everything from installing the necessary libraries… read more

How To Concatenate Lists In Python

Merging lists in Python is a common task that every programmer needs to know. This article provides simple examples and explanations on how to concat… read more

How to Use Assert in Python: Explained

The assert statement in Python is a powerful tool for validating the correctness of your code. This article explains its syntax and provides examples… read more

How to Extract Unique Values from a List in Python

Retrieving unique values from a list in Python is a common task for many programmers. This article provides a simple guide on how to accomplish this … read more