Lecture 03: Linear Algebra Review
A comprehensive review of essential linear algebra concepts for machine learning, including matrices, vectors, matrix operations, inverses, transposes, eigenvalues, eigenvectors, and key properties for probabilistic models (GDA, GMM, EM).
Matrices - Overview
A matrix is a rectangular array of numbers written between square brackets.
- 2D Array: Matrices are represented as two-dimensional arrays.
- Naming: Named using capital letters (e.g., ).
- Dimensions: The dimensions of a matrix are defined as .
- Count starting from top-left, down to bottom-left, and across to bottom-right.
- denotes a matrix with rows and columns.

- The example matrix above is a matrix.
Matrix Elements
- .

Matrices provide a powerful way to organize, index, and access large volumes of data.
Vectors - Overview
A vector is a matrix with rows and 1 column ( matrix).
- Naming: Usually denoted by lower-case letters (e.g., ).
- Dimensions: Has rows and 1 column.

- The example above is a 4-dimensional vector, referred to as belonging to .
Vector Elements
- .
- Indexing:
- Vectors can be 0-indexed (common in C/C++) or 1-indexed (common in MATLAB).
- In standard mathematics, 1-indexing is most common.
- In machine learning implementations, 0-indexing is frequently useful.
- Normally assume 1-indexed vectors unless explicitly specified as 0-indexed.
Matrix Manipulation
Addition
- Add corresponding elements one at a time.
- Can only add matrices of the same dimensions.
- Addition yields a new matrix of the exact same dimensions as the operands.

Multiplication by Scalar
- Scalar: A real number.
- Multiply every element of the matrix by the scalar.
- Generates a matrix of the same dimensions as the original matrix.

Division by Scalar
- Equivalent to multiplying the matrix by (e.g., multiplying by ).
- Each element is divided by the scalar.
Combination of Operands
- Standard order of operations applies: evaluate scalar multiplications first before additions.

Matrix-Vector Multiplication
Multiplying a matrix by a vector yields a vector.
- General Rule: Multiplying an matrix by a matrix/vector yields an result.
- Procedure:
- Take the elements of the vector and multiply them with the corresponding elements of the matrix's first row, then sum the results to get the first element of the new vector.
- Repeat the step for the second row of the matrix to get the second element.
- Repeat for the final row to complete the new vector.

Detailed Rules
For :
- is an matrix.
- is an vector.
- The inner dimensions () must match between the matrix and vector.
- The result is an -dimensional vector ().
- To calculate , multiply the elements of 's row by all the corresponding elements of vector and sum them up.
Vectorization Trick for Machine Learning
Suppose we have a dataset with four samples and a hypothesis function:
Instead of computing predictions individually:
- Represent your dataset as a data matrix.
- Put parameters () into a column vector.
- Compute predictions in a single matrix-vector multiplication:

- Key Step: Add an extra column of 1s to the data matrix (representing ) so that the intercept parameter is properly calculated.
- Efficiency: This matrix formulation is computationally far more efficient than explicit
forloops and produces cleaner, more concise code when using optimized linear algebra libraries.
Matrix-Matrix Multiplication
General Concept
- Step through the second matrix one column at a time.
- Multiply each column vector of the second matrix by the entire first matrix, generating a column vector for each step.
- Combine (concatenate) the resulting column vectors side-by-side to form the final matrix.
Details
For :
- is .
- is .
- is . (Note: matrix-vector multiplication is just the special case where ).
- Multiplication is only valid if the number of columns in matches the number of rows in .
Summary
The column of matrix is obtained by multiplying matrix with the column of matrix .
Step-by-Step Example
Given :

- Multiply matrix by the 1st column vector of to get the 1st column of the result.
- Multiply matrix by the 2nd column vector of to get the 2nd column of the result.

