Recommended resources
The freely available resources this lesson will direct you to.
Hello! Welcome to the second lesson in our course on advanced feature engineering.
Introduction
In our last lesson, we focused on "polishing" individual features by transforming their distributions to be more Gaussian-like using Box-Cox and Yeo-Johnson transformations. We learned how this satisfies the assumptions of certain models and improves their stability.
Now, we shift our focus from individual features to their "team dynamics"—how they relate to and interact with one another. This lesson tackles a critical and common problem in modeling: multicollinearity. This occurs when predictor variables are highly correlated with each other, making it difficult for a model to disentangle their individual contributions.
Our learning outcome for this lesson is to apply advanced multicollinearity diagnostics (e.g., condition number, determinant analysis) and remediation strategies like PCA-based regression. We will go beyond simple correlation matrices to understand and quantify collinearity with more robust statistical tools. You'll learn not just how to identify the problem, but also how to implement a powerful solution that builds directly on the linear algebra concepts you've encountered in your engineering studies.
The Problem with Redundant Features
Imagine you are building a linear model to predict a company's revenue. Your features include "marketing spend in USD" and "marketing spend in EUR". These two features convey almost the exact same information. If you include both, the model will struggle to decide how to assign credit. Should it give a positive weight to USD and a negative one to EUR? Or vice versa? The resulting coefficients will be highly unstable and sensitive to small changes in the data, rendering them uninterpretable and unreliable. This is the essence of multicollinearity.
Mathematically, the coefficients () in ordinary least squares (OLS) are calculated as:
If one feature is a linear combination of others, the matrix becomes singular, meaning its determinant is zero and it cannot be inverted. If features are nearly collinear, is ill-conditioned or nearly singular. Its inverse will have huge values, which inflates the variance of the coefficient estimates:
This leads to:
- Unstable Coefficient Estimates: Small changes in the training data can cause wild swings in the estimated coefficients.
- Inflated Standard Errors: This results in wider confidence intervals, making it likely that we will incorrectly conclude a feature is not statistically significant (high p-value) when it might actually be important.
- Poor Interpretability: Coefficients may have signs that defy logic (e.g., a negative coefficient for a feature that should have a positive effect).
To understand the core mathematical issue in more detail, please start by reading the first section of the following lecture notes.
[PDF] Lecture 17: Multicollinearity - Statistics & Data Science
These lecture notes from Carnegie Mellon University provide a rigorous mathematical foundation for why collinearity is a problem. Focus on the connection between the non-invertibility of the feature matrix and its impact on coefficient variance.
Please read Section 1, 'Why Collinearity Is a Problem'. Pay close attention to the equivalent conditions for a matrix to be singular (determinant is 0, at least one eigenvalue is 0). This sets the stage for the advanced diagnostics we'll discuss next.
Advanced Diagnostics: Moving Beyond the Correlation Matrix
The most basic way to check for multicollinearity is to compute a pairwise correlation matrix. However, this method has a major blind spot: it can't detect dependencies that involve three or more variables. For instance, if , the pairwise correlations between , and might not be close to 1, but a perfect linear dependency exists.
We need more powerful tools.
1. Variance Inflation Factor (VIF)
The VIF for a feature quantifies how much the variance of its estimated coefficient is "inflated" due to its linear dependence on other features. It is calculated as:
where is the R-squared value from a linear regression that uses as the target variable and all other features as predictors.
- Interpretation:
- A VIF of 1 means there is no correlation between and the other predictors.
- A VIF between 1 and 5 is generally considered low to moderate.
- A VIF greater than 5 or 10 indicates potentially high multicollinearity that should be investigated.
2. Eigenvalue Analysis: Condition Number
This is a more fundamental approach rooted in linear algebra, directly addressing your interest in deeper methods. The core idea is to analyze the covariance matrix of the standardized features (which is equivalent to their correlation matrix).
- Geometric Intuition: The eigenvectors of this matrix represent the principal axes (directions) of the data cloud. The eigenvalues represent the variance of the data along these axes. If the data is nearly collinear, it means the data points lie close to a lower-dimensional subspace (e.g., a line or a plane in 3D space). The direction perpendicular to this subspace will have very little variance, corresponding to a very small eigenvalue.
- Diagnosis: Perfect multicollinearity results in at least one eigenvalue being exactly zero. Near-multicollinearity results in one or more eigenvalues being close to zero.
- Condition Number: This metric summarizes the eigenvalue analysis into a single number. The Condition Number () of the correlation matrix is defined as:
where and are the maximum and minimum eigenvalues, respectively. A high condition number signifies that the matrix is ill-conditioned, meaning a small change in the input could lead to a large change in the output (the inverse), which is the hallmark of multicollinearity.
- Interpretation: Condition numbers between 10-30 suggest moderate multicollinearity, while values over 30 indicate a severe problem.
The following paper provides an excellent, accessible overview of these diagnostic tools, complete with a practical case study.
[PDF] Multicollinearity and misleading statistical results
This review paper, 'Multicollinearity and misleading statistical results,' clearly explains VIF and eigenvalue analysis and demonstrates their use on a real dataset. It's a great bridge between theory and practice.
Please read the following sections: The abstract and 'Statistical Round' on the first page for a high-level summary. 'Variance Inflation Factor' (Section 2). 'Condition Number and Condition Index' (Section 3). 'Variance Decomposition Proportion' (Section 4): This shows how to identify which variables are involved in a collinear relationship. 'Numerical Example' (Section 6): Skim this section to see how these diagnostics (VIF, Condition Indices, etc.) are reported and interpreted in a real analysis.
The image below shows a typical output from a statistical package for collinearity diagnostics. Notice how the largest condition index (15.847) corresponds to a dimension where the variance proportions for the 'Constant' and variable 'b' are extremely high (0.99), indicating a strong linear relationship between them.

