source: distools/kfd.m @ 25

Last change on this file since 25 was 10, checked in by bduin, 14 years ago
File size: 2.5 KB
RevLine 
[10]1%KFD Kernel Fisher Discriminant
2%
3%   W = KFD(K,ALF,BIAS)
4%
5% INPUT
6%   K     NxN kernel or similarity matrix (dataset)
7%   ALF   Regularization constant
8%   BIAS  Use bias (1) or not (0)
9%
10% OUTPUT
11%   W     Trained Kernel Fisher discriminant
12%
13% DEFAULT
14%   ALF  = 0.0001
15%   BIAS = 1
16%
17% DESCRIPTION
18% Finds a Fisher linear discriminant in a kernel-induced space.
19% Regularization is necessary.
20% Multi-class classifier is trained one-vs-all classes.
21%
22% SEE ALSO
23% KPCA, FISHERC, MAPPINGS, DATASETS
24%
25% REFERENCE
26% S. Mika, G. Ratsch, J. Weston, B. Scholkopf, and K.-R. Muller.
27% Fisher discriminant analysis with kernels. In: Neural Networks for
28% Signal Processing IX, pages 41-48, 1999.
29%
30
31% Copyright: Ela Pekalska, ela.pekalska@googlemail.com
32% Faculty EWI, Delft University of Technology and
33% School of Computer Science, University of Manchester
34%
35
36
37function W = kfd(K,alf,isb0)
38if nargin < 3,
39  isb0 = 1;
40end
41if nargin < 2 | isempty(alf),
42  alf = 0.0001;
43end
44if nargin < 1 | isempty(K),
45  W = mapping(mfilename,{alf,isb0});
46  W = setname(W,'KFD');
47  return
48end
49
50if alf <= 0,
51  error('A small positive regularization ALF is necessary.');
52end
53
54islabtype(K,'crisp');
55isvaldset(K,1,2);     % At least one object per class and two classes
56
57lab      = getnlab(K);
58lablist  = getlablist(K);
59[n,k,C]  = getsize(K);
60
61
62% If more than two classes, train in the one-against-all strategy.
63if C > 2,
64  W = [];
65  for i=1:C
66    mlab = 2 - (lab == i);
67    KK   = setlabels(K,mlab);
68    KK   = remclass(KK,0);
69    if ~isempty(K.prior)
70      KK = setprior(KK,[K.prior(i),1-K.prior(i)]');
71    end
72
73    v  = kfd(KK,alf,isb0);
74    W  = [W,setlabels(v(:,1),lablist(i,:))];
75  end
76  return
77
78else    % Two classes
79
80%  V  = kcenterm(K);   % Center the kernel
81%  KK = +(K*V);        % but ... centering is not necessary
82
83  KK = +K;
84  Y  = 3 - 2 * lab;   % Set labels to +/-1
85  I1 = find(Y==1);
86  I2 = find(Y==-1);
87  n1 = length(I1);
88  n2 = length(I2);
89
90  M1 = sum(KK(:,I1),2)/n1;
91  M2 = sum(KK(:,I2),2)/n2;
92  M  = (M1-M2)*(M1-M2)';
93  N  = KK(:,I1)*(eye(n1) - ones(n1)/n1)*KK(:,I1)' + KK(:,I2)*(eye(n2) - ones(n2)/n2)*KK(:,I2)';
94
95  % Regularization
96  N = N + alf * eye(n);
97
98  % Optimization
99  %[W,V,U] = svds(prinv(N)*M,1);
100  W = prinv(N)*(M1-M2);   % The same as above up to scaling
101
102  % Project data on the found direction
103  b0 = 0;
104  if isb0,
105    % Find the free parameter
106    b0 = -W'*(M1+M2)/2;
107  end
108end
109
110
111% Determine the mapping
112W = affine(W,b0,K,lablist,k,2);
113%W = cnormc(W,K);
114%W = V*W;
115W = setname(W,'KFD');
116return;
Note: See TracBrowser for help on using the repository browser.