Skip to content

Instantly share code, notes, and snippets.

@clayadavis
Last active June 16, 2020 19:02
Show Gist options
  • Save clayadavis/7715c4fbb6240d094eeea5bfa7cb3ce4 to your computer and use it in GitHub Desktop.
Save clayadavis/7715c4fbb6240d094eeea5bfa7cb3ce4 to your computer and use it in GitHub Desktop.
Compute entropy of a probability vector
import math
def entropy(vec):
norm = sum(vec)
p = (x / norm for x in vec)
return -sum(p_k * math.log(p_k) for p_k in p if p_k)
import numpy as np
def entropy(p):
p = np.array(p)
p /= sum(p)
return -sum(p * np.log(p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment