Recall NDCG and MAP at k
Quick answer
Ranking evaluation asks whether relevant held-out items appear near the top of each user’s eligible list. Recall@K measures coverage of positives, NDCG@K discounts late hits, and MAP@K averages per-user average precision. Examples are static Python snippets tested locally on CPU; NumPy and scikit-learn are required for selected examples. ByteMind execution is unverified.
Average precision at K
r={"a","c"};rank=["b","a","c"]hits=0;total=0for j,item in enumerate(rank[:2],1): if item in r:hits+=1;total+=hits/jprint(hits/len(r),total/min(2,len(r)))Expected output (locally tested): 0.5 0.25. Recall counts one hit; AP discounts its second-place position.
How it works
For unique ranked IDs and relevant set R, recall@K=|topK intersect R|/|R| and precision@K=hits/K under a fixed K with missing ranks counted as misses. No positives means recall undefined; exclude and report such user counts, not silently zero them. For binary relevance rel_j, DCG@K=sum rel_j/log2(j+1), j starting at 1. Ideal DCG uses min(K,|R|) leading ones; NDCG divides by ideal DCG. Graded gains can use raw relevance (scikit-learn) or 2^rel-1; declare the convention. AP@K here sums precision at hit ranks divided by min(K,|R|); other implementations divide by |R|, so document the choice. MAP is the arithmetic mean of user AP values; macro recall gives each eligible user equal weight. Break score ties deterministically before evaluating; scikit-learn ndcg_score averages tied permutations by default, so use unique ranking scores for agreement. Choose a global time cutoff for fitting, validation and final test; build user histories only from permissible past events and keep all related rows together for uncertainty resampling. A warm-user temporal test intentionally reuses user IDs, unlike a user-disjoint cold-start test. Exclude seen items and use full eligible catalog when feasible. Report candidate coverage, skipped users and K. Use paired user bootstrap for uncertainty, not independent rank rows. Offline metrics do not measure causal lift.
Native NDCG check
from sklearn.metrics import ndcg_scoreprint(round(float(ndcg_score([[1,0,1]],[[2,3,1]],k=2)),6))Expected output (locally tested): 0.386853. Unique scores order the irrelevant item first, then one relevant item.
Common mistakes
Do not count repeated recommended IDs as multiple hits, average over interaction rows as if independent users, or tune K on the final test. Do not compare sampled-negative and full-catalog scores without noting the difference.
Practice and next steps
For R={a,c}, ranked [b,a,c], compute recall@2, AP@2 and binary NDCG@2. Explain why position matters even when the hit count is fixed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Recall NDCG and MAP at k — knowledge check
Apply recall ndcg and map at k to small examples and debugging scenarios.
What is recall@K?
What is NDCG normalized by?
Why define AP@K denominator?
What is macro MAP?
Which split suits known users at future times?