Using Matlab “princomp” for Easy Dimension Reduction Using Principal Component Analysis (PCA)

Although I have detailed another way of doing dimension reduction in Matlab I recently found the command “princomp” which does everything for you. The following code reads in .csv files from a directory and reduces them to a set number of dimensions (“OutputSize” in this case). This is a lot easier than doing it yourself with the eigenvectors etc:

function [output_args]=ReduceUsingPCA2(DirName,OutputSize)

files = dir(fullfile(DirName, ‘*.csv’));
for i=1:length(files)
% read files(i).name and process
FileName= [DirName '/' files(i).name];
% read in csv file from FileName and store in x

% calculate PCs and project data onto principal components
[COEFF,SCORE] = princomp(x);

[infile, remain] = strtok(FileName,’/’);
infile = strtok(remain,’.’);
mkdir([num2str(OutputSize) 'PC']);
outputfilename = [num2str(OutputSize) 'PC' infile '_' num2str(OutputSize) 'PCs.csv'];
csvwrite(outputfilename,SCORE(:,1:OutputSize));
end
end

The important method is   [COEFF,SCORE] = princomp(x); which takes in your data “x” and stores its projection into PCA space in “SCORE” which I then output to csv. I still need to find out how to project back into normal space but I think it should be just as straightforward as this was. For more info on “princomp” type “help princomp” into matlab and have a look at the help files.

10 thoughts on “Using Matlab “princomp” for Easy Dimension Reduction Using Principal Component Analysis (PCA)”

1. Mahfuzul Haque on said:

Hi James,
Thanks for your post.

I am also using the princomp function. However, apart from transforming the data into reduced dimension space, I am also interested to know which are the most significant variables/features in original data space? Do you have any idea on getting this sort of information?
Mahfuzul

• Mohammad Ali Kadampur on said:

Hello, Mahafuzul Haq

Try using the following
cumsum(pcvars./sum(pcvars) * 100)

You can decide after reading its output.

2. Gheida on said:

Hi James,

Have you found out how to project your data into normal space?

3. Pierre Gogin on said:

I have the same problem than Mahfuzul. How do I get informations about which of my features a relevant or not relevant?

4. Torsten on said:

n=size(x,1);

[coeff,score,roots] = princomp(x);
ndim = 17;
reconstructed = repmat(mean(x,1),n,1) + score(:,1:ndim)*coeff(:,1:ndim)’;

I think on should mention that princomp uses SVD instead of eig and so on

5. lui on said:

hi, im working with pca in prtools, and i would like to know if exists a special command who can tell me which one of the caracteristicas has been retained…im stuck in this part of the work. will be really nice if some one knows how to do it!!! :) thx

6. Rahul on said:

Hi,
I am doing random projection of polytope to a subspace. In this you are projecting to eigen space, How to do it for any subspace.

7. Daljit Singh on said:

how to run this program in matlab???i am familar with princomp,pca,lda,& all with these i can perform operation on matrix and rgb images but don’t know what are output values that i have to use for classification,please help me,thanks in advance….

8. Abhay on said:

Hello there, Iam a student of Engineering, from Pune University. I need some help to perform inverse PCA..

I have done up to here..

clc
clear all;
%img=rand(4,4,3)
figure,imshow(img);

img1=img(:,:,1);
[m n]=size(img1);
%figure,imshow(img1);
img2=img(:,:,2);
%figure,imshow(img2);
img3=img(:,:,3);
%figure,imshow(img3);

%to get elements along rows we take imagg’
temp1=reshape(img1′,m*n,1);
%figure,imshow(temp1);
temp2=reshape(img2′,m*n,1);
%figure,imshow(temp2);
temp3=reshape(img3′,m*n,1);
%figure,imshow(temp3);

I=[temp1 temp2 temp3];
figure,imshow(I);

%to get mean
m1=mean(I,2);
figure,imshow(m1);

%subtract mean
temp=double(I);
for i=1:3
I1(:,i)=(temp(:,i)-m1);
end
a1=double(I1);
figure(6),imshow(I1);

a=a1′;
covv = a*a’;

[eigenvec eigenvalue]=eig(covv);

eigenvalue = diag(eigenvalue);

[egn,index]=sort(-1*eigenvalue);

eigenvalue=eigenvalue(index);

eigenvec=eigenvec(:,index);
figure,imshow(eigenvec);

pcaoutput=a1*eigenvalue;

vt=transpose(eigenvalue);
for i=1:size(pcaoutput,2)
ima=reshape(pcaoutput(:,i)’,n,m);
ima=ima’;
imshow(ima,[]);
end

We are performing PCA image fusion b/w MS & PAN Image.

Thank you..