SMOTE vs Class Weights: Imbalanced Classification

Five strategies for imbalanced classification with scikit-learn and imblearn: downsampling, upsampling, SMOTE, and class weights, judged on precision-recall.

Cover Image for SMOTE vs Class Weights: Imbalanced Classification

A fraud classifier reports 99.5% accuracy, but a check of its predictions shows that it labels every transaction as legitimate. If fraudulent transactions make up only 0.5% of the data, that score is possible without catching any fraud. Class imbalance makes the overall accuracy difficult to interpret, because the rare cases you care about can contribute so little to it.

Counting missed cases gives a different view of performance. The baseline catches 18% of minority cases. Resampling raises recall to about 82% to 88%, with lower precision. We compare downsampling, added examples, SMOTE and class weights, counting false alarms alongside missed cases.

Four Ways to Rebalance

Before we look at code, watch how four resampling strategies transform the same imbalanced training set. The original data has 1,324 majority samples (blue) and only 76 minority samples (red):

Animation showing four resampling strategies applied to an imbalanced dataset: original, random downsampling, random upsampling, and SMOTE

Downsampling discards majority samples to match the minority count (76 each). Upsampling duplicates minority samples to match the majority count (1,324 each). SMOTE creates new synthetic minority samples by interpolating between existing ones. Each approach changes the class balance the model sees during training, but the test set remains untouched.

Let's Build It

Click the badge to run this yourself:

Open In Colab

import numpy as np
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import f1_score, precision_score, recall_score
from imblearn.over_sampling import SMOTE
from collections import Counter

np.random.seed(42)

# Create an imbalanced dataset: 95% class 0, 5% class 1
X, y = make_classification(
    n_samples=2000, n_features=20, n_informative=10, n_redundant=5,
    n_classes=2, weights=[0.95, 0.05], flip_y=0.01, random_state=42
)
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)
print(f"Train: {Counter(y_train)}")  # {0: 1324, 1: 76}

# Indices for majority and minority classes
majority_idx = np.where(y_train == 0)[0]
minority_idx = np.where(y_train == 1)[0]

# Strategy 1: Random downsampling
down_idx = np.random.choice(majority_idx, size=len(minority_idx), replace=False)
X_down = np.vstack([X_train[down_idx], X_train[minority_idx]])
y_down = np.concatenate([y_train[down_idx], y_train[minority_idx]])

# Strategy 2: Random upsampling
up_idx = np.random.choice(minority_idx, size=len(majority_idx), replace=True)
X_up = np.vstack([X_train[majority_idx], X_train[up_idx]])
y_up = np.concatenate([y_train[majority_idx], y_train[up_idx]])

# Strategy 3: SMOTE
X_smote, y_smote = SMOTE(random_state=42).fit_resample(X_train, y_train)

# Strategy 4: Class weights (no resampling needed)
# Train and evaluate all strategies
strategies = {
    'Baseline':      (X_train, y_train, {}),
    'Downsample':    (X_down, y_down, {}),
    'Upsample':      (X_up, y_up, {}),
    'SMOTE':         (X_smote, y_smote, {}),
    'Class Weights': (X_train, y_train, {'class_weight': 'balanced'}),
}

for name, (X_tr, y_tr, kwargs) in strategies.items():
    clf = LogisticRegression(max_iter=1000, random_state=42, **kwargs)
    clf.fit(X_tr, y_tr)
    pred = clf.predict(X_test)
    print(f"{name:<15} Prec={precision_score(y_test, pred):.3f}  "
          f"Rec={recall_score(y_test, pred):.3f}  "
          f"F1={f1_score(y_test, pred):.3f}  "
          f"Acc={np.mean(pred == y_test):.3f}")
Train: Counter({0: 1324, 1: 76})
Baseline        Prec=0.545  Rec=0.182  F1=0.273  Acc=0.947
Downsample      Prec=0.185  Rec=0.879  F1=0.305  Acc=0.780
Upsample        Prec=0.222  Rec=0.848  F1=0.352  Acc=0.828
SMOTE           Prec=0.245  Rec=0.818  F1=0.378  Acc=0.852
Class Weights   Prec=0.228  Rec=0.848  F1=0.359  Acc=0.833

Bar chart comparing precision, recall, and F1 score across all five strategies

The baseline has the highest accuracy (94.7%) but the worst F1 score (0.27). It catches only 18% of minority samples. Every resampling strategy dramatically improves recall (to 82-88%) at the cost of precision. SMOTE achieves the best F1 (0.38) by creating synthetic minority samples that generalise better than simple duplication.

What Just Happened?

The Accuracy Paradox

