Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,34 @@
from __future__ import division
import math
def hist(source):
hist = {}; l = 0;
for e in source:
l += 1
if e not in hist:
hist[e] = 0
hist[e] += 1
return (l,hist)
def entropy(hist,l):
elist = []
for v in hist.values():
c = v / l
elist.append(-c * math.log(c ,2))
return sum(elist)
def printHist(h):
flip = lambda (k,v) : (v,k)
h = sorted(h.iteritems(), key = flip)
print 'Sym\thi\tfi\tInf'
for (k,v) in h:
print '%s\t%f\t%f\t%f'%(k,v,v/l,-math.log(v/l, 2))
source = "1223334444"
(l,h) = hist(source);
print '.[Results].'
print 'Length',l
print 'Entropy:', entropy(h, l)
printHist(h)

View file

@ -0,0 +1,8 @@
from math import log2
from collections import Counter
def entropy(s):
p, lns = Counter(s), float(len(s))
return log2(lns) - sum(count * log2(count) for count in p.values()) / lns
print(entropy("1223334444"))

View file

@ -0,0 +1,19 @@
def Entropy(text):
import math
log2=lambda x:math.log(x)/math.log(2)
exr={}
infoc=0
for each in text:
try:
exr[each]+=1
except:
exr[each]=1
textlen=len(text)
for k,v in exr.items():
freq = 1.0*v/textlen
infoc+=freq*log2(freq)
infoc*=-1
return infoc
while True:
print Entropy(raw_input('>>>'))