## Fastest way to compute entropy in Python

In my project I need to compute the entropy of 0-1 vectors many times. Here’s my code: def entropy(labels): “”” Computes entropy of 0-1 vector. “”” n_labels = len(labels) if n_labels <= 1: return 0 counts = np.bincount(labels) probs = counts[np.nonzero(counts)] / n_labels n_classes = len(probs) if n_classes <= 1: return 0 return – np.sum(probs … Read more