Skip to content

Instantly share code, notes, and snippets.

@miguno
Created January 8, 2015 12:21
Show Gist options
  • Save miguno/65495d92bb5758a93232 to your computer and use it in GitHub Desktop.
Save miguno/65495d92bb5758a93232 to your computer and use it in GitHub Desktop.
Entropy
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