Chart showing accuracy versus F1 score for each method, with a dashed line at 94.5% showing the accuracy of a trivial all-negative classifier

The baseline model has 94.7% accuracy, barely above the 94.5% you'd get by predicting the majority class for everything. That 0.2 percentage point "improvement" over the trivial classifier is misleading. The resampling strategies all have lower accuracy (78-85%) but higher F1, because they sacrifice correct majority-class predictions to catch more minority samples. With imbalanced data, accuracy is almost useless as a metric.

Precision vs Recall: The Fundamental Tradeoff

Every binary classifier faces a tradeoff between two error types:

  • Precision answers: "Of all the samples I flagged as positive, how many actually are?" High precision means few false alarms.
  • Recall answers: "Of all the actual positives, how many did I catch?" High recall means few missed cases.

The F1 score is the harmonic mean of precision and recall:

F1=2⋅Precision⋅RecallPrecision+RecallF_1 = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}}

The harmonic mean punishes extreme imbalances: if either precision or recall is near zero, F1 will also be near zero. This makes it a much better single metric for imbalanced problems than accuracy.

Why Resampling Shifts the Tradeoff

A logistic regression model outputs probabilities. The default decision threshold is 0.5: predict class 1 if P(class=1∣x)>0.5P(\text{class} = 1 \mid \mathbf{x}) > 0.5. On imbalanced data, the model rarely outputs probabilities above 0.5 for the minority class, because it has learned that class 1 is rare. Most predictions stay below the threshold, so recall is low.

Resampling changes the class proportions the model sees during training, which shifts the predicted probabilities upward for the minority class. More predictions cross the 0.5 threshold, catching more true positives (higher recall) but also more false positives (lower precision).

What Each Strategy Does

Random downsampling discards majority samples until the classes are balanced. This is the approach from the original R code, which used caret::downSample(). It is fast and simple, but throws away potentially useful data. With only 76 samples per class (152 total), the model has much less to learn from.

Random upsampling duplicates minority samples until the classes are balanced. No data is lost, but the duplicated samples add no new information. The model may overfit to specific minority instances.

SMOTE (Synthetic Minority Oversampling Technique) creates new minority samples by interpolating between existing ones. For each minority sample, it finds its k-nearest minority neighbours and generates synthetic points along the line segments connecting them.

Class weights adjust the loss function instead of the data. Setting class_weight='balanced' in scikit-learn multiplies the loss for each class by n/(2⋅nk)n / (2 \cdot n_k), where nkn_k is the count of class kk. The minority class gets a weight of 1324/76≈17.4×1324 / 76 \approx 17.4\times higher, making each minority sample count as much as 17 majority samples in the gradient update.

Confusion Matrices: Where the Errors Go

Five confusion matrices side by side, one for each strategy

The baseline gets 562 out of 567 negatives right but only 6 out of 33 positives. Downsampling flips the balance: it catches 29 out of 33 positives but misclassifies 128 negatives. SMOTE finds a middle ground: 27 true positives with only 83 false positives.

Going Deeper

How SMOTE Works

Diagram showing SMOTE generating a synthetic sample by interpolating between a minority sample and its k-nearest neighbour

SMOTE generates each synthetic sample in three steps:

  1. Pick a minority sample xi\mathbf{x}_i
  2. Find one of its kk nearest minority neighbours xnn\mathbf{x}_{nn} (default k=5k = 5)
  3. Create a new sample at a random point on the line segment between them:
xnew=xi+λ⋅(xnn−xi),λ∼U(0,1)\mathbf{x}_{new} = \mathbf{x}_i + \lambda \cdot (\mathbf{x}_{nn} - \mathbf{x}_i), \quad \lambda \sim U(0, 1)

This fills in the feature space around existing minority samples rather than simply duplicating them. The synthetic samples are plausible minority examples that the model has not seen before, which reduces overfitting compared to random upsampling.

SMOTE has known failure modes. If minority samples are scattered among majority samples (rather than forming clusters), the interpolated points may land in majority-class territory, creating noisy synthetic examples. Variants like Borderline-SMOTE (only oversample minority points near the decision boundary) and SMOTE-ENN (apply Edited Nearest Neighbours after SMOTE to clean noisy samples) address this.

Precision-Recall Curves: The Full Picture

The single-threshold metrics above (precision, recall, F1) depend on the 0.5 decision threshold. The precision-recall curve traces all possible thresholds, showing the tradeoff across the full range:

Precision-recall curves for all five strategies, with average precision scores in the legend

