Posted By

adkatrit on 02/11/11


Tagged

entropy


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

adkatrit


Entropy


 / Published in: Python
 

takes a list and returns the entropy

  1. def entropy(l):
  2. from math import log
  3. log2=lambda x:log(x)/log(2)
  4.  
  5. total=len(l)
  6. counts={}
  7. for item in l:
  8. counts.setdefault(item,0)
  9. counts[item]+=1
  10.  
  11. ent=0
  12. for i in counts:
  13. p=float(counts[i])/total
  14. ent-=p*log2(p)
  15. return ent

Report this snippet  

You need to login to post a comment.