📄 entropy.m
字号:
function H = entropy(X)
% -------------------------------------------------------------------------
% this code is part of the 'Reduction Testbench' suite
% developed by A. Manganaro, R. Todeschini, A. Ballabio, D. Mauri
% 2006 - Milano Chemometrics and QSAR Research Group
% -------------------------------------------------------------------------
%
%
% H = entropy(X)
%
% entropy calculates the normalized Shannon entropy for the given vector
%
% Input:
% X = data vector
%
% Output:
% H = entropy value
echo off;
[n p] = size(X);
% If X is a vector, puts it in column form
if ((n==1)&(p>1))
X = X';
[n p] = size(X);
end
% Sorts the values of X in ascending order
X = sort(X);
% Inits the variables
classes = [];
cur_class = X(1);
cur_class_val = 1;
% Scans X checking for all the equivalence classes.
% For each class, add an element to the classes vector with
% the value of the elements in that class
for idx=2:n
if (X(idx)==cur_class)
cur_class_val = cur_class_val + 1;
else
classes = [classes cur_class_val];
cur_class = X(idx);
cur_class_val = 1;
end
end
classes = [classes cur_class_val];
H = 0;
% Calculate the entropy value
for idx=1:length(classes)
H = H - ( (classes(idx)/n) * log2(classes(idx)/n) );
end
% Normalize the entropy value (0<H<1)
H = H / log2(n);
echo on;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -