def _entropy(self, y, return_class_counts=False):
""" Entropy for the classes in the array y
:math: \sum_{x \in X} p(x) \log_{2}(1/p(x)) :math: from
https://en.wikipedia.org/wiki/ID3_algorithm
Parameters
----------
y : nparray of shape [n remaining attributes]
containing the class names
Returns
-------
: float
information for remaining examples given feature
"""
n = y.shape[0]
if n <= 0:
return 0
classes, count = unique(y)
p = np.true_divide(count, n)
res = np.abs(np.sum(np.multiply(p, np.log2(p))))
if return_class_counts:
return res, np.vstack((classes, count)).T
else:
return res
评论列表
文章目录