entropy.go 423 B

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