Implementation:Scikit learn contrib Imbalanced learn geometric mean score
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Machine_Learning, Model_Evaluation, Imbalanced_Learning |
| Last Updated | 2026-02-09 03:00 GMT |
Overview
Concrete tool for computing the geometric mean of per-class sensitivities provided by the imbalanced-learn library.
Description
The geometric_mean_score function computes the G-mean metric. It supports binary, multiclass, macro, weighted, and micro averaging. The correction parameter substitutes a non-zero value for classes with zero sensitivity to prevent the entire metric from collapsing. Uses scipy.stats.gmean internally.
Usage
Import this function to evaluate classifier performance on imbalanced datasets using the geometric mean metric.
Code Reference
Source Location
- Repository: imbalanced-learn
- File: imblearn/metrics/_classification.py
- Lines: L550-736
Signature
def geometric_mean_score(
y_true,
y_pred,
*,
labels=None,
pos_label=1,
average="multiclass",
sample_weight=None,
correction=0.0,
):
"""
Args:
y_true: array-like of shape (n_samples,) - Ground truth labels.
y_pred: array-like of shape (n_samples,) - Predicted labels.
labels: array-like or None - Label subset to include.
pos_label: int - Positive class for binary (default: 1).
average: str - Averaging method: 'multiclass', 'binary', 'macro',
'weighted', 'micro', None (default: 'multiclass').
sample_weight: array-like or None - Sample weights.
correction: float - Substitute for zero sensitivity (default: 0.0).
Returns:
float or ndarray - Geometric mean score.
"""
Import
from imblearn.metrics import geometric_mean_score
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| y_true | array-like of shape (n_samples,) | Yes | Ground truth labels |
| y_pred | array-like of shape (n_samples,) | Yes | Predicted labels |
| average | str or None | No | Averaging strategy (default: 'multiclass') |
| correction | float | No | Substitute for zero sensitivity (default: 0.0) |
Outputs
| Name | Type | Description |
|---|---|---|
| score | float or ndarray | G-mean score (0.0 to 1.0); array if average=None |
Usage Examples
from imblearn.metrics import geometric_mean_score
y_true = [0, 1, 2, 0, 1, 2]
y_pred = [0, 2, 1, 0, 0, 1]
# Default multiclass
score = geometric_mean_score(y_true, y_pred)
print(f"G-mean: {score:.3f}")
# Per-class scores
per_class = geometric_mean_score(y_true, y_pred, average=None)
print(f"Per-class: {per_class}")
# With correction for zero sensitivity
corrected = geometric_mean_score(y_true, y_pred, correction=0.001)
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment