Skip to content

Instantly share code, notes, and snippets.

@nuria
Created February 12, 2019 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuria/3204691aea95b2e6f3c97e3a593dee69 to your computer and use it in GitHub Desktop.
Save nuria/3204691aea95b2e6f3c97e3a593dee69 to your computer and use it in GitHub Desktop.
Calculate entropy
#!/usr/bin/python
import sys
import math
f = sys.argv[1]
_file = open(f)
data = {}
total = 0
for l in _file:
d = l.split()
data[d[0]] = float(d[1])
total = float(d[1]) + total
E = 0;
for country in data:
count = data[country]
p = count/total
#print "p {0} count {1}".format(p, count)
E = E + p * (-1)* math.log(p,2)
bound = math.log(len(data),2)
print "Entrophy: {0} Upper bound {1} : ".format(E,bound)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment