Created
January 8, 2015 12:21
-
-
Save miguno/65495d92bb5758a93232 to your computer and use it in GitHub Desktop.
Entropy
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
private def entropy(counts: Vector[Long], totalCount: Double): Double = { | |
if (totalCount == 0) { | |
return 0 | |
} | |
def log2(x: Double) = scala.math.log(x) / scala.math.log(2) | |
counts.filter(_ != 0).map { count => | |
val freq = 1.0 * count / totalCount | |
freq * log2(freq) | |
}.foldLeft(0.0) { case (agg, i) => agg - i } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment