Skip to content

Instantly share code, notes, and snippets.

@alexrutherford
Last active July 23, 2019 09:01
Show Gist options
  • Save alexrutherford/cad44ae24ac110068b2d to your computer and use it in GitHub Desktop.
Save alexrutherford/cad44ae24ac110068b2d to your computer and use it in GitHub Desktop.
Function to calculate entropy using Python
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