Hello! Welcome to the third lesson in our module on Linear Algebra Foundations.
In our previous lesson, we explored how to measure the "size" of vectors and matrices using norms, like the L1, L2, and Frobenius norms. This gave us a way to quantify concepts like distance and error.
Today, we're moving on to two of the most fundamental and frequently used matrix operations in all of machine learning: matrix multiplication and matrix transposition. The goal of this lesson is to learn how to perform these operations and understand their crucial properties. Mastering these is non-negotiable for understanding how algorithms like linear regression and deep neural networks process data.
1. Matrix Multiplication: The Engine of Machine Learning
Matrix multiplication is more than just a computational procedure; it's the mathematical operation that underpins how information is transformed in most machine learning models. Unlike element-wise operations, it combines information from rows and columns in a very specific way.
The Fundamental Rule of Matrix Multiplication
The first thing to learn about matrix multiplication is the rule for when it's possible. To multiply two matrices, the number of columns in the first matrix must equal the number of rows in the second matrix.
If you have a matrix with dimensions and a matrix with dimensions , you can multiply them to get a new matrix with dimensions .
Each element in the resulting matrix is calculated by taking the dot product of a row from matrix and a column from matrix .

A Step-by-Step Walkthrough
To see this process in action, let's watch a video that calculates a matrix-vector product (a special case of matrix multiplication) and then a full matrix-matrix product by hand.
Matrix Multiplication — Topic 19 of Machine Learning Foundations
The video 'Matrix Multiplication' by Jon Krohn provides a clear, step-by-step calculation of matrix multiplication, first with a vector and then with another matrix. This will solidify the computational mechanics.
Please watch the following segments: The introduction (00:00 - 01:47) to understand the dimension compatibility rule. The matrix-vector multiplication example (03:01 - 07:35). The matrix-matrix multiplication example (07:35 - 12:45). Focus on how each element of the output matrix is computed from a row of the first matrix and a column of the second.
Matrix Multiplication in NumPy
As you might expect, you don't need to implement this by hand in Python. NumPy provides a dedicated operator, @ (the "at" symbol), for matrix multiplication. This is the modern, recommended way to perform this operation. An older method, np.dot(), also works and you will see it in many codebases.
Let's use the examples from the video to perform the calculation in NumPy.
import numpy as np
# Matrix-vector multiplication example from the video
A_mv = np.array([[3, 4],
[5, 6],
[7, 8]])
B_mv = np.array([[1],
[2]]) # Shape (2, 1) to be a column vector
# In the video, B was a 1D array. NumPy is flexible, but being explicit with shapes is good practice.
# B_mv_1d = np.array([1, 2])
# print(A_mv @ B_mv_1d) # This also works
C_mv = A_mv @ B_mv
print("Matrix-Vector Multiplication Result:")
print(C_mv)
print("-" * 20)
# Matrix-matrix multiplication example
A_mm = np.array([[3, 4],
[5, 6],
[7, 8]])
B_mm = np.array([[1, 9],
[2, 0]])
C_mm = A_mm @ B_mm
print("Matrix-Matrix Multiplication Result:")
print(C_mm)
2. The Deeper Meaning: Multiplication as Transformation
So, what does matrix multiplication mean? From a geometric perspective, it represents applying one linear transformation after another. This is called the composition of transformations.
For example, you could have one matrix that represents a rotation and another that represents a shear. Multiplying them together creates a single new matrix that represents the combined effect of rotating and then shearing.
This is a profound insight that simplifies many complex ideas in linear algebra. Let's see it explained visually.
Matrix multiplication as composition | Chapter 4, Essence of linear algebra
In this classic video 'Matrix multiplication as composition' from the 'Essence of linear algebra' series, 3Blue1Brown provides the definitive geometric intuition for matrix multiplication.
Watch the segment from 01:50 to 07:04. Focus on how multiplying two matrices is equivalent to composing two successive linear transformations. Notice how the columns of the resulting matrix are determined by where the basis vectors land after both transformations.
3. Properties of Matrix Multiplication
Understanding the properties of matrix multiplication is essential for correctly manipulating equations in machine learning.
- Not Commutative: This is perhaps the most important property. In general, . The order matters! Thinking geometrically, rotating then shearing is not the same as shearing then rotating.
- Associative: The grouping of multiplication doesn't matter. . Geometrically, this means applying transformation C, then B, then A is the same regardless of how you group the operations.
- Distributive: It distributes over addition. .
- Multiplicative Identity: There is an identity matrix , which is a square matrix with 1s on the main diagonal and 0s elsewhere. For any matrix , and . It's the matrix equivalent of the number 1.
The text below summarizes these properties clearly.
Introduction to Linear Algebra for Applied Machine ...
The 'Matrix-matrix multiplication' section from the 'Introduction to Linear Algebra for Applied Machine...' textbook provides a concise formal definition and lists the key properties of this operation.
Read the subsection titled 'Matrix-matrix multiplication'. Pay close attention to the list of properties (Associativity, Distributivity, etc.) and the explicit warning that matrix multiplication is not commutative.
4. Matrix Transposition
The transpose is a much simpler operation that rearranges a matrix's elements.
Definition and Calculation
The transpose of a matrix is obtained by swapping its rows and columns. The first row becomes the first column, the second row becomes the second column, and so on. The transpose of matrix is denoted as .
If is an matrix, then will be an matrix.
Example:
In NumPy, you can get the transpose of an array using the .T attribute.
A = np.array([[1, 2, 3],
[4, 5, 6]])
A_transpose = A.T
print("Original Matrix A (2x3):")
print(A)
print("\nTransposed Matrix A.T (3x2):")
print(A_transpose)
To learn more about the properties and some nuances of transposition, please review the following resource.
The article 'Matrix Transpose Operation' provides a thorough explanation of the transpose, its properties, and how it works in NumPy.
Please read the entire article, but focus on these sections: 'What is a Matrix Transpose?' for the definition. 'Transposing Matrices in NumPy' for the code examples. 'Properties of the Transpose' for the key rules, especially the transpose of a product. 'Transposing Vectors' for the important note on 1D vs. 2D arrays.
Key Properties of the Transpose
As detailed in the article, the transpose has several important properties. The most critical one for machine learning is the transpose of a product:
Notice that the order of the matrices is reversed. This property is used extensively in deriving machine learning algorithms.
Test your understanding!
You are given two matrices:A = np.array([[1, 0], [2, 3]]) (a 2x2 matrix)B = np.array([[4, 5, 6], [7, 8, 9]]) (a 2x3 matrix)
- Can you compute
A @ B? If so, what is the shape of the resulting matrix? - Can you compute
B @ A? Why or why not? - Calculate
C = A @ B. - What is the shape of
C.T? Verify the property(AB)^T = B^T A^Tby calculatingB.T @ A.Tand comparing it toC.T.
Show answer
- Yes.
Ais (2x2) andBis (2x3). The inner dimensions match (2 and 2). The resulting matrix will have the shape (2x3). - No.
Bis (2x3) andAis (2x2). The inner dimensions (3 and 2) do not match. C = A @ Bresults in[[4, 5, 6], [29, 34, 39]].A = np.array([[1, 0], [2, 3]]) B = np.array([[4, 5, 6], [7, 8, 9]]) C = A @ B # C is [[1*4+0*7, 1*5+0*8, 1*6+0*9], [2*4+3*7, 2*5+3*8, 2*6+3*9]] # C is [[4, 5, 6], [8+21, 10+24, 12+27]] # C is [[4, 5, 6], [29, 34, 39]]C.Thas the shape (3x2).
B.Tis (3x2) andA.Tis (2x2). The productB.T @ A.Tis valid and results in a (3x2) matrix.
The results are identical, confirming the property.C_T = C.T # C_T is [[4, 29], [5, 34], [6, 39]] BT_AT = B.T @ A.T # B.T is [[4, 7], [5, 8], [6, 9]] # A.T is [[1, 2], [0, 3]] # BT_AT is [[4*1+7*0, 4*2+7*3], [5*1+8*0, 5*2+8*3], [6*1+9*0, 6*2+9*3]] # BT_AT is [[4, 8+21], [5, 10+24], [6, 12+27]] # BT_AT is [[4, 29], [5, 34], [6, 39]]
Conclusion
Great job today! We've covered two workhorse operations of linear algebra that are absolutely essential for machine learning.
Key Takeaways:
- Matrix Multiplication:
- Requires the inner dimensions of the matrices to match: .
- It is not commutative (), but it is associative.
- Geometrically, it represents the composition of linear transformations.
- In NumPy, use the
@operator.
- Matrix Transposition:
- Swaps the rows and columns of a matrix.
- The transpose of a product reverses the order: .
- In NumPy, use the
.Tattribute.
These operations are not just abstract math; they are how a neural network processes inputs through its layers and how a linear regression model makes predictions from features.

Preview of the Next Lesson:
In the next lesson, we will delve deeper into the geometric intuition we touched on today. We will formally describe linear transformations like scaling, rotation, and shearing and see how to represent them with matrices. This will connect the abstract algebra to visual, geometric changes in space.