Possible Duplicate:
MATLAB is running out of memory but it should not be
I want to perform PCA analysis on a huge data set of points. To be more specific, I have size(dataPoints) = [329150 132] where 328150 is the number of data points and 132 are the number of features.
I want to extract the eigenvectors and their corresponding eigenvalues so that I can perform PCA reconstruction.
However, when I am using the princomp function (i.e. [eigenVectors projectedData eigenValues] = princomp(dataPoints); I obtain the following error :
>> [eigenVectors projectedData eigenValues] = princomp(pointsData);
Error using svd
Out of memory. Type HELP MEMORY for your options.
Error in princomp (line 86)
[U,sigma,coeff] = svd(x0,econFlag); % put in 1/sqrt(n-1) later
However, if I am using a smaller data set, I have no problem.
How can I perform PCA on my whole dataset in Matlab? Have someone encountered this problem?
Edit:
I have modified the princomp function and tried to use svds instead of svd, but however, I am obtaining pretty much the same error. I have dropped the error bellow :
Error using horzcat
Out of memory. Type HELP MEMORY for your options.
Error in svds (line 65)
B = [sparse(m,m) A; A' sparse(n,n)];
Error in princomp (line 86)
[U,sigma,coeff] = svds(x0,econFlag); % put in 1/sqrt(n-1) later
Solution based on Eigen Decomposition
You can first compute PCA on
X'Xas @david said. Specifically, see the script below:Actually,
Vholds the right singular vectors, and it holds the principal vectors if you put your data vectors in rows. The eigenvalues,D, are the variances among each direction. The singular vectors, which are the standard deviations, are computed as the square root of the variances:Then, the left singular vectors,
U, are computed using the formulaX = USV'. Note thatUrefers to the principal components if your data vectors are in columns.Let us reconstruct the original data matrix and see the L2 reconstruction error:
It is almost zero:
If your data vectors are in columns and you want to convert your data into eigenspace coefficients, you should do
U.'*X.This code snippet takes around 3 seconds in my moderate 64-bit desktop.
Solution based on Randomized PCA
Alternatively, you can use a faster approximate method which is based on randomized PCA. Please see my answer in Cross Validated. You can directly compute
fsvdand getUandVinstead of usingeig.You may employ randomized PCA if the data size is too big. But, I think the previous way is sufficient for the size you gave.