Skip to content
Back to skills

Class Balanced Epoch Undersampling

ASecurity

Dynamically undersample majority class each epoch with per-class keep probabilities for stochastic balance

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

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill class-balanced-epoch-undersampling --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Class Balanced Epoch Undersampling?

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

Security grade badge for Class Balanced Epoch Undersampling
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-class-balanced-epoch-undersampling/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-class-balanced-epoch-undersampling)

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: cv-class-balanced-epoch-undersampling
description: Dynamically undersample majority class each epoch with per-class keep probabilities for stochastic balance
---

## Overview

Instead of fixed undersampling (which discards data permanently), resample at the start of each epoch using per-class keep probabilities. Negative samples are randomly dropped with probability p, so the model sees different negative subsets each epoch while maintaining approximate class balance. This preserves data diversity across epochs while controlling imbalance.

## Quick Start

```python
import numpy as np

class BalancedGenerator:
    def __init__(self, ids, labels, batch_size, keep_probs=None):
        self.ids = ids
        self.labels = labels
        self.batch_size = batch_size
        self.keep_probs = keep_probs or {0: 0.35, 1: 0.5}

    def on_epoch_end(self):
        keep_prob = self.labels.map(self.keep_probs)
        keep = keep_prob > np.random.rand(len(keep_prob))
        self.indices = np.where(keep)[0]
        np.random.shuffle(self.indices)

    def __len__(self):
        return len(self.indices) // self.batch_size

    def __getitem__(self, idx):
        batch_ids = self.indices[idx * self.batch_size:(idx + 1) * self.batch_size]
        return self._load_batch(batch_ids)
```

## Workflow

1. Define keep probabilities per class (e.g., negative=0.35, positive=0.5)
2. At each epoch start, draw random numbers for all samples
3. Keep sample if random draw < its class keep probability
4. Shuffle surviving indices and train on this subset
5. Next epoch gets a different random subset

## Key Decisions

- **Keep probabilities**: Set lower for majority class. 0.35 for negatives with 0.5 for positives gives ~1.4:1 ratio from a 6:1 imbalanced set.
- **vs. fixed undersampling**: Fixed discards data permanently. Stochastic sees all data over multiple epochs.
- **vs. oversampling**: No duplicate samples, avoids overfitting to minority class. Works better with augmentation.

## References

- [RSNA InceptionV3 Keras](https://www.kaggle.com/code/akensert/rsna-inceptionv3-keras-tf1-14-0)

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…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.