Dimensionality reduction in Matlab

血红的双手。 提交于 2019-12-18 09:08:49

问题


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.


回答1:


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.




回答2:


Try using the squeeze command - see ref here: http://uk.mathworks.com/help/matlab/ref/squeeze.html



来源:https://stackoverflow.com/questions/30353432/dimensionality-reduction-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!