Skip to content

Commit

Permalink
knPcaPred is not finished. need test
Browse files Browse the repository at this point in the history
  • Loading branch information
sth4nth committed Dec 18, 2015
1 parent b9e8d14 commit 9f083e0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
11 changes: 9 additions & 2 deletions chapter06/demo.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,15 @@
% maxabsdiff(sigma_kn,sigma_lin)
% maxabsdiff(p_kn,p_lin)
%% kernel PCA with linear kernel is PCA
% clear; close all;
% n = 100;
clear; close all;
d = 10;
p = 2;
n = 500;
X = randn(d,n);

model = pca(X,p);
Y = pcaPred(model,X);

%% test case for knCenter
% kn = @knGauss;
% X=rand(2,100);
Expand Down
21 changes: 10 additions & 11 deletions chapter06/knPca.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [ R, Z, err, model] = knPca(X, p, kn)
function model = knPca(X, p, kn)
% Kernel PCA
% X: dxn data matrix
% p: target dimension
Expand All @@ -7,17 +7,16 @@
if nargin < 3
kn = @knGauss;
end

K = kn(X,X);
K = knCenter(K);
[V,A] = eig(K);
[A,idx] = sort(diag(A),'descend');
V = V(:,idx(1:p))';
A = A(1:p);
R = bsxfun(@times,V,1./sqrt(A));
[V,L] = eig(K);
[L,idx] = sort(diag(L),'descend');
V = V(:,idx(1:p));
L = L(1:p)';
U = bsxfun(@times,V,1./sqrt(L));
if nargout > 1
Z = bsxfun(@times,V,sqrt(A));
Z = bsxfun(@times,V,sqrt(L));
end
if nargout > 2
err = diag(K)'-sum(Z.^2,1);
end
model.V = V;
model.L = L;
model.X = X;
7 changes: 6 additions & 1 deletion chapter06/knPcaPred.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
function X = knPcaPred(model, Xt)
function X = knPcaPred(model, Xt, opt)
% Prediction for kernel PCA
% model: trained model structure
% X: d x n testing data
% t (optional): 1 x n testing response
% Written by Mo Chen ([email protected]).

U = model.U;
L = model.L;

if nargin == 3 && opt.whiten
Y = ;
end

0 comments on commit 9f083e0

Please sign in to comment.