您的位置:首页 > 其它

Principal component analysis

2010-03-29 22:39 260 查看
function [finalData]=pca2(t,n,target)%t is the original data set.and n is the number of features you need
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
temp = t;
result = cov(temp);%calculate the covariance matrix of the temp' matrix
[vect value] = eig(result);% calculate the eignvector and the eignvalue
EigArray = eig(result);%calculate the eignvalue
eign=EigArray';
e_length = length(eign);%compute the length of the eignvalue vector

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%the following loops are to sort the eignvector and record the subscript
%corresponding to each eignvalue
for j=1:1:e_length
val=-inf;
for i=1:1:e_length
if val<eign(i)
val=eign(i);
e_sub(j)=i;
end
end
eign(e_sub(j))=-inf;
end
featureVector=[vect(:,e_sub(1))];
% disp('feature is');
% featureVector
for m=2:1:n
featureVector=[featureVector,vect(:,e_sub(m))];

end
disp('feature is');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%calculate the meanVector of the temp'
meanVector = temp;
for i=1:1:100
mean = 1/42*sum(meanVector(:,i));
for j=1:1:42
meanVector(j,i)=meanVector(j,i)-mean;
end
end

finalData=featureVector'*meanVector';%get the final data which is processed use PCA method

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: