在Matlab中使用PCA降低训练数据的维数

时间:2013-12-14 13:59:52

标签: matlab linear-algebra pca dimensionality-reduction

这是一个后续问题:

PCA Dimensionality Reduction

为了对新的10维测试数据进行分类,我是否还要将训练数据减少到10维?

我试过了:

X = bsxfun(@minus, trainingData, mean(trainingData,1));           
covariancex = (X'*X)./(size(X,1)-1);                 
[V D] = eigs(covariancex, 10);   % reduce to 10 dimension
Xtrain = bsxfun(@minus, trainingData, mean(trainingData,1));  
pcatrain = Xtest*V;

但是使用分类器和10维测试数据会产生非常不可靠的结果吗?我在做什么根本就错了吗?

编辑:

X = bsxfun(@minus, trainingData, mean(trainingData,1));           
covariancex = (X'*X)./(size(X,1)-1);                 
[V D] = eigs(covariancex, 10);   % reduce to 10 dimension
Xtrain = bsxfun(@minus, trainingData, mean(trainingData,1));  
pcatrain = Xtest*V;

X = bsxfun(@minus, pcatrain, mean(pcatrain,1));           
covariancex = (X'*X)./(size(X,1)-1);                 
[V D] = eigs(covariancex, 10);   % reduce to 10 dimension
Xtest = bsxfun(@minus, test, mean(pcatrain,1));  
pcatest = Xtest*V;

1 个答案:

答案 0 :(得分:7)

您必须同时减少训练和测试数据,但两者都是相同的。因此,一旦从训练数据中获得PCA的缩减矩阵,就必须使用此矩阵来降低测试数据的维数。简而言之,您需要一个适用于训练和测试元素的恒定变换。

使用您的代码

% first, 0-mean data
Xtrain = bsxfun(@minus, Xtrain, mean(Xtrain,1));           
Xtest  = bsxfun(@minus, Xtest, mean(Xtrain,1));           

% Compute PCA
covariancex = (Xtrain'*Xtrain)./(size(Xtrain,1)-1);                 
[V D] = eigs(covariancex, 10);   % reduce to 10 dimension

pcatrain = Xtrain*V;
% here you should train your classifier on pcatrain and ytrain (correct labels)

pcatest = Xtest*V;
% here you can test your classifier on pcatest using ytest (compare with correct labels)