[10] | 1 | %KPCA Kernel Principal Component Analysis
|
---|
| 2 | %
|
---|
| 3 | % [W,L] = KPCA(K,ALF)
|
---|
| 4 | % OR
|
---|
| 5 | % [W,L] = KPCA(W,ALF)
|
---|
| 6 | %
|
---|
| 7 | % INPUT
|
---|
| 8 | % K NxN kernel or symmetric similarity matrix (dataset)
|
---|
| 9 | % W Trained KPCA projection a kernel-induced space
|
---|
| 10 | % ALF Parameter determining the dimensionality and the mapping (optional, default: Inf)
|
---|
| 11 | % (0,1) - fraction of the total preserved variance; e.g. ALF = 0.9
|
---|
| 12 | % M - Number of dimensions in the subspace; e.g. ALF = 5
|
---|
| 13 | % Inf - No dimensionality reduction, keeping all dimensions (it's VERY noisy)
|
---|
| 14 | %
|
---|
| 15 | % OUTPUT
|
---|
| 16 | % W Kernel PCA mapping
|
---|
| 17 | % L Sorted list of eigenvalues
|
---|
| 18 | %
|
---|
| 19 | % DEFAULTS
|
---|
| 20 | % ALF = Inf
|
---|
| 21 | %
|
---|
| 22 | % DESCRIPTION
|
---|
| 23 | % Performs principal component analysis in a kernel-induced space by finding
|
---|
| 24 | % M significant directions. M is provided directly or determined by ALF such
|
---|
| 25 | % that the fraction ALF of the total variance is preserved. L is a sorted list
|
---|
| 26 | % of eigenvalues describing the variances in the kernel subspace.
|
---|
| 27 | % The projection X is found as X = K*W.
|
---|
| 28 | %
|
---|
| 29 | % A trained mapping can be reduced further by:
|
---|
| 30 | % W = KPCA(W,ALF)
|
---|
| 31 | %
|
---|
| 32 | % SEE ALSO
|
---|
[79] | 33 | % MAPPINGS, DATASETS, PCAM, PSEM
|
---|
[10] | 34 | %
|
---|
| 35 | % REFERENCE
|
---|
| 36 | % B. Scholkopf, A. Smola, and K.-R. Muller. Kernel Principal Component Analysis.
|
---|
| 37 | % in Advances in Kernel Methods - SV Learning, pages 327-352. MIT Press, Cambridge, MA, 1999.
|
---|
| 38 |
|
---|
| 39 | % Copyright: Elzbieta Pekalska, ela.pekalska@googlemail.com
|
---|
| 40 | % Faculty EWI, Delft University of Technology and
|
---|
| 41 | % School of Computer Science, University of Manchester, UK
|
---|
| 42 |
|
---|
| 43 |
|
---|
[120] | 44 | function [W,L] = kpca (K,alf,reg)
|
---|
[10] | 45 |
|
---|
| 46 | if nargin < 2 | isempty(alf),
|
---|
| 47 | alf = inf;
|
---|
| 48 | end
|
---|
| 49 | if nargin < 1 | isempty(K),
|
---|
[120] | 50 | W = prmapping(mfilename,{alf,reg});
|
---|
[10] | 51 | W = setname(W,'Kernel PCA');
|
---|
| 52 | return
|
---|
| 53 | end
|
---|
| 54 |
|
---|
[120] | 55 | if nargin > 2 && ~isempty(reg)
|
---|
| 56 | K = K+reg*eye(size(K,1));
|
---|
| 57 | end
|
---|
[10] | 58 |
|
---|
| 59 | if (isdataset(K) | isa(K,'double')),
|
---|
| 60 | if ismapping(alf),
|
---|
| 61 | % APPLY MAPPING: project new data using the trained mapping.
|
---|
| 62 | [m,n] = size(K);
|
---|
| 63 | pars = getdata(alf);
|
---|
| 64 |
|
---|
| 65 | % Parameters
|
---|
| 66 | Q = pars{1}; % Eigenvectors
|
---|
| 67 | Kme = pars{2}; % Vector of averaged kernel values
|
---|
| 68 |
|
---|
| 69 | % Centering the kernel
|
---|
| 70 | H = -repmat(1/n,n,n);
|
---|
| 71 | H(1:n+1:end) = H(1:n+1:end) + 1; % H = eye(n) - ones(n,n)/n
|
---|
| 72 | K = (K - Kme(ones(m,1),:)) * H;
|
---|
| 73 | W = K*Q;
|
---|
| 74 | if isdataset(W),
|
---|
| 75 | W.name = ['Projected ' updname(W.name)];
|
---|
| 76 | end
|
---|
| 77 | return;
|
---|
| 78 | end
|
---|
| 79 | end
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | if ~isnumeric(alf) & ~isinf(alf)
|
---|
| 83 | error('Wrong ALF.')
|
---|
| 84 | end
|
---|
| 85 | if alf <= 0
|
---|
| 86 | error('ALF should be positive.')
|
---|
| 87 | end
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | % REDUCE ALREADY TRAINED MAPPING
|
---|
| 91 | if ismapping(K),
|
---|
| 92 | pars = getdata(K);
|
---|
| 93 |
|
---|
| 94 | Q = pars{1};
|
---|
| 95 | L = pars{3};
|
---|
| 96 | m = size(Q,1);
|
---|
| 97 |
|
---|
| 98 | [ll,P] = sort(-abs(L));
|
---|
| 99 | L = L(P);
|
---|
| 100 | Q = Q(:,P);
|
---|
| 101 | J = seleigs(L,alf); % J is the index of selected eigenvalues
|
---|
| 102 | Q = Q(:,J); % Eigenvectors
|
---|
| 103 | L = L(J); % Eigenvalues
|
---|
| 104 |
|
---|
[79] | 105 | W = prmapping(mfilename,'trained',{Q,pars{2},L},[],m,length(J));
|
---|
[10] | 106 | W = setname(W,'KPCA');
|
---|
| 107 | return
|
---|
| 108 | end
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | % TRAIN MAPPING
|
---|
| 113 | K = +K;
|
---|
| 114 | [n,m] = size(K);
|
---|
| 115 |
|
---|
| 116 | % Tolerance value used in comparisons
|
---|
| 117 | if mean(diag(+K)) < 1,
|
---|
| 118 | tol = 1e-12;
|
---|
| 119 | else
|
---|
| 120 | tol = 1e-10;
|
---|
| 121 | end
|
---|
| 122 |
|
---|
| 123 | if ~issym(K,tol),
|
---|
| 124 | prwarning(1,'Kernel should be symmetric. It is made so by averaging.')
|
---|
| 125 | K = 0.5 * (K+K');
|
---|
| 126 | end
|
---|
| 127 |
|
---|
| 128 | eigmin = min(preig(K));
|
---|
| 129 | if eigmin < 0,
|
---|
| 130 | error(['K is not psd. Minimum eig(K) = ' ...
|
---|
| 131 | num2str(eigmin) '. Please regularize the kernel appropriately or use IKPCA.']);
|
---|
| 132 | end
|
---|
| 133 |
|
---|
| 134 | Kme = mean(K,2)';
|
---|
| 135 |
|
---|
| 136 | % Project the data such that the mean lies at the origin
|
---|
| 137 | H = -repmat(1/n,n,n);
|
---|
| 138 | H(1:n+1:end) = H(1:n+1:end) + 1; % H = eye(n) - ones(n,n)/n
|
---|
| 139 | K = H * K * H; % K is now the centered kernel
|
---|
| 140 | K = 0.5 * (K+K'); % Make sure that K is symmetric after centering
|
---|
| 141 |
|
---|
| 142 | [Q,L] = preig(K);
|
---|
| 143 | Q = real(Q);
|
---|
| 144 | l = diag(real(L));
|
---|
| 145 | [lm,Z] = sort(-l);
|
---|
| 146 | Q = Q(:,Z);
|
---|
| 147 | l = l(Z); % Eigenvalues are sorted by decreasing value
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | J = seleigs(l,alf); % J is the index of selected eigenvalues
|
---|
| 151 | L = l(J); % Eigenvalues
|
---|
| 152 | Q = Q(:,J); % Eigenvectors
|
---|
| 153 |
|
---|
| 154 | % Normalize Q such that the eigenvectors of the covariance
|
---|
| 155 | % matrix are orthonormal
|
---|
| 156 | Q = Q* diag(1./sqrt(diag(Q'*K*Q)));
|
---|
| 157 |
|
---|
| 158 | % Determine the mapping
|
---|
[79] | 159 | W = prmapping(mfilename,'trained',{Q,Kme,L},[],m,length(J));
|
---|
[10] | 160 | W = setname(W,'KPCA');
|
---|
| 161 | return
|
---|