[MMD]
Linear Algebra Basics Chapter 3: Vectors, Linear Transformations, Matrix Multiplication, and Inverse Matrices
In Chapter 2, we solved systems of equations using elimination and Gaussian elimination. In this post, we take a deep dive into vectors — the core data structure of machine learning — and explore linear transformations, matrix multiplication, inverse matrices, and how all of this connects to neural networks.
This post is based on Week 3 of DeepLearning.AI's Mathematics for Machine Learning and Data Science course.
What You'll Learn in This Post#
- Addition, subtraction, scalar multiplication, and dot product operations on vectors
- Matrix-vector multiplication
- The concept of linear transformations and how to represent them with matrices
- Matrix multiplication — composing two linear transformations
- Computing the inverse matrix and understanding when it exists
- The perceptron — a neural network viewed through matrix operations
What Is a Vector?#
A vector can be thought of as an arrow in a plane or higher-dimensional space. It has two key properties:
- Magnitude: the length of the arrow
- Direction: where the arrow points
In machine learning, a single data instance (observation) is typically represented as a vector.
Vector Norm#
The norm of a vector is its magnitude — the distance from the origin to that point.
L1-Norm (Manhattan Norm)#
The sum of the absolute values of all coordinates. This is equivalent to traveling along city blocks in a grid.
L2-Norm (Euclidean Norm)#
The square root of the sum of squared coordinates — the same as finding the hypotenuse using the Pythagorean theorem.
In practice? The L2-norm is used in regularization to constrain the size of weights (L2 regularization, Ridge), while the L1-norm induces sparsity in L1 regularization (Lasso).
Vector Addition and Subtraction#
Simply add or subtract the corresponding coordinates.
Geometrically, is the diagonal of the parallelogram formed by the two vectors.
Distance Between Vectors#
Compute the difference between two vectors, then apply the L1 or L2 norm.
Scalar Multiplication#
Multiply each coordinate of a vector by a scalar (constant).
- Positive scalar: same direction, magnitude changes
- Negative scalar: direction reverses, magnitude changes
- Scalar = 0: results in the zero vector
Dot Product#
Multiply the corresponding coordinates and sum them all up. The result is a scalar.
Geometric Interpretation of the Dot Product#
The dot product is closely related to the angle between two vectors.
| value | Angle | Dot product sign |
|---|---|---|
| (same direction) | positive | |
| (orthogonal) | 0 | |
| (opposite direction) | negative |
Key insight: If two vectors are orthogonal, their dot product is always 0.
In practice? Cosine similarity is the dot product divided by the product of the two vectors' L2-norms. It is widely used in natural language processing to measure similarity between sentences.
Matrix-Vector Multiplication#
Compute the dot product of each row of the matrix with the vector.
Example:
Note: The number of columns in the matrix must match the length of the vector.
Matrix-vector multiplication is a compact way to represent a system of equations.
Linear Transformation#
A linear transformation is a function that maps each point in a plane to another point in a structured way.
The matrix-vector product is exactly a linear transformation. The matrix defines the transformation rule.
Finding the Matrix of a Linear Transformation#
To find the matrix of a linear transformation :
- Column 1: the result of
- Column 2: the result of
Example: If and , then
Matrix Multiplication#
Matrix multiplication is the operation of composing two linear transformations. Applying transformation first, then , gives .
The entry of is the dot product of the -th row of and the -th column of .
Example:
Note: — matrix multiplication is not commutative.
Size condition: If is and is , then is . The number of columns in must match the number of rows in .
Identity Matrix#
A matrix with all ones on the diagonal and zeros everywhere else.
Applying the identity matrix as a linear transformation leaves any vector or matrix unchanged.
It plays the same role as multiplying by in ordinary arithmetic: .
Inverse Matrix#
The inverse of matrix , written , satisfies:
In terms of linear transformations, the inverse matrix undoes the original transformation.
Computing the Inverse Matrix#
You can find the inverse by solving a system of linear equations.
From the condition :
Which Matrices Have an Inverse?#
- Non-singular matrix: inverse exists ()
- Singular matrix: inverse does not exist ()
The inverse matrix is used to solve systems of equations in matrix form: →
Neural Networks and Matrices — The Perceptron#
Let's see how the concepts we've learned are used in machine learning models.
The Perceptron#
The perceptron is the simplest form of a neural network. It computes the input data through a matrix product, then outputs 1 (activated) if the result exceeds a threshold, and 0 otherwise.
| Component | Matrix/Vector Role |
|---|---|
| Input data | vector (each feature is an element) |
| Weights | vector (model parameters) |
| dot product → scalar output | |
| threshold | determines whether to activate |
AND Gate Example#
| Lottery win | Exam pass | Scholarship |
|---|---|---|
| 0 | 0 | 0 |
| 1 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 1 | 1 |
Set weights to lottery=1, exam=1, threshold=1.5:
- : → 0 ✓
- : → 0 ✓
- : → 0 ✓
- : → 1 ✓
This is exactly how an AND gate is implemented as a perceptron.
In Larger Models#
When processing multiple data instances at once, matrix multiplication is used.
- : data matrix (each row is one data instance)
- : weight matrix (model parameters)
Deep learning models repeat this kind of matrix multiplication across hundreds to thousands of layers. This is why deep learning requires GPUs — they are optimized for matrix operations.
Summary#
| Concept | Description |
|---|---|
| Vector | A unit of data representation with magnitude and direction |
| L1-norm | Sum of absolute coordinate values (Manhattan distance) |
| L2-norm | Square root of sum of squared coordinates (Euclidean distance) |
| Dot product | Scalar value obtained by multiplying and summing corresponding coordinates |
| Orthogonal | Dot product = 0, two vectors at 90° |
| Linear transformation | A structured transformation expressed as a matrix-vector product |
| Matrix multiplication | Composition of two linear transformations (not commutative) |
| Identity matrix | A matrix that leaves any matrix unchanged () |
| Inverse matrix | A matrix that undoes the original transformation (exists only for non-singular matrices) |
| Perceptron | A simple neural network implemented with a dot product and threshold |
Quiz#
Q1. Find the L1-norm and L2-norm of the following vector.
Show answer
Q2. Given and , find the dot product.
Show answer
Since the dot product is negative, the angle between the two vectors is greater than 90°.
Q3. Compute the following matrix multiplication.
Show answer
Q4. Express the linear transformation , as a matrix.
Show answer
Column 1 = , Column 2 =
Q5. Determine whether the following matrix has an inverse, and find it if it does.
Show answer
Since the determinant is 0, is a singular matrix and no inverse exists.
In the next post, we will cover the geometric meaning of the determinant (as area), eigenvalue and eigenvector computation, dimensionality reduction (PCA), and Markov matrices.
// Related Posts
Probability & Statistics in Practice: Inference Problems from the ML Trenches
From probability fundamentals and Bayes' theorem to distributions, MLE/MAP, confidence intervals, and hypothesis testing — a collection of practice problems grounded in real ML and data analysis scenarios.
Probability & Statistics Coding Assignments: Building ML Statistical Tools in Python
Bayesian updates, distribution simulation, CLT verification, MLE/MAP implementation, confidence intervals, hypothesis testing, and a full A/B test pipeline — implementing probability & statistics chapters 1–4 in code.
ML Probability & Statistics Chapter 4: Confidence Intervals and Hypothesis Testing
A complete guide to confidence intervals, the t-distribution, hypothesis testing fundamentals (null/alternative hypotheses, p-values, rejection regions, statistical power), various t-tests, and A/B testing.