0% found this document useful (0 votes)
19 views8 pages

PCA: A Comprehensive Guide

The document provides a detailed overview of Principal Component Analysis (PCA), an unsupervised learning algorithm used for dimensionality reduction. It outlines the steps involved in PCA, including standardization, covariance matrix computation, and eigenvector/eigenvalue analysis, while discussing its advantages and disadvantages. Additionally, it includes a practical example and a Python implementation of PCA.

Uploaded by

edu.9rpm9.edu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views8 pages

PCA: A Comprehensive Guide

The document provides a detailed overview of Principal Component Analysis (PCA), an unsupervised learning algorithm used for dimensionality reduction. It outlines the steps involved in PCA, including standardization, covariance matrix computation, and eigenvector/eigenvalue analysis, while discussing its advantages and disadvantages. Additionally, it includes a practical example and a Python implementation of PCA.

Uploaded by

edu.9rpm9.edu
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

Get unlimited access to the best of Medium for less than $1/week. Become a member

Principal Component Analysis (PCA) In Depth


5 min read · Jul 20, 2024

Fraidoon Omarzai Follow

Listen Share More

This blog presents a comprehensive guide to the essentials of the PCA algorithm.

PCA stands for principal components analysis

Unsupervised learning algorithm

PCA is a dimensionality reduction technique

Require feature scaling

[Link] 1/15
18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

Filter noisy dataset

Improve model performance

Non-parametric statistic technique

How does PCA work?


→ PCA is a powerful tool for reducing the dimensionality of data while preserving its
essential information. The goal is to get the best principle components that capture
maximum variance.

Given a dataset with m observations and n features, PCA calculates the


covariance matrix of the data

It then finds the eigenvectors and eigenvalues of this covariance matrix

The eigenvectors represent the directions (principal components) of maximum


variance in the data, and the corresponding eigenvalues represent the
magnitude of this variance along each eigenvector

By sorting the eigenvectors based on their corresponding eigenvalues in


descending order, PCA identifies the most significant principal components

[Link] 2/15
18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

Steps In PCA:
1. Standardization: Scale the features to have a mean of 0 and a standard deviation
of 1 (optional but recommended).

2. Compute Covariance Matrix: This captures how the features vary together.

3. Compute Eigenvectors and Eigenvalues: Eigenvectors represent the directions


of greatest variance, and eigenvalues tell you how much variance each
eigenvector explains.

4. Select Principal Components: Choose the top k eigenvectors based on their


corresponding eigenvalues to form the new feature subspace.

5. Projection: Project the original data onto the new feature subspace formed by
the selected eigenvectors. Or project the original data points onto the new
principal component axes.

Practical Example
The table below displays scores on math, English, and art tests for 5 students

Step 1- Compute Covariance Matrix:

[Link] 3/15
18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

- cov(X,Y): covariance between X and Y

- X, Y: features X and Y

- x̄, ȳ: mean of features X and Y

Below is our matrix A which represents our above data in matrix format

When we compute the covariance matrix for matrix A, our output is:

A few points that can be noted here are:

- Shown in Blue along the diagonal, we see the variance of scores for each test

- The art test has the biggest variance (720)

[Link] 4/15
18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

- The English test, the smallest (360), so we can say that art test scores have more
variability than English test scores

- The covariance is displayed in black in the off-diagonal elements of the matrix A

- The covariance between math and English is positive (360)

- The covariance between math and art is positive (180)

- The covariance between English and art, however, is zero. This means there tends to be
no predictable relationship between the movement of English and art scores

Step 2- Compute Eigenvectors and Eigenvalues:

Let A be a square matrix, ν a vector, and λ a scalar that satisfies Aν = λν, then λ is
called the eigenvalue associated with eigenvector ν of A

[Link] 5/15
18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

Step 3- Sort the eigenvectors based on their corresponding eigenvalues in


descending order, and choose the top k eigenvectors based on their corresponding
Open in app
eigenvalues to form the new feature subspace.
Search
Note : The lowest eigenvalues bear the least information about the distribution of the
data, and those are the ones we want to drop.

[Link] 6/15
18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

Sorting eigenvalues in decreasing order

For this example, our goal is to reduce a 3-dimensional feature space to a 2-


dimensional feature space

So eigenvectors corresponding to two maximum eigenvalues are:

Step4- Projection or Transform the samples onto the new subspace:

In the last step, we use the 3x2 dimensional matrix W that we just computed to
transform our samples onto the new subspace via the equation y = W′ × x where
W′ is the transpose of the matrix W.

Pros:
Reduced complexity: Makes data easier to visualize, manage, and interpret by
reducing the number of features

Improved algorithm performance: Many machine learning algorithms struggle


with high-dimensional data. PCA reduces training time and improves the
accuracy of some algorithms by reducing irrelevant features

[Link] 7/15
18/07/2025, 01:21 Principal Component Analysis (PCA) In Depth | by Fraidoon Omarzai | Medium

Reduced redundancy: Eliminates redundant features, focusing on the unique


information each piece of data provides. This can be especially helpful when
dealing with datasets with a lot of correlated features

Cons:
Information loss: PCA discards information in the process of dimensionality
reduction. There’s a trade-off between the number of features retained and the
amount of information preserved

