Principal Component Analysis (PCA) is an exploratory tool designed by Karl Pearson in 1901 to identify unknown trends in a multidimensional data set. It involves a mathematical procedure that transforms a number of possibly correlated variables into a smaller number of uncorrelated variables called principal components.

- Download source code.
- Download sample application.
- Browse source code online.

### Foreword

Before you read this article, please keep in mind that it was written before the Accord.NET Framework was created and became popular. As such, if you would like to do Principal Component Analysis in your projects, **download the accord-net framework from NuGet** and either follow the starting guide or download the PCA sample application from the sample gallery in order to get up and running quickly with the framework.

## Introduction

PCA essentially rotates the set of points around their mean in order to align with the first few principal components. This moves as much of the variance as possible (using a linear transformation) into the first few dimensions. The values in the remaining dimensions, therefore, tend to be highly correlated and may be dropped with minimal loss of information. Please note that the signs of the columns of the rotation matrix are arbitrary, and so may differ between different programs for PCA.

For a more complete explanation for PCA, please visit Lindsay Smith excellent Tutorial On Principal Component Analysis (2002).

#### Accord.NET Framework

This new library, which I called **Accord.NET**, was initially intended to extend the AForge.NET Framework through the addition of new features such as Principal Component Analysis, numerical decompositions, and a few other mathematical transformations and tools. However, the library I created grew larger than the original framework I was trying to extend. In a few months, both libraries will merge under Accord.NET. (**Update** April 2015)

## Design decisions

As people who want to use PCA in their projects usually already have their own Matrix classes definitions, I decided to avoid using custom Matrix and Vector classes in order to make the code more flexible. I also tried to avoid dependencies on other methods whenever possible, to make the code very independent. I think this also made the code simpler to understand.

The code is divided into two projects:

**Accord.Math**, which provides mathematical tools, decompositions and transformations, and**Accord.Statistics**, which provides the statistical analysis, statistical tools and visualizations.

Both of them depends on the AForge.NET core. Also, their internal structure and organization tries to mimic AForge’s wherever possible.

The given source code doesn’t include the full source of the Accord Framework, which remains as a test bed for new features I’d like to see in AForge.NET. Rather, it includes only limited portions of the code to support PCA. It also contains code for Kernel Principal Component Analysis, as both share the same framework. Please be sure to look for the correct project when testing.

## Code overview

Below is the main code behind PCA.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/// <summary>Computes the Principal Component Analysis algorithm.</summary> public void Compute() { int rows = sourceMatrix.GetLength(0); int cols = sourceMatrix.GetLength(1); // Create a new matrix to work upon double[,] matrix = new double[rows, cols]; // Prepare the data, storing it in the new matrix. if (this.analysisMethod == AnalysisMethod.Correlation) { for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) // subtract mean and divide by standard deviation (convert to Z Scores) matrix[i, j] = (sourceMatrix[i, j] - columnMeans[j]) / columnStdDev[j]; } else { for (int i = 0; i < rows; i++) for (int j = 0; j < cols; j++) // Just center the data around the mean. Will have no effect if the // data is already centered (the mean will be zero). matrix[i, j] = (sourceMatrix[i, j] - columnMeans[j]); } // Perform the Singular Value Decomposition (SVD) of the matrix SingularValueDecomposition singularDecomposition = new SingularValueDecomposition(matrix); singularValues = singularDecomposition.Diagonal; // Eigen values are the square of the singular values for (int i = 0; i < singularValues.Length; i++) { eigenValues[i] = singularValues[i] * singularValues[i]; } // The principal components of 'Source' are the eigenvectors of Cov(Source). Thus if we // calculate the SVD of 'matrix' (which is Source standardized), the columns of matrix V // (right side of SVD) will be the principal components of Source. // The right singular vectors contains the principal components of the data matrix this.eigenVectors = singularDecomposition.RightSingularVectors; // The left singular vectors contains the scores of the principal components this.resultMatrix = singularDecomposition.LeftSingularVectors; // Calculate proportions double sum = 0; for (int i = 0; i < eigenValues.Length; i++) sum += eigenValues[i]; sum = (1.0 / sum); for (int i = 0; i < eigenValues.Length; i++) componentProportions[i] = eigenValues[i] * sum; // Calculate cumulative proportions this.componentCumulative[0] = this.componentProportions[0]; for (int i = 1; i < this.componentCumulative.Length; i++) { this.componentCumulative[i] = this.componentCumulative[i - 1] + this.componentProportions[i]; } // Creates the object-oriented structure to hold the principal components PrincipalComponent[] components = new PrincipalComponent[singularValues.Length]; for (int i = 0; i < components.Length; i++) { components[i] = new PrincipalComponent(this, i); } this.componentCollection = new PrincipalComponentCollection(components); } |

