Skip to content
Back to skills

Predicted Class Mass Reweighting

ASecurity

Post-hoc rescales ensemble probabilities by the inverse of each class's estimated total mass across the test set, correcting for class imbalance in predictions.

  • 61 stars
  • 0 votes
  • 0 copies
  • 1 view
  • Added September 12, 2026
businesspython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill predicted-class-mass-reweighting --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Predicted Class Mass Reweighting?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Predicted Class Mass Reweighting
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-predicted-class-mass-reweighting/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-predicted-class-mass-reweighting)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: tabular-predicted-class-mass-reweighting
description: >
  Post-hoc rescales ensemble probabilities by the inverse of each class's estimated total mass across the test set, correcting for class imbalance in predictions.
---
# Predicted Class Mass Reweighting

## Overview

Even after ensembling, predicted probabilities can be systematically biased toward the majority class — the model assigns too much total probability mass to class 0 and too little to class 1. This technique estimates each class's total mass across all test predictions (sum of predicted probabilities), then rescales each sample's probabilities by the inverse of its class mass. This shifts the calibration so the predicted class distribution better matches the expected true distribution, improving balanced metrics like balanced log loss.

## Quick Start

```python
import numpy as np

def reweight_by_class_mass(probs):
    """Rescale probabilities by inverse class mass.

    Args:
        probs: (n_samples, n_classes) predicted probabilities
    Returns:
        Reweighted and renormalized probabilities
    """
    class_mass = probs.sum(axis=0)  # total mass per class
    inv_mass = 1.0 / class_mass
    reweighted = probs * inv_mass[np.newaxis, :]
    # Renormalize to sum to 1 per sample
    reweighted /= reweighted.sum(axis=1, keepdims=True)
    return reweighted

# After ensemble averaging
ensemble_probs = np.mean([m.predict_proba(X_test) for m in models], axis=0)
calibrated_probs = reweight_by_class_mass(ensemble_probs)
```

## Workflow

1. Compute ensemble-averaged probabilities for the test set
2. Sum probabilities per class to get estimated class mass
3. Multiply each sample's probability by `1 / class_mass`
4. Renormalize so each row sums to 1

## Key Decisions

- **When to apply**: After ensembling, as a final calibration step
- **Binary case**: For 2-class, simplifies to adjusting the decision boundary
- **vs Platt scaling**: This is non-parametric (no fitting needed); Platt scaling requires a calibration set
- **Assumption**: Assumes the true test distribution is more balanced than model predictions suggest

## References

- [Postprocessin_ Ensemble](https://www.kaggle.com/code/vadimkamaev/postprocessin-ensemble)

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…