def entropy(v, axis=0):
"""
Optimized implementation of entropy. This version is faster than that in
scipy.stats.distributions, particularly over long vectors.
"""
v = numpy.array(v, dtype='float')
s = numpy.sum(v, axis=axis)
with numpy.errstate(divide='ignore', invalid='ignore'):
rhs = numpy.nansum(v * numpy.log(v), axis=axis) / s
r = numpy.log(s) - rhs
# Where dealing with binarized events, it is possible that an event always
# occurs and thus has 0 information. In this case, the negative class
# will have frequency 0, resulting in log(0) being computed as nan.
# We replace these nans with 0
nan_index = numpy.isnan(rhs)
if nan_index.any():
r[nan_index] = 0
return r
评论列表
文章目录