Last active
March 16, 2016 23:10
Shannon's entropy measure of dataset heterogeneity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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