Skip to content

Instantly share code, notes, and snippets.

@invkrh
Created January 26, 2015 17:01
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 invkrh/23ae1fccab55226ad5f9 to your computer and use it in GitHub Desktop.
Save invkrh/23ae1fccab55226ad5f9 to your computer and use it in GitHub Desktop.
correction of ndcg
def ndcgAt(k: Int): Double = {
require(k > 0, "ranking position k should be positive")
predictionAndLabels.map { case (pred, lab) =>
val labSet = lab.toSet
if (labSet.nonEmpty) {
val labSetSize = labSet.size
val predSize = pred.size
val n = math.min(math.max(pred.length, labSetSize), k)
var maxDcg = 0.0
var dcg = 0.0
var i = 0
while (i < n) {
val gain = 1.0 / math.log(i + 2)
if (i < predSize) {
if (labSet.contains(pred(i))) {
dcg += gain
}
}
if (i < labSetSize) {
maxDcg += gain
}
i += 1
}
dcg / maxDcg
} else {
logWarning("Empty ground truth set, check input data")
0.0
}
}.mean
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment