Sitemap

Understanding Principal Component Analysis (PCA)

9 min readJun 8, 2026

--

Press enter or click to view image in full size

Principal Component Analysis, or PCA, is a fundamental technique in the realm of data analysis and machine learning. It plays a pivotal role in reducing the dimensionality of complex datasets, simplifying their interpretation, and enhancing computational efficiency. In this comprehensive article, we will delve into the concept of PCA in detail, exploring its underlying principles, applications, and the mathematics behind it.

What is Principal Component Analysis?

Principal Component Analysis is a statistical method that transforms high-dimensional data into a lower-dimensional form while preserving the most important information. It accomplishes this by identifying new axes, called principal components, along which the data varies the most. These components are orthogonal to each other, meaning they are uncorrelated, making them a powerful tool for dimensionality reduction.

The Need for Dimensionality Reduction

Imagine you have a dataset with many features or variables. Each feature contributes to the overall complexity of the data, making it challenging to analyze, visualize, or build models. High dimensionality can lead to various problems:

  1. Computational Complexity: As the number of features increases, the computational resources and time required for analysis and modeling grow exponentially.
  2. Overfitting: Models can become overly complex and fit the noise in the data, leading to poor generalization on new, unseen data.
  3. Difficulty in Visualization: It becomes challenging to visualize and understand data in more than three dimensions.
  4. Redundancy: Some features may be highly correlated, meaning they convey similar information. This redundancy can be eliminated without significant loss of information.

This is where PCA comes into play. It helps us reduce the dimensionality of the data while preserving its essential characteristics.

The Mathematics Behind PCA

PCA begins with a set of data points, typically represented as a matrix, where rows represent observations, and columns represent features.

Step 1: Data Standardization

Before performing PCA, it’s essential to standardize the data. This means centering the data by subtracting the mean and scaling it by dividing by the standard deviation. Standardization ensures that all features have equal importance in the analysis.

Step 2: Covariance Matrix

PCA hinges on the computation of the covariance matrix. The covariance between two variables measures how they change together. The covariance matrix for a dataset with n features is an n x n matrix that summarizes the relationships between all pairs of features.

Step 3: Eigenvalue and Eigenvector Calculation

The next step is to compute the eigenvalues and eigenvectors of the covariance matrix. These eigenvalues represent the amount of variance explained by each eigenvector (principal component). Eigenvalues and eigenvectors are mathematical concepts related to linear transformations and matrices. In the context of PCA, they play a central role in identifying the principal components. Here’s what they mean:

  • Eigenvalue: An eigenvalue (λ) represents a scalar that indicates how much variance is explained by the corresponding eigenvector. In PCA, eigenvalues quantify the importance of each principal component. They are always non-negative, and the eigenvalue corresponding to a principal component measures the proportion of the total variance in the data explained by that component.
  • Eigenvector: An eigenvector (v) is a vector associated with an eigenvalue. In PCA, eigenvectors represent the directions along which the data varies the most. Each eigenvector points in a specific direction in the feature space and corresponds to a principal component. Eigenvectors are typically normalized, meaning their length is 1.

Step 4: Sorting Eigenvalues and Eigenvectors

To identify the most significant principal components, sort the eigenvalues in descending order. The corresponding eigenvectors are also sorted accordingly. The first principal component explains the most variance, the second explains the second most, and so on.

Step 5: Selecting Principal Components

Choose a subset of the top k eigenvectors to form a transformation matrix. After computing the eigenvalues and eigenvectors of the covariance matrix, they are sorted in descending order based on the magnitude of their eigenvalues. The principal components are then selected from the top eigenvectors. The first principal component corresponds to the eigenvector with the largest eigenvalue, the second principal component corresponds to the eigenvector with the second-largest eigenvalue, and so on. These principal components are orthogonal, meaning they are uncorrelated. This matrix is used to project the original data into a lower-dimensional space, resulting in the reduced dataset.

Applications of PCA

PCA finds applications in various domains, including but not limited to:

  1. Image Compression: Reducing the dimensionality of image data while preserving essential information, which is crucial for image storage and transmission.
  2. Bioinformatics: Analyzing high-dimensional gene expression data to identify patterns and reduce noise.
  3. Face Recognition: Extracting essential facial features for recognition tasks.
  4. Recommendation Systems: Reducing the dimensionality of user-item interaction data for efficient recommendation algorithms.
  5. Finance: Analyzing financial data to identify underlying trends and patterns.

Principal Component Analysis is a versatile and powerful technique for dimensionality reduction and feature extraction. By transforming complex, high-dimensional datasets into a more manageable form, PCA aids in data exploration, visualization, and analysis. Its mathematical underpinnings, coupled with its diverse applications, make PCA an indispensable tool in the toolkit of data scientists and analysts worldwide. Understanding the principles and mathematics behind PCA is essential for harnessing its full potential in real-world data analysis tasks.

Implementing PCA in Python

Let’s implement PCA using Python and the popular library, scikit-learn. Ensure you have scikit-learn installed in your environment.

import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
# Create a sample dataset
data = np.array([[1.0, 2.0, 3.0],
[4.0, 5.0, 6.0],
[7.0, 8.0, 9.0]])
# Standardize the data
scaler = StandardScaler()
data_std = scaler.fit_transform(data)
# Create a PCA instance
pca = PCA(n_components=2) # Specify the number of components
# Fit PCA to the standardized data
pca.fit(data_std)
# Transform the data into the reduced dimensionality
data_pca = pca.transform(data_std)
# The principal components
components = pca.components_
# Explained variance ratio
explained_variance_ratio = pca.explained_variance_ratio_
# Print the results
print("Original Data:")
print(data)
print("\nStandardized Data:")
print(data_std)
print("\nPrincipal Components:")
print(components)
print("\nExplained Variance Ratio:")
print(explained_variance_ratio)
print("\nData in Reduced Dimensionality:")
print(data_pca)

