Ranking Metrics: MAP@k and NDCG@k

medium
Unit 14 · Metrics & Preprocessing0/2 to milestone

Implement two ranking metrics for a list of recommended item ids and a set of relevant ones:

  • average_precision_at_k(recommended, relevant, k): the mean of precision@i at each rank i <= k where a relevant item appears, divided by min(len(relevant), k) (0 if nothing is relevant)
  • ndcg_at_k(recommended, gains, k): gains maps item to graded relevance. DCG=∑i=1k2gi−1log⁡2(i+1)DCG = \sum_{i=1}^{k} \frac{2^{g_i} - 1}{\log_2(i + 1)}, normalised by the DCG of the ideal ordering of all items in gains. Return 0 if the ideal DCG is 0.
#metrics#ranking#recommenders3 visible + 1 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Ranking Metrics: MAP@k and NDCG@k · TechGradify