-
Notifications
You must be signed in to change notification settings - Fork 0
/
FDA.m
107 lines (94 loc) · 3.64 KB
/
FDA.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function [Z,W] = FDA(X,Y,r)
% Fisher Discriminant Analysis for Supervised Dimensionality Reduction
% It can work binary and multiclass. It also detects class label
% automatically and it returns W that can be used to multiply any new data
% sample for reduction e.g. X for testing. If you have any problem or
% or difficulty, do not hesitate to send me an email.
% Usage:
%
% [Z,W]=FDA(X,Y,r)
% or
% [Z,W]=FDA(X,Y) , where r values by default is Number of classes
% minus 1: C-1
% Input:
% X: d x n matrix of original samples
% d --- dimensionality of original samples
% n --- the number of samples
% Y: n --- dimensional vector of class labels
% r: ----- dimensionality of reduced space (default:C-1)
% r has to be from 1<=r<=C-1, where C is # of lables "classes"
% Output:
% W: d x r transformation matrix (Z=T'*X)
% Z: r x n matrix of dimensionality reduced samples
%
% (c) Sultan Alzahrani, PhD Student, Arizona State University.
% [email protected], http://www.public.asu.edu/~ssalzahr/
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Z=0;
W=0;
[d,n]=size(X);
[nY,dum]=size(Y);
fprintf('\nNumber of instances: n = %d\n',n);
fprintf('Dimensionality of original samples: d = %d\n',d);
if(nY ~= n)
fprintf('Error: Check Y: Y has to be a vector, nX1 = %d X 1\n\n',n);
return
end
i=1;
ClsLbls=unique(Y');
NumberOfClasses = size(ClsLbls,2);
if(nargin==2)
r=NumberOfClasses-1;
end
fprintf('Number of classes = \n',NumberOfClasses);
fprintf('Class label list: %d\n',char(ClsLbls));
fprintf('Dimensionality of reduced space: r = %d\n',r)
disp('Please wait!');
LocalMu = cell(1,NumberOfClasses);
CovVal = cell(1,NumberOfClasses);
sizeC=zeros(1,NumberOfClasses);
% Compute local Mu, cov matrix, and number of observation of
% for each class class
for clsLbl=unique(Y')
Xc=X(:,Y==clsLbl);
LocalMu(i) = {mean(Xc,2)};
CovVal(i) = {cov(Xc')};
sizeC(i)=size(Xc,2);
i=i+1;
end
%Computing the Global Mu which is the overall mean of all data,X
Global_Mu = mean(X,2);
SB = zeros(d,d);
SW = zeros(d,d);
for i = 1:NumberOfClasses
SB = SB + sizeC(i).*(LocalMu{i}-Global_Mu)*(LocalMu{i}-Global_Mu)';
SW = SW+CovVal{i};
end
% To reduce dimentionality, we need to find W that maximize the ratio
% of Betweeness class Scatter to Within class Scatter.
% W has to satisfy:
% (1) Distance between the class means: the larger the better: SB
% (2) The variance of each class: the smaller the better: SW
% Thus J(W), the objective function is proportional to SB and
% inversely proportional to Sw.
% Thus invSw_by_SB is computed and the projection w that maximizes
% this ratio. This problem is converted to an Eigen
% vector problem for 1<=r<=C-1
% where W= [W1|W2|...|W_c-1] = argmax|W' SB W|/|W' SW W| =>
%(SB-?_iSW)W_i=0
invSw = pinv(SW);
invSw_by_SB = invSw * SB;
[V,D] = eig(invSw_by_SB);
eigval=diag(D);
% Sort invSw_by_SB (which is a matrix of eigen vectors) and then select
% the top vectors assocaited with the top eigen values as follows
% Sorting
[sort_eigval,sort_eigval_index]=sort(eigval,'descend');
% Selecting the top vectors
W=V(:,sort_eigval_index(1:r));
% Now Z has the dimentional reduced data sample X.
Z = W'*X;
[dZ,nZ]=size(Z);
fprintf('Dimentionality reduction is done! and the new size data is %d X %d\n',dZ,nZ);
end