undersampling

**Undersampling** is a **technique for handling imbalanced datasets by reducing the number of majority class examples** — rather than creating more minority examples (oversampling), undersampling removes majority examples until the classes are balanced, trading dataset size for class balance, which is fast and simple but risks discarding valuable information from the majority class that the model could have learned from. **What Is Undersampling?** - **Definition**: The deliberate removal of examples from the majority class to achieve a more balanced class distribution — if you have 10,000 legitimate transactions and 100 fraud cases, random undersampling selects 100 random legitimate transactions to match the 100 fraud cases. - **The Trade-off**: You solve the imbalance problem but throw away 9,900 potentially useful examples. This is acceptable when the majority class is large enough to be redundant, but dangerous when every example provides unique information. - **When It's Best**: Large datasets where the majority class has millions of examples and removing some doesn't lose important patterns (e.g., 10M legitimate emails, 10K spam). **Undersampling Methods** | Method | Approach | Pros | Cons | |--------|---------|------|------| | **Random Undersampling** | Randomly select N majority examples (N = minority count) | Simplest, fastest | May discard important edge cases | | **Tomek Links** | Remove majority examples that form "Tomek Links" with minority examples (nearest neighbors of opposite class) | Only removes ambiguous boundary examples | Mild reduction, may not fully balance | | **Edited Nearest Neighbors (ENN)** | Remove majority examples whose nearest neighbors are mostly minority | Cleans noisy boundary regions | Conservative, small reduction | | **NearMiss** | Keep majority examples closest to minority examples | Preserves boundary-relevant examples | Can lose global majority patterns | | **Cluster Centroids** | Replace majority class with cluster centroids using K-Means | Preserves distribution structure | Generated centroids may not be realistic | | **One-Sided Selection (OSS)** | Remove Tomek links + redundant majority examples | Balanced approach | More complex | **Example: Random Undersampling** | Before | After | |--------|-------| | Class A (Legitimate): 10,000 | Class A: 100 (randomly selected) | | Class B (Fraud): 100 | Class B: 100 (unchanged) | | Total: 10,100 | Total: 200 | | Ratio: 100:1 | Ratio: 1:1 | **Tomek Links (Smart Undersampling)** A Tomek Link is a pair of examples (one from each class) that are each other's nearest neighbor. These pairs sit right on the decision boundary and are the most ambiguous examples. Removing the majority example from each Tomek Link cleans the boundary without aggressive data removal. **Python Implementation** ```python from imblearn.under_sampling import ( RandomUnderSampler, TomekLinks, EditedNearestNeighbours ) # Random undersampling rus = RandomUnderSampler(random_state=42) X_resampled, y_resampled = rus.fit_resample(X_train, y_train) # Tomek Links (smart boundary cleaning) tl = TomekLinks() X_clean, y_clean = tl.fit_resample(X_train, y_train) ``` **Undersampling vs Oversampling** | Factor | Undersampling | Oversampling (SMOTE) | |--------|--------------|---------------------| | **Dataset size after** | Smaller (faster training) | Larger (slower training) | | **Information** | Loses majority examples | Keeps all original + adds synthetic | | **Risk** | Underfitting (too little data) | Overfitting (synthetic noise) | | **Speed** | Fast | Moderate | | **Best when** | Majority class is very large (millions) | Dataset is small overall | **Undersampling is the fast, simple approach to class imbalance** — trading majority class examples for balanced class distributions, best used when the majority class is large enough that removing examples doesn't sacrifice important patterns, with Tomek Links and Edited Nearest Neighbors providing smarter alternatives to random removal by targeting only the ambiguous boundary examples.

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account