Skip to content
Back to skills

Class Balanced Dataset Merge

ASecurity

Merges multiple training datasets while keeping all positive examples and downsampling negatives to control class imbalance.

  • 61 stars
  • 0 votes
  • 0 copies
  • 4 views
  • Added September 12, 2026
datapython

Security analysis

A100/100

Scanned September 12, 2026

npx -y skills add wenmin-wu/ds-skills --skill class-balanced-dataset-merge --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Class Balanced Dataset Merge?

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

Security grade badge for Class Balanced Dataset Merge
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/wenmin-wu-class-balanced-dataset-merge/badge)](https://www.skillsdirectory.com/skills/wenmin-wu-class-balanced-dataset-merge)

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: nlp-class-balanced-dataset-merge
description: >
  Merges multiple training datasets while keeping all positive examples and downsampling negatives to control class imbalance.
---
# Class-Balanced Dataset Merge

## Overview

When combining datasets from different sources or competitions, class ratios can become severely skewed. A common pattern: keep all positive (minority) examples from every source but cap the number of negatives. This gives the model maximum signal on the rare class while preventing the majority class from dominating training.

## Quick Start

```python
import pandas as pd

# Dataset 1: primary training data (use all rows)
# Dataset 2: auxiliary data (keep all positives, downsample negatives)
train = pd.concat([
    train1[["text", "label"]],
    train2[["text", "label"]].query("label == 1"),
    train2[["text", "label"]].query("label == 0").sample(n=100_000, random_state=42),
])
train = train.sample(frac=1, random_state=42).reset_index(drop=True)
```

## Workflow

1. Identify primary and auxiliary training datasets
2. Include all rows from the primary dataset
3. From each auxiliary dataset, keep all positive examples
4. Downsample auxiliary negatives to a fixed count or target ratio
5. Concatenate and shuffle before training

## Key Decisions

- **Sample count**: Set based on desired positive:negative ratio (1:5 to 1:10 is typical)
- **Random state**: Fix seed for reproducibility across experiments
- **Multiple auxiliaries**: Apply the same pattern per source; adjust counts per source quality
- **Alternative**: Use sample weights instead of dropping rows — keeps all data but upweights positives
- **Validation**: Keep validation set untouched; only rebalance training data

## References

- [Jigsaw TPU: XLM-Roberta](https://www.kaggle.com/code/xhlulu/jigsaw-tpu-xlm-roberta)

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.