# Principal Component Analysis

### Introduction <a href="#introduction" id="introduction"></a>

The sheer size of data in the modern age is not only a challenge for computer hardware but also a main bottleneck for the performance of many machine learning algorithms. The main goal of a PCA analysis is to identify patterns in data; PCA aims to detect the correlation between variables. If a strong correlation between variables exists, the attempt to reduce the dimensionality only makes sense. In a nutshell, this is what PCA is all about: Finding the directions of maximum variance in high-dimensional data and project it onto a smaller dimensional subspace while retaining most of the information.

### PCA and Dimensionality Reduction

Often, the desired goal is to reduce the dimensions of a **d**-dimensional dataset by projecting it onto a (**k**)-dimensional subspace (where **k\<d**) in order to increase the computational efficiency while retaining most of the information. An important question is “what is the size of **k** that represents the data ‘well’?”

Later, we will compute eigenvectors (the principal components) of a dataset and collect them in a projection matrix. Each of those eigenvectors is associated with an eigenvalue which can be interpreted as the “length” or “magnitude” of the corresponding eigenvector. If some eigenvalues have a significantly larger magnitude than others, then the reduction of the dataset via PCA onto a smaller dimensional subspace by dropping the “less informative” eigenpairs is reasonable.

### A Summary of the PCA Approach

* Standardize the data.
* Obtain the Eigenvectors and Eigenvalues from the covariance matrix or correlation matrix, or perform Singular Value Decomposition.
* Sort eigenvalues in descending order and choose the **k** eigenvectors that correspond to the k-largest eigenvalues where **k** is the number of dimensions of the new feature subspace (**k≤d**).
* Construct the projection matrix **W** from the selected **k** eigenvectors.
* Transform the original dataset **X** via **W** to obtain a **k**-dimensional feature subspace **Y**.