## Using the code

To perform a simple analysis, you can simple instantiate a new **PrincipalComponentAnalysis** object passing your data and call its *Compute* method to compute the model. Then you can simply call the *Transform* method to project the data into the principal component space.

A sample sample code demonstrating its usage is presented below.

1 2 3 4 5 6 7 8 9 10 |
// Creates the Principal Component Analysis of the given source PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis(sourceMatrix, PrincipalComponentAnalysis.AnalysisMethod.Correlation); // Compute the Principal Component Analysis pca.Compute(); // Creates a projection considering 80% of the information double[,] components = pca.Transform(sourceMatrix, 0.8f, true); |

## Example application

To demonstrate the use of PCA, I created a simple Windows Forms Application which performs simple statistical analysis and PCA transformations.

**Note:** The principal components are not unique because the Singular Value Decomposition is not unique. Also the signs of the columns of the rotation matrix are arbitrary, and so may differ between different programs for PCA.

Together with the demo application comes an Excel spreadsheet containing several data examples. The first example is the same used by Lindsay on his Tutorial on Principal Component Analysis. The others include Gaussian data, uncorrelated data and linear combinations of Gaussian data to further exemplify the analysis.

I hope this code and example can be useful! If you have any comments about the code or the article, please let me know.

## See also

### A Tutorial On Principal Component Analysis with the Accord.NET Framework

This is a tutorial for those who are interested in learning how PCA works and how each step of Lindsay’s tutorial can be computed in the Accord.NET Framework, in C#.

### Kernel Principal Component Analysis in C#

This is the non-linear extension of Principal Component Analysis. While linear PCA is restricted to rotating or scaling the data, kernel PCA can do arbitrary transformations (such as folding and twisting the data and the space that contains the data).

just what i needed !

but unfortunately I can’t download your source code from http://www.comp.ufscar.br.

DNS kind of problem… Any mirror ?

Sorry, we just had an almost national-wide power outage here in Brazil. The link is working now.

What licence is used for this code?

I’ve updated the sources to include license information.

your project is really very good but i have problems,when i open the program, it has difficulties in identifing the “statistics” and “samples” characterizing them as unavailable. Thus some properties in solution cannot be read.

Thank you in advance

why the eigenvalue is so big?

I suspect this is because the PCA is being computed using the SVD.

In SVD, singular values are equal to the square root of the eigenvalues. So the singular values are being squared (thus becoming large) to give the eigenvalues. Those eigenvalues, however, may not be the actual eigenvalues that would be obtained using a Eigendecomposition because the SVD implementation used automatically normalizes its singular values.

As the eigenvalues are used only to compute the amount of variance explained by each component, the important thing to note is that their ratio is preserved.

Cesar

thanks alot for ur efforts, it’s really great

i have a question, if i wanna get the principal components for an image

i mean my target is to classify faces and non faces, so i need to get the pca to the face images

how can i pass my data which is image in my case to the pca object?

thanks in advance

Hi,

You can always transform your image into a single vector and then pass it to PCA as you would pass any other input vector.

For example, if you have a 320×240 image, you can create a vector of 320*240=76800 positions and then copy the image pixel by pixel to this vector, in any order you wish, as long as you are consistent using the same ordering with all your images.

In the page http://www.face-rec.org/algorithms/ you can find more information about face recognition algorithms, including ones using PCA.

Best regards,

César

Really thanks

great work, and it works with me well

just another small question

are there any restrictions about type or size of the images entered to pca.compute()?

Thanks alot and sorry for too many questions.

Well, I have not tested the memory limits of this implementation, but in theory it should work as long as you have enough memory to accommodate those matrices during computation.

About the type of the image, it all depends on how you are going to create your input vector. But typically only gray-scale images are used.

Best regards,

César

Dear César,

Thank you very much for making this valuable code available to the public.

I have a problem using your code:

My data is too large: about 500,000 rows and 118 columns. When I give this matrix to the DescriptiveAnalysis(sourceMatrix, sourceColumns) method, the exception “System.OutOfMemoryException” is thrown.

Is there any way that instead giving a matrix, I can pass a comma-seperated file containing the info to the methods?

Hi Mohammed,

Have you tried running the method on a 64 bit system? Perhaps it should work. Besides, the error happens in the DescriptiveAnalysis class, perhaps it may work if you comment this portion of the code and use only the PrincipalComponentAnalysis classes.

Best regards,

César

Hello! This code is really helpful but my problem is that I dont know how to use this. My project is about face recognition and base on my researches, PCA is paired with neural network on most of the face recognition systems. How will I connect your code with my neural network? What is really the output of PCA that will be the input of the neural network? Are eigenvectors values? I have read that the output of PCA are eigenvectors? I really cant understand PCA. I wish you could help me. Thank you in advance!

