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.
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:
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:
The explained variance for component k is:
The cumulative variance is:
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:
| Component | Eigenvalue | Explained Variance Ratio | Cumulative Variance Ratio |
| 1 | 5.12 | 51.2% | 51.2% |
| 2 | 2.56 | 25.6% | 76.8% |
| 3 | 0.80 | 8.0% | 84.8% |
| 4 | 0.52 | 5.2% | 90.0% |
| 5 | 0.42 | 4.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
- How many processes does TensorFlow open?
- How much matrix size the function Spectral clustering of Scikit learn can handle?
- How much time does it take to train a SVM classifier?
- How predict_proba in sklearn produces two columns? what are their significance?
- How shall we read the Kafka topics in a given time range?
- How should I handle input data with nan values in TensorFlow?
- How SelectKBest chi2 calculates score?
- How should BatchNorm layer be used in caffe?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.