source: [https://sebastianraschka.com/Articles/2015\_pca\_in\_3\_steps.html](https://sebastianraschka.com/Articles/2015_pca_in_3_steps.html)

Great ELI5 and different level of explanations:&#x20;

[https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues](https://stats.stackexchange.com/questions/2691/making-sense-of-principal-component-analysis-eigenvectors-eigenvalues)

**Other intro - diff source**

we come to Principal Components Analysis (PCA). What is it? It is a way of identifying patterns in data, and expressing the data in such a way as to highlight their similarities and differences. Since patterns in data can be hard to find in data of high dimension, where the luxury of graphical representation is not available, PCA is a powerful tool for analysing data. The other main advantage of PCA is that once you have found these patterns in the data, and you compress the data, ie. by reducing the number of dimensions, without much loss of information. This technique used in image compression, as we will see in a later section.

Eigen decomposition

Many mathematical objects can be understood better by breaking them into constituent parts, or ﬁnding some properties of them that are universal, not caused by the way we choose to represent them.For example, integers can be decomposed into prime factors. The way were present the number 12 will change depending on whether we write it in base tenor in binary, but it will always be true that 12 = 2×2×3. From this representation we can conclude useful properties, for example, that 12 is not divisible by 5, andthat any integer multiple of 12 will be divisible by 3.Much as we can discover something about the true nature of an integer by decomposing it into prime factors, we can also decompose matrices in ways that show us information about their functional properties that is not obvious from there presentation of the matrix as an array of elements.One of the most widely used kinds of matrix decomposition is called eigen-decomposition, in which we decompose a matrix into a set of eigenvectors and eigenvalues. An eigenvector of a square matrix A is a nonzero vector v such that multiplication by A alters only the scale of v:

Av = λv.

#### Latent Features <a href="#latent-features" id="latent-features"></a>

Latent features are features that aren't explicitly in your dataset.

In this example, you saw that the following features are all related to the latent feature **home size**:

1. lot size
2. number of rooms
3. floor plan size
4. size of garage
5. number of bedrooms
6. number of bathrooms

Similarly, the following features could be reduced to a single latent feature of **home neighborhood**:

1. local crime rate
2. number of schools in five miles
3. property tax rate
4. local median income
5. average air quality index
6. distance to highway

So even if our original dataset has the 12 features listed, we might be able to reduce this to only 2 latent features relating to the home size and home neighborhood.

## Reducing the Number of Features - Dimensionality Reduction <a href="#reducing-the-number-of-features-dimensionality-reduction" id="reducing-the-number-of-features-dimensionality-reduction"></a>

Our real estate example is great to help develop an understanding of feature reduction and latent features. But we have a smallish number of features in this example, so it's not clear why it's so necessary to reduce the number of features. And in this case it wouldn't actually be required - we could handle all six original features to create a model.

But the ["curse of dimensionality"](https://en.wikipedia.org/wiki/Curse_of_dimensionality) becomes more clear when we're grappling with large real-world datasets that might involve hundreds or thousands of features, and to effectively develop a model really requires us to reduce our number of dimensions.

### Two Approaches : Feature Selection and Feature Extraction <a href="#two-approaches-feature-selection-and-feature-extraction" id="two-approaches-feature-selection-and-feature-extraction"></a>

#### Feature Selection <a href="#feature-selection" id="feature-selection"></a>

Feature Selection involves finding a **subset** of the original features of your data that you determine are most relevant and useful. In the example image below, taken from the video, notice that "floor plan size" and "local crime rate" are features that we have selected as a subset of the original data.

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Ln38vw7B2MS_UMN7YIc%2F-Ln39ZASfc4ZACAJWHpM%2Fimage.png?alt=media\&token=29956bff-281c-4ad3-b047-807c37ccfd6f)



**Methods of Feature Selection:**

* **Filter methods** - Filtering approaches use a ranking or sorting algorithm to filter out those features that have less usefulness. Filter methods are based on discerning some inherent correlations among the feature data in unsupervised learning, or on correlations with the output variable in supervised settings. Filter methods are usually applied as a preprocessing step. Common tools for determining correlations in filter methods include: **Pearson's Correlation**, **Linear Discriminant Analysis (LDA)**, and **Analysis of Variance (ANOVA)**.
* **Wrapper methods** - Wrapper approaches generally select features by directly testing their impact on the performance of a model. The idea is to "wrap" this procedure around your algorithm, repeatedly calling the algorithm using different subsets of features, and measuring the performance of each model. Cross-validation is used across these multiple tests. The features that produce the best models are selected. Clearly this is a computationally expensive approach for finding the best performing subset of features, since they have to make a number of calls to the learning algorithm. Common examples of wrapper methods are: **Forward Search**, **Backward Search**, and **Recursive Feature Elimination**.

Scikit-learn has a [feature selection module](https://scikit-learn.org/stable/modules/feature_selection.html) that offers a variety of methods to improve model accuracy scores or to boost their performance on very high-dimensional datasets.

#### Feature Extraction <a href="#feature-extraction" id="feature-extraction"></a>

Feature Extraction involves extracting, or constructing, new features called **latent features**. In the example image below, taken from the video, "Size Feature" and "Neighborhood Quality Feature" are new latent features, extracted from the original input data

![](https://846345873-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LagOeJ2nL90MQERwhxy%2F-Ln38vw7B2MS_UMN7YIc%2F-Ln39hiRrfBcpnQivTvq%2Fimage.png?alt=media\&token=3b285409-f1f7-42a4-af8d-cc36e371ec2a)

e[xample of Feature Extraction](https://classroom.udacity.com/nanodegrees/nd025/parts/31adfeff-4f9d-4a6e-9856-629feab109be/modules/a4504b57-e14c-451e-ac40-70a2717efa98/lessons/dd43ee95-8403-4b8b-8574-ef5f41aa22bf/concepts/7e8947f4-f122-42bf-b058-96e21b0044e5)

**Methods of Feature Extraction**

Constructing latent features is exactly the goal of **Principal Component Analysis (PCA)**, which we'll explore throughout the rest of this lesson.

Other methods for accomplishing Feature Extraction include **Independent Component Analysis (ICA)**&#x61;nd **Random Projection**, which we will study in the following lesson.

#### Further Exploration <a href="#further-exploration" id="further-exploration"></a>

If you're interested in deeper study of these topics, here are a couple of helpful blog posts and a research paper:

* [https://www.analyticsvidhya.com/blog/2016/12/introduction-to-feature-selection-methods-with-an-example-or-how-to-select-the-right-variables/](https://www.analyticsvidhya.com/blog/2016/12/introduction-to-feature-selection-methods-with-an-example-or-how-to-select-the-right-variables/)
* [https://elitedatascience.com/dimensionality-reduction-algorithms](https://elitedatascience.com/dimensionality-reduction-algorithms)
* [http://www.ai.mit.edu/projects/jmlr/papers/volume3/guyon03a/source/old/guyon03a.pdf](http://www.ai.mit.edu/projects/jmlr/papers/volume3/guyon03a/source/old/guyon03a.pdf)

#### Principal Components <a href="#principal-components" id="principal-components"></a>

A few takeaways from this video:

1. An advantage of Feature Extraction over Feature Selection is that the latent features can be constructed to incorporate data from multiple features, and thus retain more information present in the various original inputs, than just losing that information by dropping many original inputs.
2. **Principal components** are linear combinations of the original features in a dataset that aim to retain the most information in the original data.
3. You can think of a **principal component** in the same way that you think about a **latent feature**.

The general approach to this problem of high-dimensional datasets is to search for a **projection** of the data onto a smaller number of features which preserves the information as much as possible.

We'll take a closer look in the rest of this lesson.



#### Principal Component Properties <a href="#principal-component-properties" id="principal-component-properties"></a>

There are two main properties of **principal components**:

1. **They retain the most amount of information in the dataset.** In this video, you saw that retaining the most information in the dataset meant finding a line that reduced the distances of the points to the component across all the points (same as in regression!).
2. **The created components are orthogonal to one another.** So far we have been mostly focused on what the first component of a dataset would look like. However, when there are many components, the additional components will all be orthogonal to one another. Depending on how the components are used, there are benefits to having orthogonal components. In regression, we often would like independent features, so using the components in regression now guarantees this.

[This is a great post answering a number of common questions on PCA.](https://stats.stackexchange.com/questions/110508/questions-on-pca-when-are-pcs-independent-why-is-pca-sensitive-to-scaling-why)

#### Where is PCA Used? <a href="#where-is-pca-used" id="where-is-pca-used"></a>

In general, PCA is used to **reduce the dimensionality of your data**. Here are links to some specific use cases beyond what you covered in this lesson:

1. PCA for [microarray data](https://www.ncbi.nlm.nih.gov/pmc/articles/PMC2669932/).
2. PCA for [anomaly detection](https://arxiv.org/pdf/1801.01571.pdf).
3. PCA for [time series data](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.853.2380\&rep=rep1\&type=pdf).

If you ever feel overwhelmed by the amount of data you have, you can look to PCA to reduce the size of your dataset, while still retaining the maximum amount of information (though this does often come at the cost of reducing your data interpretability).
