Map at k metric

From wenmin-wu/ds-skills · View interactive version

Map at k metric is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Custom MAP@K evaluation metric — scores top-K classification predictions with reciprocal rank weighting for HuggingFace Trainer

How to use this skill

Skill file

MAP@K Metric

Overview

Mean Average Precision at K (MAP@K) scores multi-class predictions by checking if the true label appears in the top-K predictions. The score is 1/rank if found (1.0 for rank 1, 0.5 for rank 2, etc.), 0 if not in top-K. Averages across all samples. Standard metric for classification-as-ranking tasks.

Quick Start

import numpy as np
import torch

def compute_map_at_k(eval_pred, k=3):
    """MAP@K metric compatible with HuggingFace Trainer.
    
    Args:
        eval_pred: (logits, labels) tuple from Trainer
        k: number of top predictions to consider
    """
    logits, labels = eval_pred
    probs = torch.nn.functional.softmax(
        torch.tensor(logits, dtype=torch.float32), dim=-1
    ).numpy()
    top_k = np.argsort(-probs, axis=1)[:, :k]
    
    score = 0.0
    for i in range(len(labels)):
        matches = np.where(top_k[i] == labels[i])[0]
        if len(matches) > 0:
            score += 1.0 / (matches[0] + 1)  # reciprocal rank
    return {f"map@{k}": score / len(labels)}

# Usage with HuggingFace Trainer
trainer = Trainer(
    model=model,
    args=training_args,
    compute_metrics=lambda ep: compute_map_at_k(ep, k=3),
)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.