Hi Ligemm,

PCA can be seen as a linear transformation. Being a transformation, what it does is project your data into another space. The PCA output you are looking for is the projection of your original data into this space, which in the case of PCA, will be a space where your variables are (hopefully) uncorrelated.

The eigenvectors found by the analysis will form the basis for this new space, and the eigenvalues can be used to measure the importance of each of the vectors. If you discard the less important eigenvectors before performing the projection, then you can also perform dimensionality reduction in the process.

By the way, I have used PCA as a preprocessing step for ANNs too. If you wish, please take a look on the images on this poster, they might help to understand how PCA can be used in this scenario.

Regards,

César

Dear César,

I reduced the number of samples to 50, but still the number of features (118) seems to be too much for your code. The maximum number of features the code can handle seems to be 46 for my data, otherwise it takes forever to finish pca.Compute().

Do you have any suggestions?

Thanks.

Hi Mohammad,

Well, can I have a look in your data? If Compute is taking forever (and is not throwing any exceptions) then this may be a bug. If you could provide an excerpt of your data (perhaps the 50 samples with 118 columns you mentioned) it would be great!

Best regards,

César

Hello, I think there is s small bug in PCA implementation. When you use matrix where column number is higher then row number, then it does not work correctly (transform method returns all zeros).

I think solution is in method PCA.Compute where turning all params to “true” value helps.

SingularValueDecomposition svd = new SingularValueDecomposition(matrix, true, true, true);

Hi Anonymous,

Thanks, you are correct about that. The latest version of PCA in the development branch of Accord.NET does indeed uses those parameters when creating the SingularValueDecomposition, but I forgot to update the code available here.

Thanks again!

César

Yes, Cesar and Anonymous, I have noticed this for my data, as well (I have a matrix where the column nr is higher than the row nr and I get zeros from the transform method).

I am using the latest Accord.Net framework (version 2.1.4) and get this problem… How do I update this myself?

BTW – THANK YOU VERY MUCH for providing us with this great framework, Cesar!

Greetings from Monika

Hi Monika,

I am going to release an updated version of the framework soon. If you wish, you can change the aforementioned line in the PrincipalComponentAnalysis.cs class to:

SingularValueDecomposition svd = new SingularValueDecomposition(matrix, true, true, true);

If you have installed the framework using the executable installer, the source code will be available in the installation folder. However, if you can wait a little, I will try to release a new version of the framework this week.

Best regards,

César

Ok, I’ll wait.

Dear César,

As I encountered the same problem with column count > row count, I wanted to ask, if the updated version of the framework is already online and maybe within the version 2.1.4?

Thanks again for your efforts and regards,

MSA

Hi MSA2000, Monika,

A new version of the Accord.NET Framework (v2.1.5) has just been released. The bug has been fixed in this release. There are many additions in this release as well, which has delayed the release a little. Those additions, corrections and enhancements are detailed in the release notes.

Best regards,

César

Dear César,

thanks you very much This code is really helpful

i use PCA for my examination but how put jacobian iterasion for this code to get eigen vektor And eigen value

thanks…

Dear César,

I’m all of a sudden stuck with a problem concerning the dimensions of input data and pca / kda and I’d like to ask, if you have encountered similar behaviour.

Is it only possible to set the principal components count maximum to rows count of input data? I always receive an exception “Index was outside the bounds of the array” during the first call of “.Transform(matrix,pcacomponents)” after “.Compute()” with Analysis method “Center”.

Hope you can help me.

Thanks in advance.

MSA2000

Hi MSA2000,

In PCA this shouldn’t be a problem. I believe I had corrected this in recent versions of the Accord.NET Framework. However, for KPCA, the limit is indeed the number of rows in your data. KPCA works by performing PCA over the Kernel matrix. The kernel matrix have the same dimensions as the number of rows in your data, so I guess it is not possible to generate more components than rows.

Best regards,

César

the SVD computation never ends with my data. For example if i pass data 51×400 it works, if i pass 52×400 stops working 😡 i did some debugging, and found out that the “p” variable in SingularValueDecomposition class is not decreasing its value in switch (kase) statement (only when kase==4), what is never reached 😡

Hi there,

Sorry for the absolutely incredible very late reply. Did you fix your issue? If you are still encountering this issue, could you consider sending a sample of your data to the Accord.NET issue tracker?

Best regards,

César

hello,

what to do for categorical data? i mean does it work?

Hi there,

Well, sort of. When we apply PCA to categorical data, we could indeed obtain a lower-dimensional representation of the data. Please see page 339 from the book “Principal Component Analysis”, by I.T. Jolliffe. This particular page is available in Google Books. As it can be seen, the author states that “For data in which all variables are binary, Gower (1966) points out that using PCA does provide a plausible low-dimensional representation.”

