-
Notifications
You must be signed in to change notification settings - Fork 4
/
Fisher_Score.m
41 lines (36 loc) · 912 Bytes
/
Fisher_Score.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
function [W] = Fisher_Score(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
numC = max(Y);
[~, numF] = size(X);
W = zeros(1,numF);
% statistic for classes
cIDX = cell(numC,1);
n_i = zeros(numC,1);
for j = 1:numC
cIDX{j} = find(Y(:)==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
W(i) = 0;
else
if temp2 == 0
W(i) = 100;
else
W(i) = temp1/temp2;
end
end
end