-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsFisher.m
47 lines (41 loc) · 1 KB
/
fsFisher.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
function [out] = fsFisher(X,Y)
%Fisher Score, use the N var formulation
% X, the data, each raw is an instance
% Y, the label in 1 2 3 ... format
class = sort(unique(Y))';
numC = length(class);
% numC = max(Y);
[~, numF] = size(X);
out.W = zeros(1,numF);
% statistic for classes
cIDX = cell(numC,1);
n_i = zeros(numC,1);
for j = 1:numC
cIDX{j} = find(Y(:)==class(j));
n_i(j) = length(cIDX{j});
end
% calculate score for each features
for i = 1:numF
temp1 = 0;
temp2 = 0;
f_i = X(:,i);
u_i = mean(f_i);
for j = 1:numC
u_cj = mean(f_i(cIDX{j}));
var_cj = var(f_i(cIDX{j}),1);
temp1 = temp1 + n_i(j) * (u_cj-u_i)^2;
temp2 = temp2 + n_i(j) * var_cj;
end
if temp1 == 0
out.W(i) = 0;
else
if temp2 == 0
out.W(i) = 100;
else
out.W(i) = temp1/temp2;
end
end
end
out.fList = 1:1:numF;
% [~, out.fList] = sort(out.W, 'descend');
out.prf = 1;