Principal Component Analysis: A Beginner‘s Guide
Principal Component Analysis (PCA) is a foundational unsupervised learning technique in machine learning and data science. It is widely used for exploratory data analysis, dimensionality reduction, and as a preprocessing step for other algorithms. In this comprehensive beginner‘s guide, we‘ll dive into what PCA is, how it works, when to use it, and how to interpret its results.
What is PCA?
PCA is a statistical method that aims to find a new set of features, called principal components, that best capture the essential information in your data. These new features are linear combinations of the original ones, but have some special properties:
- They are uncorrelated with each other.
- They are ordered by how much of the data‘s variance they explain.
In other words, PCA seeks to find a new orthogonal basis for the data that optimally explains its variance.
The first principal component accounts for the largest possible variance in the data, and each subsequent component accounts for the highest possible variance under the constraint that it is orthogonal to the preceding components.
Why Use PCA?
In the era of big data, datasets often contain a large number of features. While more data can lead to better models, it also comes with challenges. This is where PCA shines. Its main applications include:
-
Dimensionality Reduction: High-dimensional data can be difficult to work with due to the "curse of dimensionality". As the number of features grows, the amount of data needed to generalize accurately grows exponentially. PCA allows us to reduce the number of features while still retaining most of the information.
-
Visualization: It‘s hard to visualize data in more than three dimensions. By reducing the data to two or three principal components, we can create insightful plots that reveal hidden structure in the data.
-
Feature Extraction: PCA can be used to extract new, more informative features from the original ones. These new features can then be used for further analysis or as inputs to other machine learning algorithms.
-
Noise and Redundancy Reduction: PCA can help to identify and remove noise and redundant information in the data.
According to a survey by Kaggle, dimensionality reduction techniques like PCA are used in over 40% of machine learning projects [1]. In fields like genetics, neuroscience, and computer vision, where the number of features can easily run into the thousands or even millions, PCA is an indispensable tool.
The Mathematics of PCA
At its core, PCA relies on the eigendecomposition of the covariance matrix of the data. Let‘s break this down step by step.
Given a dataset $X$ with $n$ samples and $p$ features, we first standardize the data so that each feature has a mean of 0 and a standard deviation of 1:
$X_{standardized} = \frac{X – \mu}{\sigma}$
where $\mu$ is the mean of each feature and $\sigma$ is the standard deviation.
Next, we compute the covariance matrix of the standardized data:
$\Sigma = \frac{1}{n-1} X{standardized}^T X{standardized}$
The eigenvectors and eigenvalues of this covariance matrix are then calculated:
$\Sigma v = \lambda v$
where $v$ is an eigenvector and $\lambda$ is the corresponding eigenvalue.
The eigenvectors are then ordered by their eigenvalues, from highest to lowest. The $k$ eigenvectors with the largest eigenvalues are chosen as the $k$ principal components. These $k$ eigenvectors form a new orthogonal basis for the data.
Finally, the data is transformed into the new space defined by the principal components:
$Y = X_{standardized} W$
where $W$ is a matrix whose columns are the $k$ selected eigenvectors.
A Step-by-Step Example
Let‘s illustrate these steps with a simple example using Python and scikit-learn.
Suppose we have a dataset with two features, $x_1$ and $x_2$:
import numpy as np
from sklearn.decomposition import PCA
X = np.array([[1, 1], [1, 2], [2, 1], [2, 2],
[3, 1], [3, 2], [4, 1], [4, 2]])
We can visualize this data as follows:
import matplotlib.pyplot as plt
plt.scatter(X[:,0], X[:,1])
plt.xlabel(‘x1‘)
plt.ylabel(‘x2‘)
plt.show()

Now let‘s apply PCA:
pca = PCA(n_components=2)
pca.fit(X)
We can see the explained variance ratio of each principal component:
print(pca.explained_variance_ratio_)
[0.99244289 0.00755711]
The first principal component explains about 99.2% of the variance in the data, while the second explains about 0.8%.
We can visualize the principal components:
plt.scatter(X[:,0], X[:,1])
for length, vector in zip(pca.explained_variance_, pca.components_):
v = vector * 3 * np.sqrt(length)
plt.plot([0, v[0]], [0, v[1]], ‘k-‘, lw=2)
plt.xlabel(‘x1‘)
plt.ylabel(‘x2‘)
plt.show()

