Implementation:Scikit learn Scikit learn MetricsModule
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Model Evaluation |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for providing score functions, performance metrics, pairwise metrics, and distance computations, provided by scikit-learn.
Description
The sklearn.metrics module aggregates all evaluation metrics in scikit-learn under a single namespace. It includes classification metrics (accuracy, precision, recall, F1, ROC AUC, confusion matrix), regression metrics (MSE, MAE, R2, explained variance), ranking metrics (DCG, NDCG, average precision), clustering metrics, pairwise distance and kernel functions, and visualization displays (ConfusionMatrixDisplay, RocCurveDisplay, PrecisionRecallDisplay, DetCurveDisplay).
Usage
Use this module to evaluate the performance of machine learning models. It provides functions for computing scores, losses, and distances, as well as display classes for plotting evaluation curves.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/metrics/__init__.py
Signature
# Module-level imports (selected):
from sklearn.metrics._classification import accuracy_score, f1_score, precision_score, recall_score
from sklearn.metrics._ranking import roc_auc_score, roc_curve, auc
from sklearn.metrics._regression import mean_squared_error, mean_absolute_error, r2_score
from sklearn.metrics._scorer import make_scorer, get_scorer
Import
from sklearn.metrics import accuracy_score, mean_squared_error, roc_auc_score
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| y_true | array-like | Yes | Ground truth (correct) labels or values |
| y_pred | array-like | Yes | Predicted labels or values from the estimator |
| y_score | array-like | No | Predicted probabilities or decision function values (for ranking metrics) |
Outputs
| Name | Type | Description |
|---|---|---|
| score | float | Metric value (e.g., accuracy, F1 score, MSE) |
| curve | tuple of arrays | Curve data points (e.g., FPR/TPR for ROC, precision/recall) |
Usage Examples
Basic Usage
from sklearn.metrics import accuracy_score, mean_squared_error
import numpy as np
# Classification metric
y_true = [0, 1, 1, 0]
y_pred = [0, 1, 0, 0]
print(accuracy_score(y_true, y_pred)) # 0.75
# Regression metric
y_true = np.array([3, -0.5, 2, 7])
y_pred = np.array([2.5, 0.0, 2, 8])
print(mean_squared_error(y_true, y_pred)) # 0.375