Created
February 12, 2019 04:58
-
-
Save nuria/3204691aea95b2e6f3c97e3a593dee69 to your computer and use it in GitHub Desktop.
Calculate entropy
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
#!/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