forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpcaLsi.m
34 lines (32 loc) · 902 Bytes
/
pcaLsi.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function [V, A] = pcaLsi(X, p)
% Perform Least Square iteration algorithm for PCA (by Sam Roweis).
% X: d x n data matrix
% p: dimension of target space
% Reference:
% Pattern Recognition and Machine Learning by Christopher M. Bishop
% EM algorithms for PCA and SPCA by Sam Roweis
% Written by Mo Chen ([email protected]).
[d,n] = size(X);
X = bsxfun(@minus,X,mean(X,2));
W = rand(d,p);
tol = 1e-8;
error = inf;
last = inf;
t = 0;
while ~(abs(last-error)<error*tol)
t = t+1;
Z = (W'*W)\(W'*X);
W = (X*Z')/(Z*Z');
last = error;
E = X-W*Z;
error = E(:)'*E(:)/n;
end
fprintf('Converged in %d steps.\n',t);
W = normalize(orth(W));
% [W,R] = qr(W,0); % qr() orthnormalize W which is faster than orth().
Z = W'*X;
Z = bsxfun(@minus,Z,mean(Z,2)); % for numerical purpose, not really necessary
[V,A] = eig(Z*Z');
[A,idx] = sort(diag(A),'descend');
V = V(:,idx);
V = W*V;