Finally, we can transform the data into the new space defined by the principal components:
X_pca = pca.transform(X)
print(X_pca)
[[-1.38340578 0.02935229]
[-0.86114989 -0.25107521]
[-0.86114989 0.25107521]
[-0.33889399 -0.0407465 ]
[-0.33889399 0.0407465 ]
[ 0.18336191 -0.28042751]
[ 0.18336191 0.28042751]
[ 0.7056178 -0.01868121]]
Interpreting PCA Results
The output of PCA can be interpreted in several ways:
-
Loadings: The eigenvectors, also known as loadings, define each principal component as a linear combination of the original features. The absolute value of each loading indicates how much the corresponding original feature contributes to that principal component.
-
Scores: After transforming the data into the space defined by the principal components, we obtain a new dataset where each sample has a "score" for each principal component. These scores represent the transformed values of each sample in the new coordinate system.
-
Scree Plot: A scree plot shows the eigenvalue associated with each principal component. Typically, there will be an "elbow" in the plot, where the eigenvalues start to level off. The components before this elbow are usually considered to be the most important.
-
Cumulative Explained Variance: This plot shows the cumulative sum of explained variance against the number of principal components. It helps determine how many components are needed to explain a certain percentage (usually 90% or 95%) of the total variance.
Choosing the Number of Components
A crucial decision in PCA is how many principal components to retain. There are several strategies:
-
Scree Plot Elbow: The elbow point in the scree plot is often chosen as the cutoff for the number of components.
-
Kaiser‘s Criterion: This rule suggests retaining only principal components with eigenvalues greater than 1, as they explain more variance than a single original variable.
-
Proportion of Variance Explained: Another approach is to select the number of components that explain a certain proportion (usually 90% or 95%) of the total variance.
In practice, the choice often involves a trade-off between simplicity (retaining fewer components) and completeness (explaining more of the variance).
Advantages and Disadvantages
PCA has several advantages:
- It can reveal hidden patterns and structures in the data.
- It reduces data complexity while minimizing information loss.
- It eliminates correlated features.
- Transformed features are orthogonal, which can be beneficial for some machine learning algorithms.
However, PCA also has some limitations:
- It assumes that the data is linearly separable.
- It is sensitive to the scale of the original features, necessitating standardization.
- It can be difficult to interpret the meaning of the principal components.
- It may not always yield the most compact representation of the data, especially if there are significant nonlinear relationships.
As noted by Jolliffe and Cadima in their 2016 paper, "PCA is, of course, not a panacea for all problems of analyzing high-dimensional datasets and it has several limitations which are important to note" [2].
PCA vs. Other Techniques
PCA is just one of many dimensionality reduction techniques. Others include:
-
t-SNE (t-Distributed Stochastic Neighbor Embedding): A nonlinear technique that is particularly well suited for visualization in two or three dimensions.
-
UMAP (Uniform Manifold Approximation and Projection): Another nonlinear technique that seeks to preserve more of the global structure of the data.
-
Autoencoders: Neural networks that learn a compressed representation of the data.
Each has its strengths and weaknesses, and the choice often depends on the specific problem and the nature of the data.
Best Practices and Tips
-
Standardize your data: PCA is sensitive to the scale of the features. Always standardize your data before applying PCA.
-
Check your assumptions: PCA assumes that your data is linearly separable. If there are significant nonlinear relationships, consider using a nonlinear dimensionality reduction technique instead.
-
Experiment with different numbers of components: There‘s no one-size-fits-all answer to how many principal components to retain. Try different values and see how they affect your results.
-
Visualize your results: Plotting the transformed data can give you valuable insights into the structure of your data.
-
Use domain knowledge: The interpretation of the principal components can be aided by domain knowledge. What do the loadings tell you about your problem domain?
Frequently Asked Questions
-
Is PCA a supervised or unsupervised technique?
PCA is an unsupervised technique, as it does not use the target variable during the learning process. -
Can PCA be used for feature selection?
Yes, PCA can be used for feature selection by keeping only the most important principal components as features. -
How is PCA related to Singular Value Decomposition (SVD)?
PCA can be performed using SVD. The right singular vectors of the data matrix correspond to the eigenvectors of the covariance matrix. -
Can PCA handle missing data?
Standard PCA requires complete data. If there are missing values, you‘ll need to either remove those samples or impute the missing values before applying PCA.
Conclusion
PCA is a powerful and versatile tool in the data scientist‘s toolkit. By understanding its strengths and limitations, you can use it effectively to simplify your data, uncover hidden patterns, and create insightful visualizations.
However, PCA is not a silver bullet. As Shlens notes in his tutorial, "PCA should be used as a guide for exploratory data analysis and not as a definitive answer" [3].
As with any technique, it‘s important to understand the assumptions behind PCA and to interpret its results in the context of your problem domain. With practice and experience, you‘ll develop an intuition for when PCA is appropriate and how to get the most out of it.
References
- Kaggle. (2021). State of Machine Learning and Data Science 2021. https://www.kaggle.com/kaggle-survey-2021
- Jolliffe, I. T., & Cadima, J. (2016). Principal component analysis: a review and recent developments. Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences, 374(2065), 20150202.
- Shlens, J. (2014). A tutorial on principal component analysis. arXiv preprint arXiv:1404.1100.