Remediation Strategy: Principal Component Regression (PCR)
Once we've diagnosed multicollinearity, how do we fix it? The simplest approach is to drop one of the correlated features, but this risks losing valuable information.
A more sophisticated technique is Principal Component Regression (PCR). This method directly addresses the problem revealed by our eigenvalue analysis: if the data effectively lives in a lower-dimensional space, let's build our model in that space!
The process is as follows:
- Standardize the predictor variables.
- Perform Principal Component Analysis (PCA) on the predictors to obtain a new set of variables called Principal Components (PCs). These PCs are ordered by the amount of variance they explain, and crucially, they are all orthogonal (uncorrelated) to each other.
- Select a subset of the top PCs that capture most of the variance in the original data. This is often done by examining a "scree plot," which shows the variance explained by each component.
- Run a linear regression using the selected PCs as predictors for the target variable .
Because the PCs are uncorrelated, the regression model built on them has no multicollinearity issues.
To get a clear, step-by-step walkthrough of this process, please watch the following video.
Principal component regression (PCR) - explained
The video 'Principal component regression (PCR) - explained' by TileStats provides an excellent conceptual and practical explanation of how PCR works to overcome collinearity.
Please watch the entire video (around 14 minutes). Pay special attention to: The illustration of the problem with unstable coefficients (00:00 - 03:08). How PCA is used to combine the correlated variables into new, uncorrelated components (03:08 - 06:31). How to select the optimal number of components using cross-validation (08:54 - 13:08). The key limitation of PCR: it is an unsupervised method (13:08 - 14:37).
The PCA Refresher
If you'd like a quick refresher on the mechanics of PCA itself—how it finds the components that maximize variance—the following StatQuest video is an invaluable resource.
StatQuest: Principal Component Analysis (PCA), Step-by-Step
This StatQuest video with Josh Starmer breaks down Principal Component Analysis visually and intuitively. It's a great supplement to understand the 'how' behind PCR.
You can focus on these two key parts: How PC1 is Found (03:32 - 10:47): Understand the core idea of finding the line that maximizes the sum of squared distances of projected points to the origin. Using the Scree Plot (15:06 - 16:36): This explains how to use eigenvalues to determine how much variation each PC accounts for, which is critical for deciding how many components to keep.
The scree plot, as discussed in the videos, is your primary tool for deciding how many components to retain. You typically look for an "elbow" in the plot, where adding more components yields diminishing returns in explained variance or model performance.