- Multiplying a matrix by a matrix produces a matrix.
Implementation and Applications
Vectorizing Multiple Hypotheses
Consider a housing price dataset where we want to evaluate three competing hypotheses on the same data:
- We have 4 house sizes (samples) and want to compute predictions under 3 different hypotheses simultaneously.
- Create a data matrix (house sizes with an extra column of 1s).
- Create a parameter matrix where each column represents the parameters of one hypothesis.

Benefits:
- Simultaneously evaluates 3 hypotheses across 4 houses, producing all 12 predictions in a single matrix multiplication.
- Leverages optimized parallel linear algebra routines for maximum computational efficiency.
Matrix Multiplication Properties
While matrix multiplication is a powerful operation, it behaves differently from scalar multiplication in key ways.
Commutativity
- For scalars: (commutative).
- For matrices: in general.
- Matrix multiplication is NOT commutative.
Associativity
- For scalars: .
- For matrices: Matrix multiplication IS associative.
Identity Matrix
- In scalar arithmetic, is the multiplicative identity ().
- In matrix algebra, the Identity Matrix is denoted by (or ).

- Properties of :
- Square matrix ().
- Contains s along the main diagonal and s everywhere else.
- A identity matrix is simply .
- Multiplying any matrix by an appropriate identity matrix returns :
- (where is )
- (where is )
- Exception to Commutativity: Matrix multiplication is commutative only when one of the matrices is the identity matrix ().
Inverse and Transpose Operations
Matrix Inverse
Concept in Real Numbers
- The number is the multiplicative identity.
- The inverse of a number is , such that (e.g., ).
- Not all numbers have an inverse: does not have a multiplicative inverse.
Matrix Inverse Definition
- If is an matrix, its inverse is such that:
- Square Matrices Only: Only square matrices () can have an inverse.

Singular and Degenerate Matrices
- Matrices filled with all zeros do not have an inverse.
- Matrices that do not have an inverse are called singular or degenerate matrices (intuitively, matrices that are "too close to 0" or lack full rank).
Matrix Transpose
Concept
Given an matrix , the transpose is an matrix formed by swapping rows and columns.
Procedure
- 1st row of becomes the 1st column of .
- 2nd row of becomes the 2nd column of .
- In general, for :

