Cluster-based undersampling#
Clusters the majority class and keeps representatives of each cluster, shrinking it while preserving its structure.
Important
✨ AI-generated content. This page was written with the assistance of an AI language model and is provided as a learning aid. Despite careful review, it may still contain mistakes, omissions, or out-of-date information. Whether you are new to the topic, a team lead, or a senior practitioner, treat it as a starting point rather than an authoritative reference: read it critically and independently verify anything you act on (code, commands, figures, and factual claims) against official documentation and primary sources before relying on it.
What it is#
Cluster-based undersampling is a smarter way to shrink the majority class than dropping rows at random. It first clusters the majority samples (typically with K-means) and then keeps a representative from each cluster, so the reduced set still covers the full spread of the majority class instead of leaving gaps by chance.
How it works#
Split the data into majority and minority classes.
Cluster the majority class (commonly K-means).
From each cluster, keep representatives — the points nearest the centroid, or a fixed proportion of the cluster.
Combine those with the minority class to form a balanced dataset.
Trade-offs#
Advantages:
Preserves the structure of the majority class, so less information is lost.
Avoids the luck-of-the-draw problem of random undersampling.
Often classifies better than naive undersampling.
Disadvantages:
The clustering step adds cost.
Results depend on the clustering method and the number of clusters \(K\).
It still discards data, so cutting too far risks underfitting.
Example#
With 10,000 majority and 1,000 minority samples, cluster the majority into 1,000 clusters and keep one representative per cluster — a balanced 1,000 vs 1,000 that still spans the majority distribution.
from collections import Counter
from imblearn.under_sampling import ClusterCentroids
print("before:", Counter(y))
X_res, y_res = ClusterCentroids(random_state=42).fit_resample(X, y)
print("after: ", Counter(y_res))
Theme: Imbalanced Learning & Resampling · All terminology
Hint
Mind map — connected ideas
Random Undersampling · NearMiss (Distance-based Undersampling) · Subsampling · Oversampling · SMOTE (Synthetic Minority Over-sampling Technique)
Hint
More in Imbalanced Learning & Resampling
Class Weighting · Downsampling · NearMiss (Distance-based Undersampling) · Oversampling · Random Undersampling · SMOTE (Synthetic Minority Over-sampling Technique) · Subsampling · Upsampling
See also
Source article Adapted (context, re-expressed) in our own words from: Cluster-based undersampling (insightful-data-lab.com).