-
-
Save invkrh/23ae1fccab55226ad5f9 to your computer and use it in GitHub Desktop.
correction of ndcg
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
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