# How to select the components that show the most variance in PCA

615 views (last 30 days)
Faraz on 27 Feb 2016
Commented: Warid Islam on 16 Jun 2020 at 18:52
I have a huge data set that I need for training (32000*2500). This seems to be too much for my classifier. So I decided to do some reading on dimensionality reduction and specifically into PCA.
From my understanding PCA selects the current data and replots them on another (x,y) domain/scale. These new coordinates don't mean anything but the data is rearranged to give one axis maximum variation. After these new coefficients I can drop the cooeff having minimum variation.
Now I am trying to implement this in MatLab and am having trouble with the output provided. MatLab always considers rows as observations and columns as variables. So my inout to the pca function would be my matrix of size (32000*2500). This would return the PCA coefficients in an output matrix of size 2500*2500.
The help for pca states:
Each column of coeff contains coefficients for one principal component, and the columns are in descending order of component variance.
In this output, which dimension is the observations of my data? I mean if I have to give this to the classifier, will the rows of coeff represent my datas observations or is it now the columns of coeff?
And how do I remove the coefficients having the least variation? And thus effectively reduce the dimension of my data

the cyclist on 27 Feb 2016
Edited: the cyclist on 25 Oct 2018
Here is some code I wrote to help myself understand the MATLAB syntax for PCA.
rng 'default'
M = 7; % Number of observations
N = 5; % Number of variables observed
X = rand(M,N);
% De-mean (MATLAB will de-mean inside of PCA, but I want the de-meaned values later)
X = X - mean(X); % Use X = bsxfun(@minus,X,mean(X)) if you have an older version of MATLAB
% Do the PCA
[coeff,score,latent,~,explained] = pca(X);
% Calculate eigenvalues and eigenvectors of the covariance matrix
covarianceMatrix = cov(X);
[V,D] = eig(covarianceMatrix);
% "coeff" are the principal component vectors. These are the eigenvectors of the covariance matrix. Compare ...
coeff
V
% Multiply the original data by the principal component vectors to get the projections of the original data on the
% principal component vector space. This is also the output "score". Compare ...
dataInPrincipalComponentSpace = X*coeff
score
% The columns of X*coeff are orthogonal to each other. This is shown with ...
corrcoef(dataInPrincipalComponentSpace)
% The variances of these vectors are the eigenvalues of the covariance matrix, and are also the output "latent". Compare
% these three outputs
var(dataInPrincipalComponentSpace)'
latent
sort(diag(D),'descend')
The first figure on the wikipedia page for PCA is really helpful in understanding what is going on. There is variation along the original (x,y) axes. The superimposed arrows show the principal axes. The long arrow is the axis that has the most variation; the short arrow captures the rest of the variation.
Before thinking about dimension reduction, the first step is to redefine a coordinate system (x',y'), such that x' is along the first principal component, and y' along the second component (and so on, if there are more variables).
In my code above, those new variables are dataInPrincipalComponentSpace. As in the original data, each row is an observation, and each column is a dimension.
These data are just like your original data, except it is as if you measured them in a different coordinate system -- the principal axes.
Now you can think about dimension reduction. Take a look at the variable explained. It tells you how much of the variation is captured by each column of dataInPrincipalComponentSpace. Here is where you have to make a judgement call. How much of the total variation are you willing to ignore? One guideline is that if you plot explained, there will often be an "elbow" in the plot, where each additional variable explains very little additional variation. Keep only the components that add a lot more explanatory power, and ignore the rest.
In my code, notice that the first 3 components together explain 87% of the variation; suppose you decide that that's good enough. Then, for your later analysis, you would only keep those 3 dimensions -- the first three columns of dataInPrincipalComponentSpace. You will have 7 observations in 3 dimensions (variables) instead of 5.
I hope that helps!

Warid Islam on 16 Jun 2020 at 1:39
Hi @the cyclist,
If I want to incorporate PCA into SVM, should I use the score array or the dataInPrincipalComponentSpace? i am actually having confusion between SCORE and dataInPrincipalComponentSpace.
the cyclist on 16 Jun 2020 at 10:11
Have you run my code above, and read the comments? The variables score and dataInPrincipalComponentSpace are equal to each other. I hope that solves the confusion.
Warid Islam on 16 Jun 2020 at 18:52
Hi @the cyclist,
I did run the code and it worked perfectly. Thank you for the clarification about the above two parameters.

naghmeh moradpoor on 1 Jul 2017
Dear Cyclist,
I used your code and I was successful to find all the PCAs for my dataset. Thank you! On my dataset, PC1, PC2 and PC3 explained more than 90% of the variance. I would like to know how to find which variables from my dataset are related to PC1, PC2 and PC3?
Please could you help me with this Regards, Ngh

#### 1 Comment

Abdul Haleem Butt on 3 Nov 2017
dataInPrincipalComponentSpace is same as in the original data, each row is an observation, and each column is a dimension.

Sahil Bajaj on 12 Feb 2019
Dear Cyclist,
Thansk a lot for your helpful explanation. I used your code and I was successful to find 4 PCAs explaining 97% variance for my dataset, which had total 14 components initially. I was just wondering how to find which variables from my dataset are related to PC1, PC2, PC3 and PC4 so that I can ignore the others, and know which parameters should I use for further analysis?
Thanks !
Sahil

Salma Hassan on 18 Sep 2019
Harsha K on 27 Feb 2020
Lets say I have found the Eigen Values sorted in descending order which is the case after following your code above.
For the Eigen vectors corresponding to the sorted Eigen Values, I would like to recover the original data, so only those variables (or columns) of the original matrix that correspond to the first 3 principle vectors, for example.
% Can I do this,
% is this corresponding first 3 or most needed 3 variable columns ?
Xexp = dataInPrincipalComponentSpace(:, 1:3) * coeff(1:3, 1:3)' + meanX(1:3);
% Where meanX = mean(X, 1);
the cyclist on 28 Feb 2020
I'll quote myself here: "In general, every variable contributes to every principal component." In my example with 5 variables, if they had all been very highly correlated with each other, that all 5 of them contributed significantly to the first principal component. You could not eliminate any of the original variables without significant loss of information.
Referring again to the figure at the top of the wikipedia page on PCA: you can't eliminate either the x-axis variable or the y-axis variable. Instead, you choose a linear combination of them that captures the maximal variation.
And, repeating myself one more time ... there are techniques like varimax, applied after PCA, that do allow you to remove some of the original variables.

Salma Hassan on 18 Sep 2019
i still not understand
i need an answer for my question------> how many eigenvector i have to use?
from these figures   the cyclist on 19 Sep 2019
It is not a simple answer. The first value of the explained variable is about 30. That means that the first principal component explains about 30% of the total variance of all your variables. The next value of explained is 14. So, together, the first two components explain about 44% of the total variation. Is that enough? It depends on what you are trying to do. It is difficult to give generic advice on this point.
You can plot the values of explained or latent, to see how the explained variance is captured as you add each additional component. See, for example, the wikipedia article on scree plots.
Salma Hassan on 19 Sep 2019
if we say that the first two components which explain about 44% enough for me, what does this mean for latent and coff . how can this lead me to the number of eigen vectors