Assumes linearity: PCA works best when the relationships between features are
linear. It may not be effective if the data has complex non-linear relationships

Interpretability of new features: The principal components (PCs) created by


PCA can be difficult to interpret in the context of the original features

Python Implementation

import numpy as np
from [Link] import PCA

X = [Link]([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
pca = PCA(n_components=2)
[Link](X)
print(pca.explained_variance_ratio_)
print(pca.singular_values_)

Pca Analysis Ml Algorithm Dimensionality Reduction Data Science

Machine Learning

Follow

[Link] 8/15

Common questions

Powered by AI

The key steps in implementing PCA are: 1) Standardization: Scale the features to have a mean of 0 and a standard deviation of 1. While optional, it is highly recommended to ensure that PCA is not biased towards any particular feature based on its scale . 2) Compute the Covariance Matrix: This matrix captures how the features vary together, which is critical for understanding the directions of maximum variance . 3) Compute Eigenvectors and Eigenvalues: These represent directions (eigenvectors) of greatest variance and the magnitude of variance (eigenvalues) for each direction. This forms the basis of PCA as these will determine the most significant principal components . 4) Select Principal Components: Based on the eigenvalues, sort and select top eigenvectors to represent the data in a reduced dimension. These are chosen by picking the top k eigenvectors to form the new feature subspace . 5) Projection: Project the original data to the new feature subspace, effectively reducing its dimensionality while retaining as much variance as possible .

The selection of the number of principal components directly impacts both the quality and performance of data analysis. Selecting too few components may lead to significant loss of important data variance, negatively affecting model effectiveness and feature representation. Conversely, selecting too many components can result in retaining noise and unnecessary complexity, which might obscure meaningful patterns. The optimal number of components should reflect the balance between retaining sufficient variance and simplifying the data complexity, typically determined by analyzing the cumulative explained variance ratio .

The mathematical foundation of PCA is rooted in linear algebra and involves several key matrix operations. First, the dataset is mean-centered, and a covariance matrix is computed to understand how data features co-vary. Then, PCA performs an eigen decomposition of this covariance matrix to find its eigenvectors and eigenvalues. Each eigenvector corresponds to a principal component direction, and its corresponding eigenvalue provides the magnitude of variance explained along that direction. Matrix diagonalization and multiplication are used to project original data into a new hyperplane defined by the top k eigenvectors, facilitating dimensionality reduction .

The primary trade-off when using PCA for dimensionality reduction is between the amount of data variance retained and the complexity of the model. As PCA reduces the number of dimensions, it may discard information that characterizes the original variance. This loss of information can affect the interpretability of the new principal components and potentially oversimplify the data representation. Furthermore, PCA assumes linear relationships among features, which might not adequately capture the information if non-linear interactions exist .

PCA might not be suitable for dimensionality reduction in cases where data exhibits strong non-linear relationships, as PCA assumes linearity in the data structure. Additionally, when interpretability is crucial, PCA may not be appropriate, as the transformed features might not have meaningful interpretations in the context of original features. Datasets with heavily skewed distributions or variables differing vastly in scale without appropriate scaling prior to PCA are also less suited, as they can introduce bias into the analysis .

Feature scaling is an important pre-processing step in PCA because it ensures that all features contribute equally to the analysis. If the data is not scaled to the same range, features with larger values could disproportionately influence the results, as PCA is sensitive to the input data's scale. Scaling, often achieved by standardizing features to have a mean of 0 and a variance of 1, addresses this potential bias by ensuring that the PCA process considers the variance of each feature on equal terms, leading to a more unbiased identification of principal components .

PCA addresses data complexity and dimensionality by reducing the number of features in a dataset while preserving as much variance as possible. This reduction makes the data easier to visualize, manage, and interpret by focusing only on the most significant components, thus reducing redundancy by eliminating irrelevant and redundant features. It improves machine learning algorithm performance by reducing the training time and potentially increasing accuracy when dealing with datasets that have numerous correlated features .

PCA addresses the problem of noisy datasets by identifying and focusing on the directions of maximum variance, which typically reflect the true data structure more accurately than noise. Since noise often manifests as small variance directions, PCA effectively filters out such variance by discarding components with smaller eigenvalues that correspond to less significant directions. Consequently, by reducing dimensions to those principal components that capture the maximum variance, PCA inherently reduces noise in the dataset, leading to improved model performance .

Interpreting principal components can be challenging because the principal components do not directly correspond to the original features but are instead linear combinations of them. This transformation can obscure the logical relationship between the reduced components and the original variables, making it difficult to interpret what each component represents in real terms. Additionally, the new features are usually orthogonal, meaning they are not inherently meaningful outside of their statistical context. This complexity in interpretation might hinder the utility of PCA in explaining real-world phenomena within datasets .

In PCA, eigenvectors and eigenvalues are crucial for identifying the principal components. The covariance matrix of the dataset is used to compute these eigenvectors and eigenvalues. An eigenvector represents a direction in the feature space, and its corresponding eigenvalue indicates how much variance exists in that direction. By sorting eigenvectors in descending order of their eigenvalues, PCA determines which directions (principal components) best capture the data's variance. The components corresponding to the highest eigenvalues are selected as the principal components, allowing for an effective dimensionality reduction .

You might also like