/ Published in: Python
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
from math import log def get_histogram_dispersion(histogram): log2 = lambda x:log(x)/log(2) total = len(histogram) counts = {} for item in histogram: counts.setdefault(item,0) counts[item]+=1 ent = 0 for i in counts: p = float(counts[i])/total ent-=p*log2(p) return -ent*log2(1/ent)