Principal Components Analysis
Dimensionality Reduction
Data Science
PCA Techniques
Machine Learning

How many principal components to take?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

When dealing with high-dimensional data in fields like machine learning or statistics, one often confronts the question of dimensionality reduction or feature selection. Principal Component Analysis (PCA) is a widely used technique for this purpose. A natural question arises: How many principal components should one choose to retain the maximum amount of useful information while reducing dimensionality effectively? This article explores several approaches and considerations for selecting the appropriate number of principal components.

Understanding Principal Component Analysis (PCA)

PCA is a statistical procedure that converts a set of correlated variables into a set of uncorrelated variables, called principal components. These components are linear combinations of the original variables and are orthogonal to each other. The first principal component accounts for the largest possible variance in the data, the second for the second-largest variance, and so forth. Technically, PCA involves solving the eigenvalue problem from the covariance matrix of the data:

 
Σ = X^T X / (n-1)

where X is the data matrix with mean-centered columns, and Σ is the covariance matrix.

Criteria for Selecting the Number of Components

1. Variance Explained Criterion

A common approach is to choose enough components so that a certain percentage of the total variance is retained. This is based on the cumulative sum of the variance explained by each principal component.

For example, if you want to retain 95% of the variance, you would select the smallest number of principal components such that their cumulative explained variance reaches or exceeds 95%.

Mathematically, if λ_i represents the eigenvalue corresponding to the i-th principal component, the total variance is given by:

 
(Total Variance) = ∑_(i=1)^(p) λ_i

The explained variance for component k is:

 
(Explained Variance Ratio)_(k) = frac(λ_k)((Total Variance))

The cumulative variance is:

 
(Cumulative Variance)_(k) = ∑_(i=1)^(k) (Explained Variance Ratio)_i

2. Eigenvalue Criterion (Kaiser Criterion)

This method suggests retaining components with eigenvalues greater than 1. The logic is that an eigenvalue below 1 would imply that the component explains less variance than an individual standardized variable.

3. Scree Plot

A scree plot visually represents the eigenvalues (in descending order) against the component number. The 'elbow' point, where the curve starts to flatten, indicates diminishing returns, suggesting an optimal number of components.

4. Cross-validation

In more sophisticated analyses, particularly within machine learning, one might employ cross-validation. Here, the dataset is split into training and testing sets to assess how the model's predictive performance varies with the number of retained components. Select the number that maximizes validation accuracy while maintaining an acceptable level of complexity.

5. Domain Expertise and Interpretability

Another consideration is domain expertise. Some variables might have intrinsic importance that should not be ignored, regardless of the statistical outcome. Also, a smaller number of components make models easier to interpret.

Example Case

Consider a dataset with 10 features. Performing PCA, suppose you obtain the following results:

ComponentEigenvalueExplained Variance RatioCumulative Variance Ratio
15.1251.2%51.2%
22.5625.6%76.8%
30.808.0%84.8%
40.525.2%90.0%
50.424.2%94.2%
6-10< 0.42< 4.2% each

In this case, if your goal is to retain 90% of the variance, you would choose the first four principal components.

Considerations and Challenges

Choosing the number of components is not always straightforward and requires a balance between accuracy and efficiency. When too few components are selected, crucial information may be lost, affecting the model's performance. Selecting too many components, on the other hand, can lead to overfitting and inefficiency.

Conclusion

Choosing the number of principal components depends on multiple factors, including explained variance, eigenvalue criteria, visual inspections, cross-validation, and domain considerations. Each method has its own advantages and limitations, and often, a combination of these approaches yields the best results. By understanding the strengths and weaknesses of each method, you can make informed decisions that enhance your model's performance while keeping complexity in check.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.