forked from PRML/PRMLT
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pca and pcaPred are rewritten (not finished).
- Loading branch information
Showing
4 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
function model = pca( X, p ) | ||
% Principal component analysis | ||
% Written by Mo Chen ([email protected]). | ||
|
||
Xo = bsxfun(@minus,X,mean(X,2)); | ||
S = Xo*Xo'/size(X,2); % 12.3 | ||
[U,A] = eig(S); % 12.5 | ||
[A,idx] = sort(diag(A),'descend'); | ||
U = U(:,idx(1:p)); | ||
A = A(1:p); | ||
|
||
model.U = U; | ||
model.A = A; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function = pcaPred( model, Xt ) | ||
% Prediction for PCA: project future data to principal subspace | ||
% model: trained model structure | ||
% Xt: d x n testing data | ||
% Written by Mo Chen ([email protected]). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function = pcaPred( model, Xt ) | ||
% Prediction for PCA: project future data to principal subspace | ||
% model: trained model structure | ||
% Xt: d x n testing data | ||
% Written by Mo Chen ([email protected]). | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
function [V, A] = pca(X, p) | ||
% Perform standard PCA (spectral method). | ||
% X: d x n data matrix | ||
% p: dimension of target space (p>=1) or ratio (0<p<1) | ||
% Written by Mo Chen ([email protected]). | ||
|
||
Xo = bsxfun(@minus,X,mean(X,2)); | ||
|
||
|
||
opts.disp = 0; | ||
opts.issym = 1; | ||
opts.isreal = 1; | ||
opts.maxit = 500; | ||
|
||
[d,n] = size(X); | ||
if nargin == 1 | ||
p = min(d,n); | ||
end | ||
|
||
|
||
if 0<p && p<1 % given ratio | ||
[V,A] = svd(X,'econ'); | ||
A = diag(A).^2; | ||
|
||
S = cumsum(A); | ||
idc = (S/S(end))<=p; | ||
V = V(:,idc); | ||
A = A(idc); | ||
elseif p >= min(d,n) % full pca | ||
[V,A] = svd(X,'econ'); | ||
A = diag(A).^2; | ||
elseif d <= n % covariance based pca | ||
[V,A] = eigs(X*X',p,'la',opts); | ||
A = diag(A); | ||
elseif d > n % inner product based pca | ||
[U,A] = eigs(X'*X,p,'la',opts); | ||
A = diag(A); | ||
V = X*bsxfun(@times,U,1./sqrt(A)'); | ||
end | ||
|