무작위 언더샘플링과 ClusterCentroids
학습자는 다수 클래스를 무작위 방식과 ClusterCentroids 방식으로 언더샘플링하고, 정보 손실과 그에 따른 모델 성능을 비교합니다.
무작위 언더샘플링과 ClusterCentroids은(는) CoddyKit의 무료 Machine Learning Academy 강의입니다. 이것은 4개 중 3번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Machine Learning Academy 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.
Undersampling: Reducing the Majority Class
While oversampling grows the minority class, undersampling shrinks the majority class to balance the dataset. The key advantage: training is faster because there is less data overall. The key risk: you discard real majority-class information, potentially causing the model to make more false positives. Undersampling is most useful when the majority class is so large that oversampling would make training prohibitively slow.
Random Undersampling: Delete Majority Examples at Random
RandomUnderSampler randomly selects and removes majority-class samples until the desired ratio is achieved. It is the fastest undersampling method but throws away potentially useful information. With small datasets, random undersampling can make the model significantly worse by discarding majority samples near the decision boundary.
from imblearn.under_sampling import RandomUnderSampler
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=1000, weights=[0.95, 0.05],
n_features=10, random_state=42)
print('Before undersampling:', np.bincount(y))
rus = RandomUnderSampler(sampling_strategy=1.0, random_state=42)
X_res, y_res = rus.fit_resample(X, y)
print('After undersampling:', np.bincount(y_res))
print('New total samples:', len(y_res))Controlling the Sampling Strategy
The sampling_strategy parameter controls the final ratio of minority to majority. 1.0 makes them equal; 0.5 keeps 2:1 majority advantage. For very large datasets you might set 0.1 to create a 10:1 ratio — still better than the original 100:1 while keeping training fast. Choose based on the business cost of false positives vs false negatives.
from imblearn.under_sampling import RandomUnderSampler
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=10000, weights=[0.99, 0.01], random_state=42)
print('Original:', np.bincount(y))
for ratio in [1.0, 0.5, 0.2]:
rus = RandomUnderSampler(sampling_strategy=ratio, random_state=0)
_, y_r = rus.fit_resample(X, y)
counts = np.bincount(y_r)
print(f'ratio={ratio}: {counts} ({counts[1]/counts[0]:.2f} min:maj)')Cluster Centroids: Intelligent Undersampling
ClusterCentroids applies K-Means clustering to the majority class, replacing each cluster with its centroid. Instead of keeping random majority samples, it retains a compressed, representative set. This preserves the overall structure of the majority class better than random deletion, but is slower and requires tuning the number of clusters (which equals the target majority count after resampling).
from imblearn.under_sampling import ClusterCentroids
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=1000, weights=[0.9, 0.1],
n_features=5, random_state=42)
print('Before:', np.bincount(y))
cc = ClusterCentroids(sampling_strategy=1.0, random_state=42)
X_cc, y_cc = cc.fit_resample(X, y)
print('After ClusterCentroids:', np.bincount(y_cc))
print('Note: synthetic centroids replace real majority samples')Comparing Undersampling Methods
Let us benchmark random undersampling vs cluster centroids vs no resampling on the same imbalanced dataset, using F1-score on the minority class as the evaluation metric.
from imblearn.under_sampling import RandomUnderSampler, ClusterCentroids
from imblearn.pipeline import Pipeline as ImbPipeline
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import f1_score
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
import numpy as np
X, y = make_classification(n_samples=1000, weights=[0.95, 0.05], random_state=42)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
sc = StandardScaler()
X_tr = sc.fit_transform(X_train)
X_te = sc.transform(X_test)
for name, sampler in [('None', None),
('RUS', RandomUnderSampler(random_state=0)),
('ClusterCentroids', ClusterCentroids(random_state=0))]:
if sampler:
Xr, yr = sampler.fit_resample(X_tr, y_train)
else:
Xr, yr = X_tr, y_train
lr = LogisticRegression().fit(Xr, yr)
f1 = f1_score(y_test, lr.predict(X_te))
print(f'{name:20s}: F1={f1:.4f} n_train={len(yr)}')Information Loss in Undersampling
The critical downside of undersampling is information loss. When 95 out of every 100 majority-class samples are discarded, the model sees a training distribution very different from reality. This can inflate false-positive rates in production. For this reason, undersampling is often combined with oversampling (e.g., using SMOTEENN or SMOTETomek) to balance the dataset while minimising information loss.
Combining Oversampling and Undersampling
SMOTETomek combines SMOTE (oversample minority) with Tomek Links removal (clean ambiguous boundary majority samples). The result balances both classes and removes borderline majority samples that confuse the classifier. This combined approach often outperforms either oversampling or undersampling alone.
from imblearn.combine import SMOTETomek
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=1000, weights=[0.95, 0.05], random_state=42)
print('Before:', np.bincount(y))
st = SMOTETomek(random_state=42)
X_res, y_res = st.fit_resample(X, y)
print('After SMOTETomek:', np.bincount(y_res))Near Miss: Informed Undersampling by Distance
NearMiss selects majority-class samples based on their distance to minority samples — choosing either the closest (NearMiss-1) or the farthest (NearMiss-3). Unlike random undersampling, NearMiss keeps majority samples that are most relevant for defining the decision boundary. NearMiss-3 keeps majority samples farthest from all minority samples, creating a cleaner boundary region.
from imblearn.under_sampling import NearMiss
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=1000, weights=[0.9, 0.1], random_state=42)
print('Before:', np.bincount(y))
for version in [1, 2, 3]:
nm = NearMiss(version=version)
_, y_nm = nm.fit_resample(X, y)
print(f'NearMiss-{version}:', np.bincount(y_nm))Undersampling Inside an imblearn Pipeline
Like oversampling, undersampling must happen inside a pipeline to prevent leakage during cross-validation. Use imblearn.pipeline.Pipeline with the undersampler as a step before the classifier. The sampler's fit_resample is called on each training fold, and test folds are never modified.
from imblearn.pipeline import Pipeline as ImbPipeline
from imblearn.under_sampling import RandomUnderSampler
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.datasets import make_classification
import numpy as np
X, y = make_classification(n_samples=1000, weights=[0.95, 0.05], random_state=42)
pipe = ImbPipeline([
('sc', StandardScaler()),
('rus', RandomUnderSampler(random_state=42)),
('lr', LogisticRegression())
])
scores = cross_val_score(pipe, X, y, cv=5, scoring='f1')
print(f'CV F1 (RUS): {np.mean(scores):.4f} +/- {np.std(scores):.4f}')When to Choose Undersampling Over Oversampling
Prefer undersampling when: the majority class is enormous (millions of samples), training time is a bottleneck, or computational resources are constrained. Prefer oversampling (SMOTE) when: the minority class is very small (fewer than a few hundred samples), discarding majority data would make the training set too small, or the minority class has complex, non-convex structure that SMOTE can fill in. In practice, try both and compare on validation metrics.
Evaluating After Undersampling
After resampling, always evaluate on the original imbalanced test set — never on a resampled test set. Resampling the test data would give an unrealistic view of production performance. The goal is to improve the model's behaviour on the true data distribution, not to optimise for an artificial balanced distribution.
from sklearn.metrics import classification_report
from imblearn.under_sampling import RandomUnderSampler
from sklearn.linear_model import LogisticRegression
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=1000, weights=[0.95, 0.05], random_state=42)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, stratify=y, random_state=42)
sc = StandardScaler()
X_train_s = sc.fit_transform(X_train)
X_test_s = sc.transform(X_test)
rus = RandomUnderSampler(random_state=0)
X_r, y_r = rus.fit_resample(X_train_s, y_train)
lr = LogisticRegression().fit(X_r, y_r)
# Evaluate on ORIGINAL imbalanced test set
print(classification_report(y_test, lr.predict(X_test_s)))Quick Check
Test your understanding of undersampling and Cluster Centroids from this lesson.
Lesson Recap
In this lesson you learned: RandomUnderSampler randomly deletes majority samples — fast but risks losing important boundary information, ClusterCentroids replaces majority clusters with their centroids to preserve the class distribution more faithfully, and always evaluate on the original imbalanced test set to measure real-world model behaviour. Next up we explore class weighting and threshold moving as alternatives to resampling.
자주 묻는 질문
“무작위 언더샘플링과 ClusterCentroids” 강의는 무료인가요?
네 — “무작위 언더샘플링과 ClusterCentroids” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Machine Learning Academy 강의 전체를 잠금 해제할 수 있습니다. Machine Learning Academy 강의에는 총 4개의 강의가 포함되어 있습니다.
“무작위 언더샘플링과 ClusterCentroids”에서 뭘 배우나요?
학습자는 다수 클래스를 무작위 방식과 ClusterCentroids 방식으로 언더샘플링하고, 정보 손실과 그에 따른 모델 성능을 비교합니다. 브라우저에서 직접 실행하는 실습 코드로 Machine Learning Academy을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Machine Learning Academy을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Machine Learning Academy은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 3번째 강의입니다.
“무작위 언더샘플링과 ClusterCentroids” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Machine Learning Academy 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Machine Learning Academy 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 불균형 감지하기: 클래스 분포와 기준선의 함정
- 무작위 오버샘플링과 SMOTE
- 무작위 언더샘플링과 ClusterCentroids
- 클래스 가중치와 임계값 조정