Skip to content

Instantly share code, notes, and snippets.

@brydavis
Last active March 16, 2016 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brydavis/50546de351d1164fd1e3 to your computer and use it in GitHub Desktop.
Save brydavis/50546de351d1164fd1e3 to your computer and use it in GitHub Desktop.
Shannon's entropy measure of dataset heterogeneity
package main
import (
"fmt"
"math"
)
func main() {
// set of probabilities
// ex. you draw 12 cards
// from a deck and organize
// them by suits
ps := []float64{
(5. / 12.), // clubs
(3. / 12.), // hearts
(3. / 12.), // spades
(1. / 12.), // diamonds
}
// measure dataset heterogeniety
fmt.Println(H(ps))
}
// Shannon's entropy
func H(ps []float64) float64 {
total := 0.0
for _, p := range ps {
p_log2 := math.Log2(p)
total += p * p_log2
}
return -1.0 * total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment