Matrix Multiplication and Dot Product
Learn how to perform matrix multiplication and vector dot products using NumPy. Understand how rows and columns interact, how matrix dimensions determine whether multiplication is possible, and how to use the @ operator, np.dot(), and np.matmul() with practical numerical examples and student data analysis.
1. Learning Objectives
By the end of this lesson, you will be able to:
- Understand the mathematical meaning of a vector dot product.
- Perform matrix multiplication using NumPy arrays.
- Use the @ operator for matrix and vector multiplication.
- Perform multiplication using np.dot() and np.matmul().
- Understand the matrix dimension compatibility rule.
- Differentiate element-wise multiplication from matrix multiplication.
- Multiply matrices by vectors and scalar values.
- Apply matrix multiplication to practical numerical problems.
2. Introduction to Matrix Multiplication
Matrix multiplication is a mathematical operation in which rows of the first matrix are combined with columns of the second matrix. Each output element is calculated by multiplying corresponding values and adding their products.
NumPy provides several ways to perform matrix multiplication. The @ operator is convenient and readable, while np.matmul() and np.dot() provide function-based alternatives.
Output:
Both matrices contain two rows and two columns. Their dimensions are compatible for matrix multiplication because the number of columns in A equals the number of rows in B.
Matrix multiplication combines each row of A with each column of B.
3. Understanding the Matrix Dimension Rule
Before multiplying two matrices, always check their shapes. Matrix multiplication is possible only when the number of columns in the first matrix matches the number of rows in the second matrix.
Dimension Formula
The inner dimensions must match. The outer dimensions determine the shape of the result.
| Matrix A Shape | Matrix B Shape | Compatible? | Result Shape |
|---|---|---|---|
| (2, 3) | (3, 4) | Yes | (2, 4) |
| (3, 2) | (2, 5) | Yes | (3, 5) |
| (2, 3) | (2, 4) | No | Not possible |
| (4, 4) | (4, 2) | Yes | (4, 2) |
Example: Multiplying Different Shapes
Output:
4. Understanding the Dot Product of Vectors
A dot product multiplies corresponding elements of two vectors and adds all the products together. When two one-dimensional vectors have the same length, their dot product produces a single scalar value.
Manual Calculation
Consider the following vectors:
Using np.dot()
Output:
Using the @ Operator
Output:
For two one-dimensional vectors, both np.dot(A, B) and A @ B calculate the dot product. The output is a scalar rather than another vector.
5. Matrix Multiplication Using the @ Operator
The @ operator is a readable and convenient way to multiply matrices in NumPy. It performs matrix multiplication rather than multiplying corresponding elements.
Output:
How Is the First Result Calculated?
Calculating All Four Values
| Output Position | Calculation | Result |
|---|---|---|
| First row, first column | (1 × 5) + (2 × 7) | 19 |
| First row, second column | (1 × 6) + (2 × 8) | 22 |
| Second row, first column | (3 × 5) + (4 × 7) | 43 |
| Second row, second column | (3 × 6) + (4 × 8) | 50 |
Every element in the result matrix is calculated using one row from the first matrix and one column from the second matrix.
6. Matrix Multiplication Using np.matmul()
NumPy provides the np.matmul() function specifically for matrix multiplication. For two-dimensional matrices, it produces the same result as the @ operator when the dimensions are compatible.
Output:
Checking the Result Shape
Output:
7. Matrix Multiplication Using np.dot()
The np.dot() function calculates dot products. For two two-dimensional arrays, it also performs matrix multiplication.
Output:
Comparison of Matrix Multiplication Methods
| Method | Purpose | Two 2D Matrices |
|---|---|---|
A @ B |
Matrix multiplication operator | Performs matrix multiplication |
np.matmul(A, B) |
Matrix multiplication function | Performs matrix multiplication |
np.dot(A, B) |
Dot product function | Performs matrix multiplication |
A * B |
Element-wise multiplication | Multiplies corresponding elements |
8. Element-Wise Multiplication vs Matrix Multiplication
One common mistake is assuming that the multiplication operator * performs matrix multiplication. In NumPy, * multiplies corresponding elements, whereas @ combines rows and columns.
Output:
Why Are the Results Different?
Element-wise multiplication:
Matrix multiplication:
9. Matrix-Vector Multiplication
A matrix can also be multiplied by a one-dimensional vector. The number of columns in the matrix must equal the number of elements in the vector.
Output:
Manual Calculation
The result contains one value for each row of the matrix. Since the matrix has two rows, the resulting vector contains two elements.
10. Scalar Multiplication of a Matrix
Scalar multiplication multiplies every element in a matrix by a single numerical value. Unlike matrix multiplication, scalar multiplication does not combine rows and columns.
Output:
Each element is multiplied by 3. The shape remains (2, 2) because scalar multiplication does not change the dimensions.
11. Matrix Multiplication Is Not Generally Commutative
In general, changing the order of matrix multiplication changes the result. A @ B is not necessarily equal to B @ A. In some cases, one order is valid while the reverse order is not dimensionally compatible.
Output:
Both operations are valid for these square matrices, but their results differ. Always preserve the intended order when multiplying matrices.
12. Handling Incompatible Matrix Dimensions
If the number of columns in the first matrix does not match the number of rows in the second matrix, matrix multiplication cannot be performed.
The shapes are (2, 3) and (2, 4). The inner dimensions are 3 and 2, so NumPy raises a ValueError.
Check Shapes Before Multiplication
Output:
13. Practical Activity: Student Marks Analysis
Activity Description
At CIIT TRANING INSTITUTE, Samadhan, Pradnya, and Samiksha have marks in Python, NumPy, and Statistics. Each subject has a different weight. Use matrix multiplication to calculate the weighted score of each student.
Subject weights:
- Python: 40%
- NumPy: 35%
- Statistics: 25%
Sample Solution
Output:
Understanding the Calculation
For Samadhan:
The weighted score is calculated by multiplying each subject mark by its corresponding weight and adding the results. The same calculation is applied to every student's row.
14. Common Mistakes
Mistake 1: Using * Instead of @
The * operator performs element-wise multiplication. Use @ when you want matrix multiplication.
Mistake 2: Ignoring Matrix Dimensions
The number of columns in the first matrix must match the number of rows in the second matrix.
Mistake 3: Confusing the Dot Product with Element-Wise Multiplication
The dot product of two one-dimensional vectors produces a scalar. Element-wise multiplication produces an array of products.
Mistake 4: Assuming A @ B Equals B @ A
Matrix multiplication is generally not commutative. Reversing the order can change the result or make the operation invalid.
Mistake 5: Forgetting the Output Shape
If A has shape (m, n) and B has shape (n, p), the result has shape (m, p). Use the shape attribute to verify the output.
15. Practice Exercises
Exercise 1: Vector Dot Product
Create two vectors [2, 4, 6] and [1, 3, 5]. Calculate their dot product using np.dot() and the @ operator.
Exercise 2: Matrix Multiplication
Create the matrices [[2, 1], [3, 4]] and [[1, 5], [2, 3]]. Calculate their product using the @ operator.
Exercise 3: Compare Multiplication Methods
Multiply two 2 × 2 matrices using A * B, A @ B, np.dot(A, B), and np.matmul(A, B). Explain why the first result differs from the others.
Exercise 4: Matrix-Vector Multiplication
Create a matrix with shape (3, 2) and a vector containing two elements. Multiply them and inspect the shape of the result.
Exercise 5: Weighted Student Scores
Create a marks matrix for Samadhan, Pradnya, and Samiksha. Multiply it by a weight vector to calculate each student's weighted score.
Exercise 6: Dimension Compatibility
Create matrices with shapes (2, 3) and (3, 4). Perform multiplication and identify the output shape. Then try multiplying shapes (2, 3) and (2, 4). Explain why the second operation fails.
Summary
- The dot product multiplies corresponding vector elements and adds the products.
- The @ operator performs matrix multiplication.
- np.matmul() performs matrix multiplication.
- np.dot() calculates dot products and performs multiplication for two-dimensional matrices.
- The * operator performs element-wise multiplication.
- The inner dimensions of two matrices must match.
- A matrix with shape (m, n) multiplied by one with shape (n, p) produces shape (m, p).
- Matrix-vector multiplication produces one result for each matrix row when the dimensions are compatible.
- Matrix multiplication is generally not commutative.
- Matrix multiplication can be applied to weighted scores, numerical analysis, and scientific computing.
You are now ready to use NumPy for vector dot products, matrix multiplication, and matrix-based calculations. In the next lesson, you will learn about determinants and matrix inverses.