Recovering features names of explained_variance_ratio_ in PCA with sklearn
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Understanding PCA and Explained Variance Ratio in Sklearn
Principal Component Analysis (PCA) is a popular technique often used in data science for dimensionality reduction, data visualization, speedup of machine learning algorithms, and finding hidden patterns in data. When performing PCA using Sklearn's PCA class, one essential property that is obtained is the explained_variance_ratio_. This article will delve into the concept of explained variance ratio in PCA, how to interpret it, and, critically, how to recover feature names from the explained variance ratio output of PCA with Sklearn.
What is Explained Variance Ratio?
The explained variance ratio indicates how much variance in the data each principal component accounts for. The PCA transformation results in a number of principal components equal to the number of original features. Each component captures a certain proportion of the total dataset's variance.
Mathematically, if is the eigenvalue corresponding to the principal component, and is the sum of all eigenvalues, the explained variance ratio for the principal component can be calculated as:
Performing PCA with Sklearn
To perform PCA using Sklearn, you need to first standardize the data, since PCA is sensitive to the scale of the features. The following is a basic workflow:
After performing PCA, the explained_variance_ratio_ attribute can be accessed to understand how much variance each component explains.
Recovering Feature Names
In PCA, the individual feature names do not have explicit output as they are transformed into principal components, which are linear combinations of the features. However, you can trace back to see which features have significant contributions to these components.
Features' Contribution to Principal Components
To ascertain which original features contribute most to each principal component, you can examine the components stored in pca.components_. These are the principal axes in feature space, and each row of this array corresponds to a principal component, while each column holds the coefficient value of the original feature.
Interpretation
Large absolute values in any column suggest that the particular feature contributes significantly to that principal component. One can create a comparative table for clarity:
| Principal Component | Feature Name | Contribution |
| 1 | Feature1 | 0.7071 |
| 1 | Feature2 | 0.7071 |
| 2 | Feature1 | -0.7071 |
| 2 | Feature2 | 0.7071 |
This table shows that both features equally contribute to the first principal component, whereas the first feature contributes negatively while the second feature contributes positively to the second principal component.
Conclusion
PCA is a powerful method for reducing the dimensionality of data and identifying the most significant features in a dataset. Understanding the explained variance ratio offers insight into how much information is retained in each component, while examining the components themselves can help trace back which original features largely contribute to these components. Such discernment is crucial for data interpretation and ensuring that important information is preserved after transformation.
By becoming proficient with these PCA practices in Sklearn, you'll gain invaluable insights into your dataset and improve your data analysis capabilities.
Related reading
- reduce size of pretrained deep learning model for feature generation
- Reducing input dimensions for a deep learning model
- regarding the correct way to understand the result of tf.pad
- regarding the decoder layer definition in autoencoder model under Keras framework
- Reduce left and right margins in matplotlib plot
- reducing number of plot ticks
- Regarding the use of tf.train.shuffle_batch to create batches
- Region Growing Algorithm
.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.