Last active
July 23, 2019 09:01
-
-
Save alexrutherford/cad44ae24ac110068b2d to your computer and use it in GitHub Desktop.
Function to calculate entropy using Python
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 numpy as np | |
def entropy(vals): | |
sum=0.0 | |
norm=0.0 | |
for v in vals:norm+=v | |
vals=[v/norm for v in vals] | |
for v in vals:sum+=(v*np.log(v)) | |
return -1.0*sum | |
for i in range(10): | |
print i,entropy([1 for j in range(i)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment