recall_at_k (metrics.py)

jcodemunch-mcp · analytics

Ranking-quality metrics: nDCG@k, MRR@k, Recall@k. Pure-Python, dependency-free. Each metric takes ``predicted`` (the ranked list of IDs returned by the system under test) and ``relevant`` (the set or list of IDs known to be correct for the query). Metrics are normalized to the 0.

"""Ranking-quality metrics: nDCG@k, MRR@k, Recall@k.

Pure-Python, dependency-free. Each metric takes ``predicted`` (the ranked
list of IDs returned by the system under test) and ``relevant`` (the set
or list of IDs known to be correct for the query). Metrics are normalized
to the 0..1 range so they can be averaged across queries and compared
across releases.
"""

from __future__ import annotations

import math
from typing import Iterable


def _to_set(ids: Iterable[str]) -> set[str]:
    return set(ids) if not isinstance(ids, set) else ids


def recall_at_k(predicted: list[str], relevant: Iterable[str], k: int = 10) -> float:
    """Fraction of relevant items present in the top-k predictions."""
    rel = _to_set(relevant)
    if not rel:
        return 0.0
    top_k = predicted[:k]
    h

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key