An important machine learning method for dimensionality reduction is called Principal Component Analysis.

It is a method that uses simple matrix operations from linear algebra and statistics to calculate a projection of the original data into the same number or fewer dimensions.

In this tutorial, you will discover the Principal Component Analysis machine learning method for dimensionality reduction and how to implement it from scratch in Python.

After completing this tutorial, you will know:

- The procedure for calculating the Principal Component Analysis and how to choose principal components.
- How to calculate the Principal Component Analysis from scratch in NumPy.
- How to calculate the Principal Component Analysis for reuse on more data in scikit-learn.

Let’s get started.

**Update Apr/2018**: Fixed typo in the explaination of the sklearn PCA attributes. Thanks kris.

## Tutorial Overview

This tutorial is divided into 3 parts; they are:

- Principal Component Analysis
- Manually Calculate Principal Component Analysis
- Reusable Principal Component Analysis

### Need help with Linear Algebra for Machine Learning?

Take my free 7-day email crash course now (with sample code).

Click to sign-up and also get a free PDF Ebook version of the course.

## Principal Component Analysis

Principal Component Analysis, or PCA for short, is a method for reducing the dimensionality of data.

It can be thought of as a projection method where data with m-columns (features) is projected into a subspace with m or fewer columns, whilst retaining the essence of the original data.

The PCA method can be described and implemented using the tools of linear algebra.

PCA is an operation applied to a dataset, represented by an n x m matrix A that results in a projection of A which we will call B. Let’s walk through the steps of this operation.

1 2 3 4 5 |
a11, a12 A = (a21, a22) a31, a32 B = PCA(A) |

The first step is to calculate the mean values of each column.

1 |
M = mean(A) |

or

1 2 |
(a11 + a21 + a31) / 3 M(m11, m12) = (a12 + a22 + a32) / 3 |

Next, we need to center the values in each column by subtracting the mean column value.

1 |
C = A - M |

The next step is to calculate the covariance matrix of the centered matrix C.

Correlation is a normalized measure of the amount and direction (positive or negative) that two columns change together. Covariance is a generalized and unnormalized version of correlation across multiple columns. A covariance matrix is a calculation of covariance of a given matrix with covariance scores for every column with every other column, including itself.

1 |
V = cov(C) |

Finally, we calculate the eigendecomposition of the covariance matrix V. This results in a list of eigenvalues and a list of eigenvectors.

1 |
values, vectors = eig(V) |

The eigenvectors represent the directions or components for the reduced subspace of B, whereas the eigenvalues represent the magnitudes for the directions. For more on this topic, see the post:

The eigenvectors can be sorted by the eigenvalues in descending order to provide a ranking of the components or axes of the new subspace for A.

If all eigenvalues have a similar value, then we know that the existing representation may already be reasonably compressed or dense and that the projection may offer little. If there are eigenvalues close to zero, they represent components or axes of B that may be discarded.

A total of m or less components must be selected to comprise the chosen subspace. Ideally, we would select k eigenvectors, called principal components, that have the k largest eigenvalues.

1 |
B = select(values, vectors) |

Other matrix decomposition methods can be used such as Singular-Value Decomposition, or SVD. As such, generally the values are referred to as singular values and the vectors of the subspace are referred to as principal components.

Once chosen, data can be projected into the subspace via matrix multiplication.

1 |
P = B^T . A |

Where A is the original data that we wish to project, B^T is the transpose of the chosen principal components and P is the projection of A.

This is called the covariance method for calculating the PCA, although there are alternative ways to to calculate it.

## Manually Calculate Principal Component Analysis

There is no pca() function in NumPy, but we can easily calculate the Principal Component Analysis step-by-step using NumPy functions.

The example below defines a small 3×2 matrix, centers the data in the matrix, calculates the covariance matrix of the centered data, and then the eigendecomposition of the covariance matrix. The eigenvectors and eigenvalues are taken as the principal components and singular values and used to project the original data.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from numpy import array from numpy import mean from numpy import cov from numpy.linalg import eig # define a matrix A = array([[1, 2], [3, 4], [5, 6]]) print(A) # calculate the mean of each column M = mean(A.T, axis=1) print(M) # center columns by subtracting column means C = A - M print(C) # calculate covariance matrix of centered matrix V = cov(C.T) print(V) # eigendecomposition of covariance matrix values, vectors = eig(V) print(vectors) print(values) # project data P = vectors.T.dot(C.T) print(P.T) |