In this example, we create a sample dataset, standardize it, and then apply PCA to reduce its dimensionality to 2 components. You can adjust the number of components (n_components) as needed.

Interview Questions on PCA

1.Explain the Curse of Dimensionality?

The curse of dimensionality refers to all the problems that arise working with data in the higher dimensions. As the number of features increase, the number of samples increases, hence, the model becomes more complex. The more the number of features, the more the chances of overfitting. A machine learning model that is trained on a large number of features, gets increasingly dependent on the data it was trained on and in turn overfitted, resulting in poor performance on real data, beating the purpose. The fewer features our training data has, the lesser assumptions our model makes and the simpler it will be.

2. Why do we need dimensionality reduction? What are its drawbacks?

In Machine Learning, dimension refers to the number of features. Dimensionality reduction is simply, the process of reducing the dimension of your feature set.

Get Sachin M Awati’s stories in your inbox

Join Medium for free to get updates from this writer.

Advantages of Dimensionality Reduction:

  1. Less misleading data means model accuracy improves.
  2. Fewer dimensions mean less computing. Less data means that algorithms train faster.
  3. Less data means less storage space required.
  4. Removes redundant features and noise.
  5. Dimensionality Reduction helps us visualize the data on 2D plots or 3D plots.

Drawbacks of Dimensionality Reduction are:

  1. Some information is lost, possibly degrading the performance of subsequent training algorithms.
  2. It can be computationally intensive.
  3. Transformed features are often hard to interpret.
  4. It makes the independent variables less interpretable.

3. Explain Principal Component Analysis, assumptions, equations.

Principal Component Analysis (PCA) is an unsupervised dimensionality reduction algorithm. It identifies the hyperplane that lies closest to the data, and then it projects the data onto it preserving the variance.

Press enter or click to view image in full size

Press enter or click to view image in full size

Here is a result of the projection of a dataset onto each of these axes. As you can see, the projection onto the solid line preserves the maximum variance, while the projection onto the dotted line preserves very little variance, and the projection onto the dashed line preserves an intermediate amount of variance.

PCA selects the axis which preserves maximum variance in the training set. PCA finds as many axes as the number of dimensions such that every axis is orthogonal to each other.

Equations:

  1. First, we calculate the covariance matrix

2. Decompose the covariance matrix and find the eigenvalues and corresponding eigenvectors

Press enter or click to view image in full size

Press enter or click to view image in full size

Eigen Value and Eigen Vector

Decompose the Covariance Matrix

3. We arrange the eigenvalues as follows

Each Eigenvalue corresponds to a principal component. The highest eigenvalue corresponds to the 1st Principal Component and so on.

Assumptions:

  • There needs to be a linear relationship between all variables. The reason for this assumption is that a PCA is based on Pearson correlation coefficients, and as such, there needs to be a linear relationship between the variables
  • You should have sampling adequacy, which simply means that for PCA to produce a reliable result, large enough sample sizes are required.
  • Your data should be suitable for data reduction. Effectively, you need to have adequate correlations between the variables to be reduced to a smaller number of components.
  • There should be no significant outliers.

4. Can PCA be used to reduce the dimensionality of a highly nonlinear dataset?

PCA can be used to significantly reduce the dimensionality of most datasets, even if they are highly nonlinear because it can at least get rid of useless dimensions. However, if there are no useless dimensions, reducing dimensionality with PCA will lose too much information.

5. Limitations of PCA?

  1. Doesn’t work well for non linearly correlated data.

Here the data points are not linearly correlated. PCA won’t work well.

2. PCA always finds orthogonal principal components. Sometimes, our data demands non-orthogonal principal components to represent the data.

The green colour vectors are principal components. But, the actual maximum variance directions are blue colour vectors. Standard PCA fails to find that vectors.

3. PCA always considered the low variance components in the data as noise and recommend us to throw away those components. But, sometimes those components play a major role in a supervised learning task.

4. If the variables are correlated, PCA can achieve dimension reduction. If not, PCA just orders them according to their variances

6. Is rotation necessary in PCA? If yes, Why? What will happen if you don’t rotate the components?

Yes, rotation (orthogonal) is necessary to account the maximum variance of the training set. If we don’t rotate the components, the effect of PCA will diminish and we’ll have to select more number of components to explain variance in the training set.

7. Is it important to standardize before applying PCA?

PCA finds new directions based on the covariance matrix of original variables. Since the covariance matrix is sensitive to the standardization of variables. Usually, we do standardization to assign equal weights to all the variables. If we use features of different scales, we get misleading directions. But, it is not necessary to standardize the variables, if all the variables are on the same scale.

8. Should one remove highly correlated variables before doing PCA?

No, PCA loads out all highly correlated variables on the same Principal Component(Eigenvector), not different ones.

9. What will happen when eigenvalues are roughly equal?

If all eigenvectors are same then PCA won’t be able to select the principal components because in that case, all principal components are equal.

10. How can you evaluate the performance of a dimensionality reduction algorithm on your dataset?

Intuitively, a dimensionality reduction algorithm performs well if it eliminates a lot of dimensions from the dataset without losing too much information. Alternatively, if you are using dimensionality reduction as a preprocessing step before another Machine Learning algorithm (e.g., a Random Forest classifier), then you can simply measure the performance of that second algorithm; if dimensionality reduction did not lose too much information, then the algorithm should perform just as well as when using the original dataset.

Hope you liked the content and were able to test your knowledge.
Please feel free to contact me on https://www.linkedin.com/in/sachin-awati-7b886b1a1?utm_source=share_via&utm_content=profile&utm_medium=member_android

Feel free to add more questions in the comments.

--

--