PCR: The Trade-Offs
- Benefit: PCR effectively eliminates multicollinearity, leading to a stable model.
- Drawback: Interpretability suffers. Each PC is a linear combination of all original features. A coefficient on PC1 doesn't tell you about the effect of a single original feature; it tells you about the effect of a complex blend of them.
- Key Limitation: As the TileStats video highlights, PCA is unsupervised. It finds the directions of maximum variance in the predictors (
X) without any regard for the target variable (y). It's possible for a low-variance component (which might be discarded) to be highly correlated with the target. This is a fundamental drawback to keep in mind.
Practical Implementation in Python
Here’s a sketch of how you would implement PCR using scikit-learn. Notice how it fits neatly into a pipeline.
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline
# Assume X_train, y_train are your training data
# 1. Define the PCR pipeline
# We first scale the data, then apply PCA, then run linear regression.
pcr_pipeline = Pipeline([
('scaler', StandardScaler()),
('pca', PCA(n_components=0.95)), # Keep components explaining 95% of variance
('regressor', LinearRegression())
])
# 2. Fit the pipeline on the training data
pcr_pipeline.fit(X_train, y_train)
# The 'pca' step transforms the scaled data into uncorrelated components,
# and the 'regressor' is trained on these components.
# 3. Make predictions
# The pipeline automatically handles scaling and PCA transformation for new data.
predictions = pcr_pipeline.predict(X_test)
# You can inspect the components found
print(f"Number of components chosen: {pcr_pipeline.named_steps['pca'].n_components_}")
Interview & Business Context Alert
Scenario: You've built a linear model to predict customer churn. You find that the coefficient for
total_data_usageis positive (more usage leads to more churn), which seems counterintuitive. An executive asks you to explain this.Your Analysis: "This is a classic potential symptom of multicollinearity.
total_data_usageis likely highly correlated with other features likemonthly_charges. The model is struggling to isolate their individual effects, leading to an unreliable coefficient. My next step would be to formally diagnose this using the Variance Inflation Factor (VIF) and Condition Number of the feature correlation matrix. If high collinearity is confirmed, I would implement a more robust modeling technique like Principal Component Regression or Ridge Regression. This would produce a more stable model, although we might trade some direct interpretability of thetotal_data_usagefeature for overall model reliability and predictive accuracy."This answer demonstrates you can debug a model beyond looking at accuracy, connect statistical artifacts to business logic, and propose concrete, advanced solutions.
Conclusion
In this lesson, we tackled the challenge of redundant information within our feature set. We established a clear narrative: from identifying the problem's symptoms to using sophisticated diagnostics and implementing a powerful, principled solution.
Key Takeaways:
- Multicollinearity destabilizes linear models by inflating the variance of coefficient estimates, making them unreliable and difficult to interpret.
- Simple correlation matrices are insufficient for diagnosis. Advanced tools like Variance Inflation Factor (VIF) and eigenvalue analysis (Condition Number) provide a much more robust assessment.
- Principal Component Analysis (PCA) can transform a set of correlated features into a smaller set of uncorrelated Principal Components.
- Principal Component Regression (PCR) leverages this by building a regression model on these stable components, though at the cost of direct feature interpretability.
- The choice of how many components to keep is a critical modeling decision, often guided by a scree plot or cross-validation.
What's Next?
We've now covered how to refine individual features (Lesson 1) and how to handle redundant features (Lesson 2). In the next lesson, we will explore a completely different way of evaluating features: through the lens of information theory. We will learn to use concepts like mutual information and entropy to quantify a feature's relevance and its interaction with the target variable, providing a powerful, model-agnostic tool for feature selection.