Reputation: 3436
I want to reduce the dimension of data to ndim dimensions in MATLAB. I am using pcares
to reduce dimension but the result (i.e. residuals,reconstructed) has the same dimensions as the data and not ndim
. How can I project the residuals to ndim
dimensions only.
[residuals,reconstructed] = pcares(X,ndim)
Sample code
MU = [0 0];
SIGMA = [4/3 2/3; 2/3 4/3];
X = mvnrnd(MU,SIGMA,1000);
[residuals,reconstructed] = pcares(X,1)
Now I expect the residuals to have 1 dimensions i.e. the data X
projected to prime component as I specified it as pcares(X,1)
. But here both residuals and reconstructed have the same of 2.
Upvotes: 2
Views: 1474
Reputation: 104464
pcares
is doing its job. If you read the documentation, you call the function this way:
[RESIDUALS,RECONSTRUCTED] = pcares(X,NDIM);
RESIDUALS
returns the residuals for each data point by retaining the first NDIM
dimensions of your data and RECONSTRUCTED
is the reconstructed data using the first NDIM
principal components.
If you want the actual projection vectors, you need to use pca
instead. You'd call it this way:
[coeff,score] = pca(x);
In fact, this is what pcares
does under the hood but it also reconstructs the data for you using the above outputs. coeff
returns the principal coefficients for your data while score
returns the actual projection vectors themselves. score
is such that each column is a single projection vector. It should be noted that these are ordered with respect to dominance as you'd expect with PCA... and so the first column is the most dominant direction, second column second dominant direction, etc.
Once you call the above, you simply index into coeff
and score
to retain whatever components you want. In your case, you just want the first component, and so do this:
c = coeff(1);
s = score(:,1);
If you want to reconstruct the data given your projection vectors, referring to the second last line of code, it's simply:
[coeff,score] = pca(x);
n = size(X,1);
ndim = 1; %// For your case
reconstructed = repmat(mean(X,1),n,1) + score(:,1:ndim)*coeff(:,1:ndim)';
The above is basically what pcares
does under the hood.
Upvotes: 3