matlab - Entropy of pure split caculated to NaN -
i have written function calculate entropy of vector each element represents number of elements of class.
function x = entropy(a) t = sum(a); t = repmat(t, [1, size(a, 2)]); x = sum(-a./t .* log2(a./t)); end
e.g: a = [4 0]
, entropy = -(0/4)*log2(0/4) - (4/4)*log2(4/4)
but above function, entropy nan
when split pure because of log2(0)
, in above example. entropy of pure split should zero.
how should solve problem least effect on performance data large? thanks
i suggest create own log2
function
function res=mylog2(a) res=log2(a); res(isinf(res))=0; end
this function, while breaking log2
behaviour, can used in specific example because multiplying result inside of log, making zero. not "mathematically correct", believe that's looking for.
Comments
Post a Comment