[MMD]
Linear Algebra in Practice: Problems You'll Actually Face in ML
This post is a collection of practice problems that apply the concepts from Linear Algebra Chapters 1–4 to real-world machine learning scenarios. The focus is on applying and interpreting concepts rather than mechanical calculation.
Part 1. Data and Matrices#
Problem 1-1. Interpreting the Rank of a Data Matrix#
A dataset has 1,000 samples and 50 features. When represented as a matrix , its rank turns out to be 30. What does this mean?
Show answer
A rank of 30 means there are effectively 30 linearly independent features.
- Of the 50 features, 20 can be expressed as linear combinations of the others → redundant information
- The true dimensionality of the information in this dataset is 30, not 50
Practical implication: Applying PCA can reduce the data from 50 to 30 dimensions with zero information loss. In practice, the top few principal components typically explain most of the variance, so you can often reduce even further below 30.
Problem 1-2. The Multicollinearity Problem#
When training a linear regression model, suppose the features satisfy . What property does the matrix have, and how does this affect model training?
Show answer
Since , the columns of the data matrix are linearly dependent.
This makes a singular matrix, so:
The closed-form solution for linear regression, , cannot be computed.
Solutions:
- Remove the redundant feature
- L2 regularization (Ridge): — the inverse always exists
- Use PCA to extract only independent components
Problem 1-3. Batch Normalization as a Matrix Operation#
In deep learning, Batch Normalization transforms each batch to have mean 0 and variance 1. How can this process be expressed as matrix operations?
Show answer
For a batch matrix (rows = samples, columns = features):
Step 1 — Mean centering:
where is the mean vector of each feature and is a ones vector
Step 2 — Divide by standard deviation:
This is the same operation as the data centering step in PCA. From a linear algebra perspective, it is a preparatory step for aligning the data along the eigenvector directions of the covariance matrix.
Part 2. Model Training and Optimization#
Problem 2-1. Determinant and Model Convergence#
During neural network training, if the determinant of a weight matrix keeps approaching 0, what problems arise?
Show answer
for a weight matrix means the matrix is approaching singularity.
Problems that follow:
- Vanishing Gradient: During backpropagation, gradients are propagated via matrix multiplication — multiplying by a near-singular matrix drives the gradient toward 0
- Loss of representational capacity: As the rank drops, the space the neural network can represent shrinks
- Numerically unstable inverse: Computing becomes numerically unreliable
Solutions: Batch normalization, residual connections (ResNet), proper weight initialization (Xavier, He initialization)
Problem 2-2. A Layer as a Linear Transformation#
In a neural network layer , if the weight matrix is , what transformation does this layer perform? Is the inverse transformation possible?
Show answer
If is (4 rows, 3 columns):
- Input dimension: 3 (a 3-dimensional vector )
- Output dimension: 4 (a 4-dimensional vector )
This layer performs a linear transformation from 3-dimensional space → 4-dimensional space.
No inverse: Since is not square, its inverse does not exist. However, the pseudoinverse can be used for an approximate inverse transformation.
Intuition: Lifting 3-dimensional data into 4 dimensions is analogous to embedding. The encoder/decoder in an autoencoder uses this kind of non-square transformation.
Problem 2-3. SVD and Model Compression#
A matrix can be decomposed via SVD as . How does using a truncated that retains only the top singular values help with model compression?
Show answer
SVD decomposes a matrix in order of information importance. Larger singular values = directions carrying more information.
Original matrix:
Keeping only terms:
Compression gain:
- Original storage: parameters
- Compressed storage: parameters
- When , the compression ratio is significant
Real-world application: LoRA (Low-Rank Adaptation) uses this principle to fine-tune LLMs efficiently, approximating large weight matrices with two low-rank matrices.
Part 3. Dimensionality Reduction and Embeddings#
Problem 3-1. PCA vs Feature Selection#
You need to reduce 100 features down to 10. What is the difference between using PCA and simply selecting the top 10 features?
Show answer
| Feature Selection | PCA | |
|---|---|---|
| Output | Keeps 10 original features | Creates 10 new principal components |
| Interpretability | High (original feature meaning preserved) | Low (principal components are linear combinations) |
| Information retention | Loses information from the 90 dropped features | Maximally preserves variance |
| Multicollinearity | Can still be present | Principal components are perfectly orthogonal (independent) |
When to choose which:
- Interpretability matters → Feature selection
- Information retention matters → PCA
- Multicollinearity is a problem → PCA is the solution
Problem 3-2. Computing Explained Variance from Eigenvalues#
Given covariance matrix eigenvalues of and a total sum of 25, what percentage of total variance do the first 2 principal components explain?
Show answer
Interpretation: The first two principal components alone explain roughly 55% of the total variance in the data.
Practical decision guidelines:
- Typically, choose enough principal components to retain 80–95%+ of explained variance
- On a Scree Plot (eigenvalues plotted in descending order), select the point where the slope sharply flattens (the "elbow")
Problem 3-3. Vector Similarity and Recommendation Systems#
User 's movie preference vector is and user 's is . Do they have similar taste? What metric do you use?
Show answer
Use cosine similarity (dot product divided by the product of L2-norms):
A cosine similarity of 0.46 is low. The two users have quite different tastes (A prefers action/sci-fi, B prefers romance/drama).
In practice: Collaborative Filtering recommendation systems are built on this principle.
Part 4. Neural Networks and Matrices#
Problem 4-1. The Forward Pass as Matrix Multiplication#
Express the forward pass of a 2-layer neural network as matrix operations:
- Input:
- Layer 1 weights: , bias:
- Layer 2 weights: , bias:
- Activation function: (ReLU)
Show answer
When processing a full batch ( samples):
As this shows, the entire neural network forward pass is a sequence of matrix multiplications. GPUs are optimized to parallelize exactly these large-scale matrix multiplications.
Problem 4-2. Backpropagation and the Chain Rule#
Explain the process of computing the gradient of loss with respect to from a linear algebra perspective.
Show answer
Backpropagation is the matrix version of the chain rule.
Step by step:
-
Compute gradient at the output layer:
-
Backpropagate to layer 1:
- : multiply by the transpose matrix to match dimensions
- : element-wise multiplication (Hadamard product)
-
Gradient for :
Key insight: The reason transpose matrices appear in backpropagation is that we are tracing the forward-pass linear transformations in reverse. The reverse of a linear transformation = multiplication by its transpose.
Quiz: Putting It All Together#
Q1. Which of the following matrices has an inverse?
Show answer
- → Singular, no inverse
- → Non-singular, inverse exists ✓
- → Singular, no inverse
Answer: B
Q2. What is the linear algebra reason that L2 regularization (Ridge) solves the multicollinearity problem in machine learning?
Show answer
With multicollinearity, becomes singular and its inverse does not exist.
L2 regularization adds to the diagonal entries:
Adding the identity matrix shifts every eigenvalue by :
This guarantees the determinant is always positive and the inverse always exists.
Q3. Why are eigenvectors in PCA mutually orthogonal?
Show answer
The covariance matrix is a symmetric matrix ().
Properties of symmetric matrices:
- Eigenvectors corresponding to distinct eigenvalues are always orthogonal
- This is known as the Spectral Theorem
Because the principal components point in mutually orthogonal directions, each one captures independent, non-overlapping information. This is exactly why PCA automatically eliminates multicollinearity.
// 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.