Last active
June 16, 2020 19:02
-
-
Save clayadavis/7715c4fbb6240d094eeea5bfa7cb3ce4 to your computer and use it in GitHub Desktop.
Compute entropy of a probability vector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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