Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn RankingMetrics

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Model Evaluation
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for computing ranking-based classification metrics provided by scikit-learn.

Description

The ranking metrics module provides functions to assess the performance of classification models based on predicted scores or rankings rather than hard class predictions. It includes widely used metrics such as Area Under the ROC Curve (AUC), average precision, precision-recall curves, ROC curves, and ranking-specific measures like NDCG and DCG. Functions named as *_score return values to maximize, while those named *_error or *_loss return values to minimize.

Usage

Use these metrics when evaluating classifiers that output probability estimates or decision scores, when you need threshold-independent evaluation, or when working with ranking and information retrieval tasks.

Code Reference

Source Location

Signature

def auc(x, y)
def average_precision_score(y_true, y_score, *, average="macro", pos_label=1, sample_weight=None)
def det_curve(y_true, y_score, *, pos_label=None, sample_weight=None)
def roc_auc_score(y_true, y_score, *, average="macro", sample_weight=None, max_fpr=None, multi_class="raise", labels=None)
def precision_recall_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=False)
def roc_curve(y_true, y_score, *, pos_label=None, sample_weight=None, drop_intermediate=True)
def label_ranking_average_precision_score(y_true, y_score, *, sample_weight=None)
def coverage_error(y_true, y_score, *, sample_weight=None)
def label_ranking_loss(y_true, y_score, *, sample_weight=None)
def dcg_score(y_true, y_score, *, k=None, log_base=2, sample_weight=None, ignore_ties=False)
def ndcg_score(y_true, y_score, *, k=None, sample_weight=None, ignore_ties=False)
def top_k_accuracy_score(y_true, y_score, *, k=2, normalize=True, sample_weight=None, labels=None)
def confusion_matrix_at_thresholds(y_true, y_score, pos_label=None, sample_weight=None)

Import

from sklearn.metrics import auc, roc_auc_score, average_precision_score
from sklearn.metrics import precision_recall_curve, roc_curve, det_curve
from sklearn.metrics import ndcg_score, dcg_score, top_k_accuracy_score
from sklearn.metrics import label_ranking_average_precision_score, coverage_error, label_ranking_loss

I/O Contract

Inputs

Name Type Required Description
y_true array-like Yes Ground truth (correct) labels or binary label indicators
y_score array-like Yes Target scores, probability estimates, or confidence values
sample_weight array-like No Sample weights for weighted metric computation
average str No Type of averaging for multiclass (macro, micro, weighted, samples)
pos_label int, float, bool or str No The label of the positive class
k int No Number of top elements to consider for DCG/NDCG/top-k metrics
max_fpr float No Maximum false positive rate for partial AUC computation
multi_class str No Multiclass handling strategy (raise, ovr, ovo)

Outputs

Name Type Description
score float Scalar metric value (for *_score functions)
fpr ndarray False positive rates (for curve functions)
tpr/recall ndarray True positive rates or recall values (for curve functions)
thresholds ndarray Decision thresholds used to compute curve points

Usage Examples

Basic Usage

import numpy as np
from sklearn.metrics import roc_auc_score, average_precision_score, roc_curve

# Binary classification scores
y_true = np.array([0, 0, 1, 1])
y_scores = np.array([0.1, 0.4, 0.35, 0.8])

# Compute ROC AUC
roc_auc = roc_auc_score(y_true, y_scores)
print(f"ROC AUC: {roc_auc:.3f}")

# Compute average precision
ap = average_precision_score(y_true, y_scores)
print(f"Average Precision: {ap:.3f}")

# Get ROC curve points
fpr, tpr, thresholds = roc_curve(y_true, y_scores)

# Compute AUC from curve
from sklearn.metrics import auc
roc_auc_from_curve = auc(fpr, tpr)

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment