Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
princomp on test data

Subject: princomp on test data

From: mainred

Date: 25 Jan, 2013 15:19:08

Message: 1 of 2

I implemented pca on training data, and the code follows like this:
[coefs,scores,variances] = princomp(si,'econ');
%choose the dimension d to keep
percent_explained = 100*variances/sum(variances);
pervar = 100*cumsum(variances)/sum(variances);
d = max(find(pervar<95));
 %new variables in pca subspace
new_variables = scores(:,1:d);

now, I get new test data, so how can i reduce the dimension of the new test data after the training data? Thanks very much in advance.

Subject: princomp on test data

From: Ilya Narsky

Date: 25 Jan, 2013 21:36:50

Message: 2 of 2

"mainred " <haoqingchuan.28@163.com> wrote in message
news:kdu7pc$dtp$1@newscl01ah.mathworks.com...
> I implemented pca on training data, and the code follows like this:
> [coefs,scores,variances] = princomp(si,'econ');
> %choose the dimension d to keep
> percent_explained = 100*variances/sum(variances);
> pervar = 100*cumsum(variances)/sum(variances);
> d = max(find(pervar<95));
> %new variables in pca subspace
> new_variables = scores(:,1:d);
>
> now, I get new test data, so how can i reduce the dimension of the new
> test data after the training data? Thanks very much in advance.
>

You need to project the test data on the reduced set of the principal
components. Note that princomp centers data before computing the principal
components. You need to center the test data as well. It is best to do that
using the means obtained from the training data. Something like this would
do:

mu = mean(Xtrain);
[coefs,scores,variances] = princomp(Xtrain,'econ');
% do your thing to select the components
Xtest = bsxfun(@minus,Xtest,mu); % center the test data
testScores = Xtest*coefs(:,1:d);

-Ilya

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us