title: Fastest way to compute entropy in Python
body: <p>In my project I need to compute the entropy of 0-1 vectors many times. Here's my code:</p>
  
  <pre><code>def entropy(labels):
      """ Computes entropy of 0-1 vector. """
      n_labels = len(labels)
  
      if n_labels &lt;= 1:
          return 0
  
      counts = np.bincount(labels)
      probs = counts[np.nonzero(counts)] / n_labels
      n_classes = len(probs)
  
      if n_classes &lt;= 1:
          return 0
      return - np.sum(probs * np.log(probs)) / np.log(n_classes)
  </code></pre>
  
  <p>Is there a faster way?</p>