github.com/lingyao2333/mo-zero@v1.4.1/core/mathx/entropy.go (about)

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