param087/agent-ml-skills/imbalanced-data

Claude Code and agent skill by param087/agent-ml-skills

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Use when the target is rare (fraud, churn, disease, anomalies). Covers correct metrics, resampling (SMOTE/undersampling), class weights, threshold tuning, and avoiding the accuracy trap and resampling leakage.

Install

npx vskill@latest install param087/agent-ml-skills/imbalanced-data

SKILL.md

The same SKILL.md was published earlier as param087/agent-ml-skills/imbalanced-data.

Imbalanced Data

Overview

When positives are rare, naive training and naive metrics both mislead. A model that always predicts "negative" can score 99% accuracy and catch zero fraud. Handle imbalance at three levels: metric, algorithm, and threshold — and resample inside cross-validation, never before.

When to use

  • Class ratio is skewed (e.g., 95/5 or worse).
  • Catching the rare class matters (fraud, defaults, rare disease, defects).

Step 1 — Fix the metric first

Drop accuracy and ROC-AUC-as-sole-metric. Prefer:

  • PR-AUC (average precision) — most informative for rare positives.
  • Recall @ fixed precision — "catch X% of fraud while keeping false alarms tolerable."
  • F1 / Fβ — β>1 weights recall when misses are costly.

Step 2 — Algorithm-level handling

TechniqueHowWhen
Class weightsclass_weight="balanced" / scale_pos_weightFirst choice — no data duplication
Undersample majorityRandomUnderSamplerLots of data, majority redundant
Oversample minoritySMOTE / ADASYNLimited minority samples
CombineSMOTE + Tomek/ENNNoisy boundaries

Class weights are the cheapest, leak-free first move:

python
# sklearn
LogisticRegression(class_weight="balanced")
# XGBoost / LightGBM
scale_pos_weight = n_negative / n_positive

Step 3 — Resample INSIDE the pipeline (no leakage)

Resampling before CV leaks synthetic neighbors across the split and inflates scores. Use imblearn's pipeline so SMOTE fits on training folds only:

python
from imblearn.pipeline import Pipeline as ImbPipeline
from imblearn.over_sampling import SMOTE
from sklearn.ensemble import HistGradientBoostingClassifier
from sklearn.model_selection import cross_val_score, StratifiedKFold
pipe = ImbPipeline([
("smote", SMOTE(random_state=42)), # applied to train fold only
("clf", HistGradientBoostingClassifier(random_state=42)),
])
cv = StratifiedKFold(5, shuffle=True, random_state=42)
print(cross_val_score(pipe, X, y, cv=cv, scoring="average_precision").mean())

Step 4 — Tune the decision threshold

Default 0.5 is almost always wrong for imbalanced problems. Pick the threshold from the validation PR curve to hit your precision/recall target:

python
from sklearn.metrics import precision_recall_curve
import numpy as np
prec, rec, thr = precision_recall_curve(y_val, val_scores)
# smallest threshold achieving >= 0.90 precision
ok = np.where(prec[:-1] >= 0.90)[0]
chosen = thr[ok[0]] if len(ok) else 0.5

Pitfalls

  • Accuracy as the metric — the central trap.
  • SMOTE before train/test split — leaks synthetic points; scores look great, production fails.
  • Resampling the test set — evaluate on the real (imbalanced) distribution.
  • Oversampling then ignoring the threshold — probabilities shift; recalibrate/tune the cut.
  • Synthetic samples on categorical/high-dim data — SMOTE assumes meaningful interpolation; use SMOTENC or class weights instead.

Hand-off

A model trained with leak-free resampling/weights, scored with PR-AUC and a tuned threshold via model-evaluation, ready for model-serving.

Security

  • Security scanPassed, 95 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

7-day trend score
0

Not refreshed yet.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Cursor
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/param087/agent-ml-skills/imbalanced-data/badge)](https://verified-skill.com/skills/param087/agent-ml-skills/imbalanced-data)

Something wrong with this skill or its page? Tell us.