Entropy: Difference between revisions

m
Line 168:
=={{header|Python}}==
 
<lang python>from __future__ import division
import math
 
Line 175:
for e in source:
l += 1
if e not in hist.has_key(e):
hist[e] = 1.0
else:hist[e] += 1
hist[e] += 1.0
return (l,hist)
 
def entropy(hist,l):
elist = []
for kv in hist.keysvalues():
c = hist[k]v / l
elist.append(-c * math.log(1.0 / c ,2))
return sum(elist)
 
Line 193 ⟶ 192:
print 'Sym\thi\tfi\tInf'
for (k,v) in h:
print '%s\t%f\t%f\t%f'%(k,v,v/l,-math.log(1.0 / (v/l), 2))
Line 202 ⟶ 201:
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)</lang>
</lang>
 
Output:
Anonymous user