entropy.go 467 B

12345678910111213141516171819202122232425262728
  1. package mathx
  2. import "math"
  3. const epsilon = 1e-6
  4. // CalcEntropy calculates the entropy of m.
  5. func CalcEntropy(m map[interface{}]int) float64 {
  6. if len(m) == 0 || len(m) == 1 {
  7. return 1
  8. }
  9. var entropy float64
  10. var total int
  11. for _, v := range m {
  12. total += v
  13. }
  14. for _, v := range m {
  15. proba := float64(v) / float64(total)
  16. if proba < epsilon {
  17. proba = epsilon
  18. }
  19. entropy -= proba * math.Log2(proba)
  20. }
  21. return entropy / math.Log2(float64(len(m)))
  22. }