Eigenvalues and Eigenvectors
Definition and Geometric Intuition
For an square matrix , a non-zero vector is an eigenvector if multiplying it by yields a scaled version of itself:
- : An transformation matrix.
- : The eigenvector ().
- : The eigenvalue (a scalar scaling factor).
Geometric Meaning
Most linear transformations both rotate and stretch vectors. Eigenvectors are special invariant directions that do not rotate under the transformation—they only stretch, shrink, or flip along the same line:
- : The vector stretches.
- : The vector shrinks.
- : The vector flips in the opposite direction.
- : The vector collapses to zero ( lies in the null space of ).
Characteristic Equation
To compute eigenvalues, solve the characteristic equation:
Special Eigenvalue Properties in Theoretical Machine Learning
In probabilistic machine learning algorithms such as Gaussian Discriminant Analysis (GDA), Gaussian Mixture Models (GMM), and the Expectation-Maximization (EM) algorithm, eigenvalues play a foundational role in analyzing covariance matrices () and Hessian matrices ().
1. Covariance Matrices & Positive Definiteness
Any empirical covariance matrix is symmetric () and Positive Semi-Definite (PSD) ():
- Real Eigenvalues: All eigenvalues of a symmetric matrix are strictly real ().
- Non-Negative Eigenvalues: For PSD matrices, .
- Strictly Positive Definite (PD): If , then every eigenvalue is strictly positive (), guaranteeing that variance along any arbitrary projection is strictly positive.
2. Determinant as Volume () & The EM Singularity
The determinant of a covariance matrix is the product of its eigenvalues:
- Gaussian Normalization: Appears in the denominator of the multivariate Gaussian probability density function:
- EM Singularity Problem in GMMs: If a Gaussian component collapses onto a single data point during EM optimization, the variance along one dimension shrinks to zero (). Consequently, , causing and the likelihood objective to diverge to .
3. Precision Matrix () & Conditional Independence
If has eigenvectors with eigenvalues , then the precision matrix shares the exact same eigenvectors , but with inverted eigenvalues:
- Checking Conditional Independence:
In a multivariate Gaussian distribution , two variables and are conditionally independent given all other variables if and only if their corresponding off-diagonal entry in the precision matrix is zero:
- Contrast this with marginal independence, which holds if and only if the covariance entry is zero: .
- Mahalanobis Distance: In the quadratic form , directions with large variance () receive smaller weight penalties, naturally normalizing distances across features with varying scales.
4. Spectral Decomposition & Geometric Contours of Gaussians
By the Spectral Theorem, any symmetric matrix can be decomposed into: where is an orthogonal matrix of eigenvectors () and .
- Iso-Density Contours: The level curves of equal probability form an ellipsoid.
- Principal Axes: The axes of the ellipsoid point along the eigenvectors .
- Axis Radii: The half-lengths of the semi-axes are proportional to (the standard deviation along that principal direction).
5. Trace as Total Variance ()
The trace of a square matrix equals the sum of its diagonal entries, which also equals the sum of its eigenvalues:
- In feature analysis and PCA, measures the total variance across all features in the dataset.
6. Regularization (Jitter / Ridge) Shifts Eigenvalues
When applying shrinkage or diagonal jittering to stabilize covariance estimation:
- The eigenvectors remain completely unchanged.
- Every eigenvalue is shifted by a constant: .
- Practical Benefit: Even if is singular or rank-deficient (), is guaranteed to be strictly positive definite with minimum eigenvalue , ensuring numerical stability when taking matrix inverses in GDA and EM.
7. Generalized Eigenvalue Problems in LDA / GDA
In Linear Discriminant Analysis (and Gaussian Discriminant Analysis with shared covariance), finding the optimal linear projection that maximizes the ratio of between-class variance to within-class variance involves maximizing the Rayleigh Quotient: Taking the gradient and setting it to zero yields the generalized eigenvalue problem: The optimal projection direction is the eigenvector corresponding to the largest eigenvalue of .
8. Hessians, Curvature, and Convexity in Maximum Likelihood
In optimization and Maximum Likelihood Estimation (MLE):
- The curvature of an objective function is governed by its Hessian matrix .
- If all eigenvalues of are strictly negative (), the objective function is strictly concave. This guarantees that any stationary point found by gradient ascent or Newton's method is the unique global maximum (e.g., Logistic Regression, GLMs).
Eigenvalues in Machine Learning: Quick Reference
| Property | Mathematical Form | ML Context & Application |
|---|---|---|
| Positivity | Guarantees non-zero variance and invertible covariance in GDA/GMMs | |
| Determinant | Normalization factor in Gaussian PDF; causes EM divergence | |
| Precision | Scales Mahalanobis distance along principal axes | |
| Conditional Independence | Zero entries in precision matrix indicate conditional independence | |
| Spectral Form | Defines orientation () and semi-axis lengths () of Gaussian density ellipsoids | |
| Trace | Total variance in the dataset | |
| Regularization | Jittering / Ridge shrinkage to ensure numerical invertibility | |
| Rayleigh Ratio | LDA / GDA optimal discriminant projection vector | |
| Hessian Curvature | Strict concavity in MLE, guaranteeing unique global optima |
Lecture 01 & 02: Introduction, Regression Analysis and Gradient Descent
An introduction to machine learning concepts, supervised vs unsupervised learning, univariate linear regression, cost functions, and gradient descent optimization.
Lecture 04: Linear Regression with Multiple Variables
A comprehensive guide to multivariate linear regression, covering gradient descent with multiple features, feature scaling, learning rate selection, polynomial regression, and the normal equation method.