/ Published in: Python
takes a list and returns the entropy
Expand |
Embed | Plain Text
def entropy(l): from math import log log2=lambda x:log(x)/log(2) total=len(l) counts={} for item in l: counts.setdefault(item,0) counts[item]+=1 ent=0 for i in counts: p=float(counts[i])/total ent-=p*log2(p) return ent
You need to login to post a comment.
