Unsupervised Learning: lesson 4 of 4
Unsupervised Learning
Dimensionality Reduction and PCA Intuition
Explain how PCA summarizes variation across many features.
Concept
Dimensionality reduction represents a dataset with fewer variables while trying to retain useful structure. This is helpful when a dataset has many numeric features, especially when several contain overlapping information. Principal Component Analysis (PCA) is a common method that creates new features, called principal components, ordered by how much variation they capture.
PCA does not select a few original columns. It transforms the original variables into new directions. That distinction matters for interpretation.
Why Fewer Dimensions Can Help
Consider a manufacturing table with 100 sensor readings per machine, or a customer dataset with many related spending and engagement measures. Many dimensions make visualization difficult, can add computational cost, and may include redundant or noisy variation. Reducing the representation can help with exploratory plots, compression, and some downstream workflows.
Reducing dimensions is a trade-off. Less data representation can make a task simpler, but it can also discard information and make features harder to explain. It is not an automatic improvement for every model.
PCA Intuition
Imagine plotting height and arm span. Because taller people often have longer arm spans, points may form an elongated diagonal cloud. The original horizontal and vertical axes are not the most efficient way to describe that cloud.
PCA finds a new direction that follows the greatest variation in the data. This becomes Principal Component 1 (PC1). A second component captures the largest remaining variation while being perpendicular, or orthogonal, to the first. In two dimensions, PC2 is the second perpendicular direction; in a larger dataset the same idea continues with more components.
Each component is a weighted combination of original features. A component such as 0.41 * income - 0.32 * debt + ... is not an original field with a direct business label. It is a new coordinate designed to summarize variation.
Explained Variance
explained_variance_ratio_ reports the share of variation represented by each component. For example, an illustrative result might be:
| Component | Explained variation |
|---|---|
| PC1 | 55% |
| PC2 | 25% |
| PC3 | 10% |
PC1 and PC2 together would represent 80% of the variation under this example. That can support a two-dimensional visualization, but 80% is not a universal target and does not prove that the components preserve every pattern needed for a prediction or decision. The acceptable trade-off depends on the use case.
Scaling Before PCA
PCA looks for directions with high variance. A variable measured in large units, such as annual income, can dominate smaller-unit variables such as number of support calls even if both matter conceptually. Standardizing compatible numeric features is therefore a common preparation step.
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
features = ["annual_spend", "purchase_frequency", "account_age_months"]
X = customers[features]
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X_scaled)
print(pca.explained_variance_ratio_)
X_pca has the same number of rows as X, but two new columns: PC1 and PC2. n_components=2 is chosen here to create a compact representation, often suitable for a scatter plot. The printed ratio tells you how much variation those two components each explain; do not present a number as output unless you actually compute it on the chosen dataset.
Interpreting a PCA View
A 2D PCA plot can reveal broad separation, overlap, or unusual observations. It is useful for exploratory questions such as, "Do customer records appear to form broad clouds?" It does not label those clouds, establish causal relationships, or guarantee that a cluster seen in the projection exists equally clearly in the full feature space.
Check component loadings and original-feature summaries when interpretation matters. A component that combines income, spend, and frequency might reflect a broad engagement pattern, but that is a hypothesis to validate with domain expertise.
PCA Is Not Feature Selection
Feature selection keeps or removes original variables, such as retaining tenure_months and dropping customer_id. PCA creates replacement variables that combine multiple original columns. Feature selection often preserves direct interpretability; PCA can reduce redundancy while making explanations less direct. Either may be useful, depending on the goal.
When PCA Helps
- Visualizing high-dimensional numeric data in two or three components.
- Compressing correlated measurements into a smaller representation.
- Reducing some redundancy before another analysis or model.
- Exploring whether observations have broad structure or unusual positions.
Use care when original feature meaning is important for action, compliance, or communication. A component is less straightforward to explain than annual_income or support_calls.
Failure Signals
Common Mistakes
- Saying PCA chooses the most important original features.
- Skipping scaling when features have incompatible units without a domain reason.
- Treating high explained variance as proof of predictive usefulness.
- Assuming PC1 is one original column or has a simple business meaning.
- Using a 2D projection as proof that clusters are real and separate.
Best Practices
Use PCA on appropriate numeric features after careful missing-value handling and scaling decisions. Pick the number of components based on the purpose, inspect explained variance, and test whether the compressed representation remains useful for the next step. Communicate the loss of direct feature interpretability rather than hiding it.
Interview Perspective
Question: What does PCA do?
Answer: It creates new orthogonal components that summarize variation across multiple numeric features, often allowing a lower-dimensional representation.
What the interviewer is testing: whether you distinguish components from original selected columns and recognize the interpretability trade-off.
Follow-up: Why is scaling commonly used before PCA?
Practice Questions
- Why might 100 correlated sensor features be hard to inspect directly?
- If PC1 explains 55% and PC2 explains 25% of variation, what does their combined 80% mean?
- Is PCA feature selection? Explain the difference using original variables and components.
- Why might annual income dominate PCA without scaling?
- A 2D PCA plot shows overlap between two groups. What can and can you not conclude from that view?
Quick Quiz
- Are principal components original dataset columns? Answer: No; they are combinations of original features.
- What does explained variance describe? Answer: The share of dataset variation captured by a component.
- Does high explained variance guarantee predictive performance? Answer: No; it measures representation of variation, not task success.
Key Takeaway
Key Takeaways
PCA summarizes many numeric variables with new components that capture variation. It can aid exploration, visualization, and compression, but it trades direct interpretability for a smaller representation and should be evaluated in context.
Next Lesson
This completes Unsupervised Learning: without a target, algorithms can suggest groups and compact representations, but people must decide whether those patterns matter. Next, Intro to Deep Learning introduces neural networks and learned representations for predictive tasks.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.