Welcome to the lesson on matrix multiplication! By this point in the course, you’ve already learned about vector operations and basic matrix arithmetic using NumPy. Matrix multiplication is a foundational operation in linear algebra that’s widely applicable in fields like computer graphics, machine learning, and engineering. The purpose of this lesson is to demonstrate how matrix multiplication can be efficiently performed in Python using the NumPy library, continuing our journey into more advanced numerical computations.
Matrix multiplication involves combining two matrices to produce a third matrix — called the product matrix — where each element is the result of taking the dot product of the corresponding row of the first matrix with the column of the second matrix. Let’s see how NumPy handles this operation for you seamlessly.
Let’s walk through the process of performing matrix multiplication with NumPy.
-
Define the Matrices:
For multiplication, ensure matrices are compatible in size. Create two 2x2 matrices usingnp.array
:Python1import numpy as np 2 3matrix_a = np.array([[1, 2], [3, 4]]) 4matrix_b = np.array([[2, 0], [1, 2]])
Each matrix should have the same number of columns in the first matrix as rows in the second matrix to be compatible for multiplication. That is, an
m x n
matrix multiplied by ann x p
matrix results in anm x p
matrix. Ifn
is not equal, matrix multiplication cannot be performed. -
Perform the Matrix Multiplication:
Use thenp.matmul
function to multiply the matrices, or alternatively, use the@
operator for the same operation:Python1product = np.matmul(matrix_a, matrix_b) 2 3# or using the @ operator 4product_alt = matrix_a @ matrix_b
Here,
np.matmul
function and the@
operator both compute the product ofmatrix_a
andmatrix_b
, which involves taking the dot product of rows frommatrix_a
with columns frommatrix_b
. -
Output the Results:
Let's print the matrices and their product:Python1print("Matrix A:\n", matrix_a) 2print("Matrix B:\n", matrix_b) 3print("Matrix Product (A * B):\n", product) 4 5# Output: 6# Matrix A: 7# [[1 2] 8# [3 4]] 9# Matrix B: 10# [[2 0] 11# [1 2]] 12# Matrix Product (A * B): 13# [[ 4 4] 14# [10 8]]
The output shows
Matrix A
,Matrix B
, and their resulting product matrix. Each element in the product matrix results from the operation we discussed earlier — combining rows and columns through dot products.
In this lesson, we explored matrix multiplication using NumPy, a critical skill for carrying out more complex matrix operations effortlessly. You’ve learned to define matrices, use the np.matmul
function, and print the result matrix to verify its correctness. This method leverages NumPy’s ability to handle complex computations efficiently.
Reflect on how this lesson builds upon previous matrix operations such as addition and subtraction, and prepare to dive into practice exercises where you'll have the opportunity to solidify these concepts. As you complete the exercises, you'll gain confidence in applying these skills in diverse applications, enhancing your proficiency in numerical computations with NumPy. Congratulations on adding this essential skill to your toolkit!