Anonymous user
Entropy: Difference between revisions
m
→{{header|Python}}
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
hist[e] =
return (l,hist)
def entropy(hist,l):
elist = []
for
c =
elist.append(-c * math.log(
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
Line 202 ⟶ 201:
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)</lang>
Output:
|