Running the example first prints the original matrix, then the eigenvectors and eigenvalues of the centered covariance matrix, followed finally by the projection of the original matrix.

Interestingly, we can see that only the first eigenvector is required, suggesting that we could project our 3×2 matrix onto a 3×1 matrix with little loss.

1 2 3 4 5 6 7 8 9 10 11 12 |
[[1 2] [3 4] [5 6]] [[ 0.70710678 -0.70710678] [ 0.70710678 0.70710678]] [ 8. 0.] [[-2.82842712 0. ] [ 0. 0. ] [ 2.82842712 0. ]] |

## Reusable Principal Component Analysis

We can calculate a Principal Component Analysis on a dataset using the PCA() class in the scikit-learn library. The benefit of this approach is that once the projection is calculated, it can be applied to new data again and again quite easily.

When creating the class, the number of components can be specified as a parameter.

The class is first fit on a dataset by calling the fit() function, and then the original dataset or other data can be projected into a subspace with the chosen number of dimensions by calling the transform() function.

Once fit, the eigenvalues and principal components can be accessed on the PCA class via the *explained_variance_* and *components_* attributes.

The example below demonstrates using this class by first creating an instance, fitting it on a 3×2 matrix, accessing the values and vectors of the projection, and transforming the original data.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Principal Component Analysis from numpy import array from sklearn.decomposition import PCA # define a matrix A = array([[1, 2], [3, 4], [5, 6]]) print(A) # create the PCA instance pca = PCA(2) # fit on data pca.fit(A) # access values and vectors print(pca.components_) print(pca.explained_variance_) # transform data B = pca.transform(A) print(B) |

Running the example first prints the 3×2 data matrix, then the principal components and values, followed by the projection of the original matrix.

We can see, that with some very minor floating point rounding that we achieve the same principal components, singular values, and projection as in the previous example.

1 2 3 4 5 6 7 8 9 10 11 12 |
[[1 2] [3 4] [5 6]] [[ 0.70710678 0.70710678] [ 0.70710678 -0.70710678]] [ 8.00000000e+00 2.25080839e-33] [[ -2.82842712e+00 2.22044605e-16] [ 0.00000000e+00 0.00000000e+00] [ 2.82842712e+00 -2.22044605e-16]] |

## Extensions

This section lists some ideas for extending the tutorial that you may wish to explore.

- Re-run the examples with your own small contrived matrix values.
- Load a dataset and calculate the PCA on it and compare the results from the two methods.
- Search for and locate 10 examples where PCA has been used in machine learning papers.

If you explore any of these extensions, I’d love to know.

## Further Reading

This section provides more resources on the topic if you are looking to go deeper.

### Books

- Section 7.3 Principal Component Analysis (PCA by the SVD), Introduction to Linear Algebra, Fifth Edition, 2016.
- Section 2.12 Example: Principal Components Analysis, Deep Learning, 2016.

### API

### Articles

### Tutorials

- Principal Component Analysis with numpy, 2011.
- PCA and image compression with numpy, 2011.
- Implementing a Principal Component Analysis (PCA), 2014.

## Summary

In this tutorial, you discovered the Principal Component Analysis machine learning method for dimensionality reduction.

Specifically, you learned:

- The procedure for calculating the Principal Component Analysis and how to choose principal components.
- How to calculate the Principal Component Analysis from scratch in NumPy.
- How to calculate the Principal Component Analysis for reuse on more data in scikit-learn.

Do you have any questions?

Ask your questions in the comments below and I will do my best to answer.

Great article! I have been more of an R programmer in the past but have started to mess with Python. Python is a very versatile language and has started to draw my attention over the last few months.

Thanks John. I’m a big fan of Python myself these days.

Hello Jason, it’s very nice you are doing great work and I request you to make such a post on ISOMAP Dimensionality Reduction too..

Thanks for the suggestion.

Hello

Could you make a post on the Scree plot ?

Thank you

Thanks for the suggestion John.

Is there any direct relation between SVD and PCA since both perform dimentionality reduction?

Yes, they both can be used for dimensionality reduction.

Can we apply this for loaded file .csv format?

Yes.

Hi Jason, thanks for the great work you are doing with your blog!