The average precision (AP) summarises each curve as a single number. Interestingly, the baseline has the highest AP (0.373), meaning it ranks positives higher on average. Resampling strategies improve recall at a fixed threshold but do not necessarily improve the overall ranking. This is an important nuance: resampling shifts the decision boundary, but it does not change the model's ability to separate classes.

When should you use ROC curves vs precision-recall curves? ROC curves can be misleading on imbalanced data because the false positive rate (x-axis) stays low even with many false positives when the negative class is huge. Precision-recall curves focus entirely on the minority class and are the preferred evaluation tool for imbalanced problems.

The Prentice-Pyke Result: Why Downsampling Works

The original R code referenced a remarkable theoretical result from case-control studies in epidemiology. Prentice & Pyke (1979) proved that for logistic regression, downsampling the majority class preserves all estimated coefficients except the intercept.

This means the model's understanding of which features matter (and by how much) is unaffected by downsampling. Only the baseline probability changes. If you know the true population prevalence, you can correct the intercept analytically:

β0corrected=β0downsampled+log⁡(π11−π1)−log⁡(π~11−π~1)\beta_0^{\text{corrected}} = \beta_0^{\text{downsampled}} + \log\left(\frac{\pi_1}{1 - \pi_1}\right) - \log\left(\frac{\tilde{\pi}_1}{1 - \tilde{\pi}_1}\right)

Where π1\pi_1 is the true prevalence and π~1\tilde{\pi}_1 is the prevalence in the downsampled data (0.5 for balanced). This result only holds for logistic regression; it does not generalise to random forests, SVMs, or neural networks.

Choosing a Strategy

There is no universally best approach. The choice depends on your constraints:

StrategyBest WhenWatch Out For
DownsamplingMajority class is very large; compute is limitedLoses useful majority-class information
UpsamplingMinority samples are high-quality; small datasetOverfitting to duplicated samples
SMOTEFeatures are continuous; minority forms clustersNoisy synthetic samples if classes overlap
Class weightsYou want simplicity; no external library neededOnly affects the loss, not the data distribution
Do nothingYou have enough minority samples; using AP or PR-AUCThreshold-based metrics (F1) will look bad

For most practical problems, start with class_weight='balanced' (simplest, no data manipulation) and compare against SMOTE. If neither satisfies your precision-recall requirements, consider threshold tuning on the original model's predicted probabilities, which can be more effective than any resampling.

Try It Yourself

  1. Tune the threshold. Instead of resampling, train the baseline model and sweep the decision threshold from 0.1 to 0.9. Plot precision and recall as a function of threshold. Can you find a threshold that gives better F1 than SMOTE?
  2. Try Borderline-SMOTE. Replace SMOTE() with BorderlineSMOTE() from imbalanced-learn. Does it improve F1 on this dataset?
  3. Use a tree-based model. Replace logistic regression with RandomForestClassifier. Do the resampling strategies still help, or do tree models handle imbalance better natively? See our combining-models post for the bagging-vs-boosting trade-off and how XGBoost's scale_pos_weight parameter compares to SMOTE on the same kind of skewed data.

Where This Comes From

Class imbalance as a research topic gained traction in the late 1990s when machine learning was increasingly applied to fraud detection, medical diagnosis, and fault detection, all domains where the event of interest is rare. The two foundational contributions came from very different fields.

Prentice & Pyke (1979) proved the theoretical basis for downsampling in the context of case-control studies, the standard design in epidemiology for studying rare diseases. Their key result, that retrospective sampling preserves the odds ratio estimates in logistic regression, gave practitioners confidence that downsampling was not merely a computational shortcut but a theoretically sound approach.

"Under a wide variety of designs in which sampling of data records is dependent on the outcome variable, estimation via the prospective logistic model yields valid estimates of the relevant odds ratios."

Prentice, R. L. & Pyke, R. (1979). Logistic Disease Incidence Models and Case-Control Studies. Biometrika, 66(3), 403-411.

Chawla et al. (2002) introduced SMOTE, which became the most widely cited paper on class imbalance handling in machine learning. They demonstrated that creating synthetic minority samples by interpolation outperformed random oversampling across multiple datasets and classifiers. The paper has been cited over 25,000 times.

"Our method of synthetic minority over-sampling technique (SMOTE) works by creating synthetic examples from the minor class instead of by over-sampling with replacement."

Chawla, N. V., Bowyer, K. W., Hall, L. O. & Kegelmeyer, W. P. (2002). SMOTE: Synthetic Minority Over-sampling Technique. JAIR, 16, 321-357.

Further Reading

Interactive Tools

Stay in the loop

Get notified when new tutorials and deep-dives are published. No spam, unsubscribe anytime.

Related Posts

Frequently Asked Questions