However, a better approach would be to use Multiple Correspondence Analysis instead.

Best regards,

Cesar

Hi César,

I discovered a bug in the PCA adjust function. If any of the standard deviations are zero this will return NaN and propagate through SVD causing the process to fail:

matrix[i, j] = (m[i, j] – columnMeans[j]) / columnStdDev[j];

I’ve updated it to do an addition check now for 0 standard deviations and divide by epsilon if found.

matrix[i, j] = (m[i, j] – columnMeans[j]) / Double.Epsilon;

I’m also a little confused by the number of eigenvalue generated when using data with more dimensions than samples.

I have a dataset consisting of 5 samples (rows) with 480 dimensions (columns). The SVD algorithm returns 480 eigenvectors, however, it only returns 6 eigenvalues. I was under the impression that there should be one eigenvalue for every eigenvector. I figured the number of Principal Components would correspond to the dimensions such that it would be possible to analyze the dimensions in their entirety. I’m still new to PCA and was wondering if you could explain.

Thanks,

Eric

Hi Eric,

Thanks for reporting the issue. This has already been fixed a while ago in the main Accord.NET sources. If a variable has zero standard deviation then it should be removed from the the data set, since it will have no impact in the analysis.

About your second question, I have written a tutorial about using PCA through SVD. However, it is still somewhat unfinished, so if you wish I can send you a partial version by email.

Best regards,

Cesar

Thanks César,

I’ll update to the latest version of Accord.NET. That would be great if you could email me your tutorial. I’ll send you an email so you’ll have my address.

Cheers,

Eric

hi,

this is ananth. i downloaded the sample from your website and could not able to run the code in visual studio 2010. its saying that the accord.statistics.dll and accord.statistics.controls.dll.?? what could be the solution

When you do “using the code”, why is the sourceMatrix a double[,] in :

PrincipalComponentAnalysis pca = new PrincipalComponentAnalysis(sourceMatrix,

PrincipalComponentAnalysis.AnalysisMethod.Correlation);

and here is double [][]:

double[,] components = pca.Transform(sourceMatrix, 0.8f, true);

?

Hi Miguel,

This had to do on how the matrices were handled by the classifiers in the AForge.NET Framework (which I wanted to keep compatible). The internal matrix processing routines in Accord.NET (such as the matrix decompositions) can work faster on multidimensional matrices than on jagged ones. This is possible because they make heavy use of unsafe pointer operations.

However, as the classifiers often expect data as double[][], I included .Transform methods for both matrix types. The latest version of Accord.NET should process both types of matrices.

Hope it helps!

Best regards,

Cesar

Hi Cesar, I’m working on image denoising Using PCA-LPG approach, LPG=> Local Pixel Grouping. The Problem is how to group the pixels that are output from your PCA. Or better still if u can provide an insight into how I can apply ur code to denoising. ur will really be appreciated thanks.

Hi Mustapha,

If you wish to perform denoising, one way is to

1) Compute the analysis;

2) Project the image into principal component space using

someprincipal components (but not all of them);3) Revert the projection using the .Revert method.

The reversion hopefully acts as a denoising step. A similar and more powerful result can also be obtained using Kernel Principal Component Analysis. The PCA/KPCA sample application includes a demo of the reversion procedure in the “Project Reversion” tab.

Hope it helps!

Best regards,

Cesar

Hello,

Does anyone know how to implement PCA Whitening?

Regards,

Alex

Hi Cesar, yesterday i downloaded the framework and tried to implemt sample you have mentioned in the section “Using the code” above.

double[,] components = pca.Transform(sourceMatrix, 0.8f, true);

But there is no function named “Transform” which is taking 3 arguments.

Is it changed in new version. If so how can we specify “percentage of information(0.8f)”.

Thanks in advance.

Hi there,

Thanks for letting me know about the issue! Perhaps it got lost away in some refactoring. If you wish to determine how many components do you need in order to achieve a given percentage of information, you can use the GetNumberOfComponents method of the PrincipalComponentAnalysis class. Then you can use this number as the second argument of the Transform function.

Hope it helps!

Best regards,

Cesar

Hi Cesar,

I downloaded the source code, but it shows me warnings about missing some files. Did I made a mistake or what else could happen? Thanks Sona

For those missing files, in downloaded projects, please use Nuget. For example, search for “nuget zedgraph” will provide you with the latest ZedGraph package…

Hello Cesar,

I run the PCA and it works fine. However, I cannot find a way to save the principal component space in order to reuse it later.

In fact I need to put my soft in production and I do not want to provide the complete set of data. I just want to project the new sample into the principal component space.

Is there a way to do that ?

Best regards

Hey Cesar,

I tried to download the source code since I need to implement PCA on my data before visualizing it, but the link is broken. Can you please upload it again?

Kind regards