Fisher_Score 计算
自己实现的代码
function W = fsFisher(data)%Fisher Score% Input:% data: dataset % Output:% W: W(i) represents the Fisher Score of the i-th feature. % numC = max(Y);Y = data(:, end); % 提取标签X = data(:, 1:end-1); % 提取样本数据,去掉标签列unique_labels = unique(Y); % 获取所有唯一的类别标签numC = length(unique_labels); % 类别数量[~, numF] = size(X);W = zeros(1,numF);% statistic for classescIDX = cell(numC,1);n_i = zeros(numC,1);for j = 1:numC%cIDX{j} = find(Y(:)==j);cIDX{j} = find(Y(:)==unique_labels(j));n_i(j) = length(cIDX{j});end% calculate score for each featuresfor i = 1:numFtemp1 = 0;temp2 = 0;f_i = X(:,i);u_i = mean(f_i);for j = 1:numCu_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% checkif temp1 == 0W(i) = 0;elseif temp2 == 0W(i) = 100;elseW(i) = temp1/temp2;endendend
end
matlab代码如下
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 ... formatnumC = 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:numCcIDX{j} = find(Y(:)==j);n_i(j) = length(cIDX{j});
end% calculate score for each features
for i = 1:numFtemp1 = 0;temp2 = 0;f_i = X(:,i);u_i = mean(f_i);for j = 1:numCu_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;endif temp1 == 0out.W(i) = 0;elseif temp2 == 0out.W(i) = 100;elseout.W(i) = temp1/temp2;endend
end[~, out.fList] = sort(out.W, 'descend');
out.prf = 1;
Bibtex 引用
@BOOK{Duda-etal01,title = {Pattern Classification},publisher = {John Wiley \& Sons, New York},year = {2001},author = {Duda, R.O. and Hart, P.E. and Stork, D.G.},edition = {2},}
}
来源:Feature Selection Package - Algorithms - Fisher Score