I think the attribute “explained_variance_” of the PCA class from scikit-learn returns the eigenvalues and not the singular values as you mention in the section “Reusable Principal Component Analysis”. For the singular values there is another attribute which is “singular_values_”. Correct?

Also, “single values” should read “eigenvalues” in the sentence “…that we achieve the same principal components, singular values, and projection as in…”. Correct?

Correct, fixed.

Thanks for pointing out the typo!

Hello teacher. can help you me ? I wanna now how to implement a CPA?

What is CPA?

I´m sorry. I mean PCA

I think he has explained that in tutorial

Hi Jason,

Is there similar support for R or Matlab users? I’m trying to find a workshop / training in this area, if you could recommend anything that may help.

I don’t know sorry.

Great post!

I found a typo: In the initial explanation, it’s said:

P = B^T . A

In the manual calculation:

P = vectors.T.dot(C.T)

Which one is correct? The original A or the mean-centered C?

No typo, perhaps confusing explanation.

B == vectors (components)

A == C (centered data to project)

When I copy the code from section “Reusable Principal Component Analysis” and run in a Jupyter notebook with a Python3.6 kernel, I get a different output to what is shown on site.

The values for the Eigenvectors and Matrix B are the same but the polarity is not the same.

Any idea what is causing the mismatch?

[[1 2]

[3 4]

[5 6]]

[[ 0.70710678 0.70710678]

[-0.70710678 0.70710678]]

[8. 0.]

[[-2.82842712e+00 -2.22044605e-16]

[ 0.00000000e+00 0.00000000e+00]

[ 2.82842712e+00 2.22044605e-16]]

Yes, I address this in the post.

Minor differences and differences in sign can occur due to differences across platforms from multiple runs of the solver (used under the covers).

These matrix operations require converging a solution, they are not entirely deterministic like arithmetic, we are approximating.

Is there a way to store the PCA model after fit() during training and reuse that model later (by loading from saved file) on live data ?

Yes, you can save the elements to file in plain text or as pickled python objects.

Hi Jason

while computing the mean, shouldn’t the axis be equal to 0 rather than 1? since each dimension or feature must be averaged rather than each data point

I believe 0 would be row-wise, 1 is column wise

This is not from stratch at all. Calculating covariance matrix and eigenvalue decomposition of is it an important part, which this tutorial skips totally.

Thanks for the note, more on covar here:

https://machinelearningmastery.com/introduction-to-expected-value-variance-and-covariance/

More on eigendecomposition here:

https://machinelearningmastery.com/introduction-to-eigendecomposition-eigenvalues-and-eigenvectors/

Dude this is still not from scratch. You just explain what eigenvectors and eigenvalues are then use a toolbox to do the dirty work for you. Can you please explain the details of finding the eigenvectors?

Sure, see this post:

https://machinelearningmastery.com/introduction-to-eigendecomposition-eigenvalues-and-eigenvectors/

HI Jason,

I have a doubt , is there u are saying PCA with eigenvector and PCA with svd both are different ? or i understood wrong,

secondly can we use together ?

PCA and SVD are different.

Hi Jason,

Can you extend PCA and Hotelling’s T^2 for confidence interval in python.

Thanks,

Venkat

Sorry, what are you referring to exactly?

Hi Jason, I found extracting top PCA explaining 90% of the variance, boosting to a large degree my h2o.deeplearning model to a +99% overall accuracy, AUC, tpr and npr. It is so good once the model is applied to my the test set to look unreal (basically only one misprediction out of 1k+ observations in my confusion matrix). I am not versant with the orthogonal transformations underlying PCA, but I was wondering: would PCA be the cause of overfitting on my data set? How is it possible to get to such an amazing result? How reliable would be my model over future and unseen observations?

Thanks

Yes, the transform must be calculated on the train dataset only, then applied to train and test sets.

I see waht you mean. Thanks!

Could you please explain more about pca.fit() and pca.transform what exactly is happening when we call these two ?

Great question, fit is converging on a solution, e.g. finding the eigenvectors and eigenvalues.

It might help to check the API documentation.

What is the difference between Split Zone design and Split Plot design?

I have not heard these terms before, sorry.

What is the content?

Amazing description Sir, but in the manual computation of PCA I’m having a different dataset having 1140 eigen vectors and want only 100 of them corresponding to their eigen values. So, how to choose the components and form the feature vector.

Perhaps choose the 100 largest?