Ranking metric definitions
Quick answer
Ranking metrics evaluate an ordered list of results for a query. Precision@k measures the fraction of top-k results that are relevant; recall@k measures recovered relevant items. Reciprocal rank rewards the first relevant item, and NDCG rewards high placement of graded relevance. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Binary top-k and reciprocal rank
r=[0,1,1,0];k=2print(sum(r[:k])/k,sum(r[:k])/sum(r),1/(r.index(1)+1))Observed local output:
0.5 0.5 0.5The first relevant document is second, and only one of two relevant documents appears in the first two.
How it works
For a fixed query and binary relevance, precision@k = relevant retrieved in top k / k (require at least k items), recall@k = relevant retrieved / all known relevant items, and reciprocal rank = 1/rank of first relevant result, or 0 if no relevant result is retrieved. Mean reciprocal rank averages queries, not items. For nonnegative graded relevance, linear-gain DCG@k = sum(rel_i/log2(i+1)) with positions starting at 1; NDCG divides by ideal DCG from sorting relevance descending. An alternative exponential gain uses 2**rel-1; do not compare conventions silently. Scikit-learn ndcg_score uses linear gains and averages possible score-tie permutations by default. For deterministic business rankings, a stable secondary ID is another explicit policy; the two policies are not interchangeable. This lesson reports null for recall with no known positives and for NDCG with ideal gain zero, although the library returns zero for zero ideal gain. Queries may have different numbers of candidates; macro-average query scores, specify exclusions, and bootstrap independent queries (not individual ranked documents) for uncertainty. Judgments can be incomplete, so measured recall may not reflect all relevant items in the corpus.
Graded ranking with tied scores
from sklearn.metrics import ndcg_scorey=[[3,1,0]]for s in [[[3,2,1]],[[1,1,1]]]: print(f"{ndcg_score(y,s,k=2,ignore_ties=False):.3f}")Observed local output:
1.0000.599The ideal ordering scores one; the tied ranking averages position gains across permutations.
Common mistakes
Do not flatten all queries into one rank, use a rank of zero, or compute ideal gain from only the already retrieved top k when other judged candidates exist.
Practice and next steps
Compare relevance lists [0,1,1] and [1,0,1] at k=2; calculate precision, recall and reciprocal rank, then discuss position sensitivity. Complete the knowledge check and staged exercise. Next: Imbalance, calibration and threshold selection.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Ranking metric definitions — knowledge check
Apply ranking metric definitions to new scenarios.
What does reciprocal rank reward?
What normalizes DCG?
What unit should query uncertainty resample?
Which tie policy does sklearn NDCG use by default?
What does precision@k divide by here?