Class-balanced log loss that weights each class by the inverse of its sample count, equalizing the contribution of minority and majority classes.
Scanned 9/12/2026
Install to Claude Code
npx -y skills add wenmin-wu/ds-skills --skill balanced-log-loss --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Balanced Log Loss?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/wenmin-wu-balanced-log-loss)More formats (shields.io, HTML) on the badges page.
---
name: tabular-balanced-log-loss
description: >
Class-balanced log loss that weights each class by the inverse of its sample count, equalizing the contribution of minority and majority classes.
---
# Balanced Log Loss
## Overview
Standard log loss is dominated by the majority class — in a 90/10 split, the loss is 90% driven by class-0 predictions. Balanced log loss weights each class by `1/N_class`, equalizing their contribution regardless of sample count. This is the correct metric when false negatives on the minority class are as costly as false positives on the majority class. Many Kaggle competitions with imbalanced binary targets use this as the official metric.
## Quick Start
```python
import numpy as np
def balanced_log_loss(y_true, y_pred):
"""Compute class-balanced log loss.
Args:
y_true: binary labels (0 or 1)
y_pred: predicted probability of class 1
Returns:
Balanced log loss (lower is better)
"""
y_pred = np.clip(y_pred, 1e-15, 1 - 1e-15)
n_0 = np.sum(1 - y_true)
n_1 = np.sum(y_true)
loss_0 = -np.sum((1 - y_true) * np.log(1 - y_pred)) / n_0
loss_1 = -np.sum(y_true * np.log(y_pred)) / n_1
return (loss_0 + loss_1) / 2
# Usage
score = balanced_log_loss(y_val, model.predict_proba(X_val)[:, 1])
```
## Workflow
1. Clip predictions to avoid log(0)
2. Compute log loss for each class separately
3. Divide each class's loss by its sample count
4. Average the two normalized losses
## Key Decisions
- **Clipping**: 1e-15 is standard; prevents -inf from log(0)
- **vs sample_weight**: Equivalent to `log_loss(y, p, sample_weight=inverse_freq)` but more explicit
- **Multiclass**: Extend by weighting each of K classes by `1/N_k`
- **Optimization**: Train with class-weighted BCE to optimize for this metric directly
## References
- [Postprocessin_ Ensemble](https://www.kaggle.com/code/vadimkamaev/postprocessin-ensemble)
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!