Hello! Welcome back to our course on the mathematical foundations of AI.
In our last lesson, you learned the mechanics of vector and matrix operations, including how to perform matrix-vector multiplication using the @ operator in NumPy. You saw that multiplying an (m, n) matrix by an (n, 1) vector results in a new (m, 1) vector.
Today, we're going to explore the meaning behind that operation. Your goal for this lesson is to apply matrix-vector multiplication to represent linear transformations. We'll move from abstract calculation to a visual, geometric understanding of what matrices do to vectors. This intuition is fundamental in AI, as it helps explain how neural networks manipulate data to find patterns.
1. What is a Linear Transformation?
First, let's build an intuition for what a "transformation" is. In this context, a transformation is simply a function that takes a vector as an input and spits out another vector as an output. We can visualize this as a vector moving from its starting position to a new one. A transformation of space is when we apply this rule to every vector.
However, we are interested in a special, well-behaved type of transformation called a linear transformation. These have two simple, defining properties:
- All lines must remain lines (they can't get curved).
- The origin must remain fixed in place.
The following video from the "Essence of linear algebra" series by 3Blue1Brown provides a fantastic visual introduction to this idea.
Linear transformations and matrices | Chapter 3, Essence of linear algebra
This video introduces the concept of a transformation and visually defines the two properties that make a transformation 'linear'.
Watch from the beginning for just over three minutes, exploring linear transformations. Focus on understanding the visual difference between a linear and a non-linear transformation based on the two properties discussed.
In essence, linear transformations are operations like rotation, scaling, and shearing (skewing), which keep the grid lines of our vector space parallel and evenly spaced.
2. How a Matrix Represents a Linear Transformation
So, how do we connect the abstract idea of a transformation to the matrices we worked with in the last lesson? Here is the single most important insight of this lesson:
A linear transformation in 2D space is completely determined by where the two basis vectors, and , land.
- is the unit vector along the x-axis: .
- is the unit vector along the y-axis: .
Once you know the coordinates of the transformed and , you can deduce where any other vector will land. The transformation matrix is simply a way to package this information:
- The first column of the matrix is the vector where lands.
- The second column of the matrix is the vector where lands.
This next segment of the 3Blue1Brown video explains this concept beautifully.
Linear transformations and matrices | Chapter 3, Essence of linear algebra
This is the core of the lesson. It explains how a transformation is defined by its effect on the basis vectors and how these transformed vectors form the columns of the transformation matrix.
Starting around 3:44, watch the transformation explanation. Pay close attention to how any vector can be written as a linear combination of basis vectors, and how this property is preserved after a transformation. Notice how this leads directly to the definition of matrix-vector multiplication.
So, when you perform a matrix-vector multiplication, A @ v, you are essentially telling the vector v to reorient itself using the transformed basis vectors stored in the columns of matrix A.
The image below shows a specific example: reflecting a vector across the y-axis.
- The original vector is .
- The transformation matrix is .
- Applying the transformation gives .
Notice the columns of : The first column, , is where lands. The second column, , is where lands (it doesn't move). This simple matrix perfectly encodes the "reflect across the y-axis" transformation.

3. Building and Applying Transformation Matrices
Let's put this into practice. To find the matrix for any linear transformation, you just need to ask yourself: "Where do my basis vectors land?"
Example 1: 90-degree counter-clockwise rotation
- (at ) rotates to the position of , so its new coordinates are .
- (at ) rotates to the negative x-axis, so its new coordinates are .
Placing these into the columns of a matrix gives us the rotation matrix:
Now, if we want to rotate the vector , we just multiply:
The vector has been rotated to .
Example 2: Shear
A "shear" transformation pushes things horizontally. Imagine keeping the x-axis fixed but pushing the top of the grid to the right.
- stays in place, so it lands at .
- gets pushed one unit to the right, so it moves from to .
The shear matrix is:
The following video provides more worked examples of deriving matrices for rotation and shear transformations.
Foundations for ML | Linear Algebra | Linear transformation as Matrix multiplication [Lecture 3]
This video from Vizuara offers another perspective and walks through several practical examples of building transformation matrices by tracking the basis vectors.
Watch the transformation examples. This section covers the 90-degree rotation and shear transformations, showing step-by-step how to construct their matrices and verify the result on an example vector.
Test your understanding!
What 2x2 matrix corresponds to a transformation that stretches everything horizontally by a factor of 3 but leaves it unchanged vertically?
Show answer
We need to see where the basis vectors land.
- is stretched horizontally by a factor of 3, so it lands at . This is our first column.
- is unchanged vertically, so it lands at . This is our second column.
The transformation matrix is:
4. Implementing Transformations in Code
Now let's apply this in Python. The Neuromatch Academy tutorial provides a great hands-on exercise for this. You'll be asked to create the matrices for a reflection and a projection transformation. This directly tests the main idea of this lesson: can you figure out where the basis vectors go and build the corresponding matrix?
Tutorial 2: Matrices — Neuromatch Academy
This section of the Neuromatch tutorial recaps the concept of matrices as linear transformations and provides an excellent coding exercise to solidify your understanding.
First, read the brief text in 'Section 1.2: Matrices as linear transformations.' Then, complete the two parts of 'Coding Exercise 1.2: Creating matrices for transformations'. The first part asks for a reflection matrix, and the second asks for a projection matrix. Try to figure out the matrices yourself before looking at the solution.
The exercises use a helper function plot_linear_transformation(A) which you can uncomment to visualize the effect of the matrix A you create. This gives you immediate visual feedback, reinforcing the connection between the matrix and its geometric effect.
Conclusion
In this lesson, you've gained a crucial piece of intuition. Matrix-vector multiplication is not just a rote calculation; it is the engine for performing linear transformations on data.
Here are the key takeaways:
- Linear transformations are functions that move vectors in a structured way (e.g., rotation, scaling, shear) while keeping grid lines straight and the origin fixed.
- A transformation matrix is a compact way to describe a linear transformation. Its columns represent the landing spots of the basis vectors.
- Applying a transformation to a vector is done via matrix-vector multiplication (
@). This computes a weighted sum of the transformed basis vectors. - You can construct the matrix for any linear transformation by determining where and end up and placing those coordinates into the matrix columns.
In our next lesson, we will switch gears from linear algebra to the basics of calculus, starting with partial derivatives and the chain rule. These concepts are the foundation of gradient descent, the optimization algorithm used to train almost all modern AI models. You'll learn how we can calculate the "slope" of a complex function to find its minimum, which is how models learn from data.