Introduction
Welcome back. In our previous lesson, we finalized our data preparation strategy by engineering domain-specific features and selecting the most predictive ones using Information Value (IV). We now have a clean, powerful, and interpretable set of features ready for modeling.
This lesson tackles a critical and common challenge in credit risk analytics: class imbalance. In our loan default dataset, as in the real world, the number of "default" cases (the minority class) is far smaller than "non-default" cases (the majority class). If ignored, this imbalance will cause our model to become biased, leading it to perform poorly on the very cases we are most interested in predicting.
Our learning outcome for this lesson is to implement a modeling pipeline that incorporates a technique to address class imbalance (e.g., SMOTE). We will not only learn the mechanics of this technique but also focus on the professional best practice for integrating it into a validation framework to avoid a serious error known as data leakage.
1. The Challenge of Imbalance and the SMOTE Solution
A model trained on an imbalanced dataset, optimizing for overall accuracy, will often learn a simple but useless rule: always predict the majority class. For loan defaults, this would mean a model that predicts every applicant will repay their loan. While this might be 99% accurate, it fails completely at its business objective—identifying risk. This is why in our first lesson, we chose metrics like Recall and F1-score, which are sensitive to performance on the rare, positive class.
To address the data imbalance itself, we will use an oversampling technique. Instead of simply duplicating existing minority class samples (random oversampling), we will use the Synthetic Minority Over-sampling Technique (SMOTE).
SMOTE is a more intelligent approach. It creates new, synthetic data points for the minority class by interpolating between existing minority instances.

To get a deeper intuition for how SMOTE works, let's watch a brief conceptual overview.
SMOTE (Synthetic Minority Oversampling Technique) for Handling Imbalanced Datasets
This video, from the channel Bhavesh Bhatt, provides an excellent, clear explanation of the logic behind SMOTE, using nearest neighbors to generate new data points.
Watch from the beginning to 06:17. Focus on how SMOTE moves from identifying nearest neighbors in the minority class to creating new synthetic instances on the vector between them.
By generating these new samples, SMOTE provides the model with a richer, more balanced training set, allowing it to learn the characteristics of the minority class more effectively without the risk of overfitting that comes from simple duplication.
2. The Critical Pitfall: Data Leakage from Incorrect Sampling
Now we arrive at one of the most important professional practices in this workflow. A common and serious mistake is to apply SMOTE to the entire dataset before performing a train-test split or cross-validation.
Why is this wrong?
When you generate synthetic samples before splitting your data, you are creating new data points based on information from the entire dataset. When you then split this altered dataset, it's highly likely that some of the original points used to create the synthetic data will be in the training set, while the synthetic points themselves land in the test set. This means your model is being tested on data that is artificially similar to data it was trained on.
This "leaks" information from the test set into the training process, leading to performance metrics (like accuracy or F1-score) that are unrealistically high and not representative of how the model will perform on truly unseen data.
The following video does an outstanding job of demonstrating both the wrong and the right way to do this, showing the dramatic and misleading impact of data leakage.
Machine Learning - Over-& Undersampling - Python/ Scikit/ Scikit-Imblearn
In this segment from Johannes Frey's tutorial, you'll see a practical demonstration of data leakage caused by incorrect sampling and learn the correct approach using a pipeline.
Watch from 11:34 to 18:07. Pay close attention to the comparison between the 'wrong' way (sampling before splitting) and the 'right' way (sampling within the cross-validation fold). Notice how the metrics from the 'wrong' way are dramatically—and falsely—inflated.
The key takeaway is unambiguous: Resampling techniques must only ever be applied to the training data. The validation and test sets must remain untouched to serve as an unbiased measure of the model's generalization performance.
3. Implementing a Correct and Robust SMOTE Pipeline
The correct way to integrate SMOTE into a modeling workflow, especially one involving cross-validation, is to use a pipeline. The imbalanced-learn library, which provides SMOTE, also provides a Pipeline object designed for this exact purpose.
This special pipeline ensures that when data is passed to it (for example, during a cross-validation fold), the resampling (SMOTE) is applied only to the training portion of that data, just before the model is trained. The validation portion is then used to score the model without ever having been seen by the SMOTE process.
Let's look at how to construct this pipeline in Python. We will be combining our SMOTE step with a LogisticRegression classifier, which will serve as our baseline model in the next lesson.
First, let's review the necessary preprocessing steps and the train-test split that lead up to our pipeline.
Credit Card Fraud Detection in Python
This article on credit card fraud detection walks through the necessary data preparation steps before modeling, which are analogous to our loan default project.
Please read the 'Data Preparation' section. This will be a quick review, as it covers concepts like scaling and train-test split that we've discussed. Note the use of RobustScaler and train_test_split. This sets the stage for where our pipeline will fit in.
With our data split into X_train, X_test, y_train, and y_test, we can now define our robust modeling pipeline.
Here is the code structure for building and using the pipeline.
# Import necessary libraries
from imblearn.pipeline import Pipeline as ImbPipeline
from imblearn.over_sampling import SMOTE
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import StratifiedKFold, cross_val_score
import numpy as np
# Assume X_train, y_train are your training data from the previous split
# 1. Define the SMOTE-enhanced pipeline
# This pipeline chains the SMOTE sampler and the Logistic Regression classifier.
pipeline = ImbPipeline([
('smote', SMOTE(random_state=42)),
('classifier', LogisticRegression(random_state=42, solver='liblinear'))
])
# 2. Define the cross-validation strategy
# StratifiedKFold is essential for imbalanced datasets to ensure that each
# fold has a proportional representation of each class.
cv = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
# 3. Evaluate the pipeline using cross-validation
# When cross_val_score is called, for each split, the pipeline will:
# a. Apply SMOTE ONLY on the training part of the split.
# b. Train the LogisticRegression model on the new, balanced training data.
# c. Evaluate the trained model on the untouched validation part of the split.
# We use 'f1_macro' as our scoring metric, aligned with our project goals.
scores = cross_val_score(pipeline, X_train, y_train, scoring='f1_macro', cv=cv, n_jobs=-1)
# 4. Report the performance
print(f"F1 Macro Scores for each fold: {scores}")
print(f"Average F1 Macro Score: {np.mean(scores):.4f}")
print(f"Standard Deviation of F1 Macro Score: {np.std(scores):.4f}")
This code block represents a professional, robust, and correct approach to training a model on imbalanced data. By encapsulating the logic within the imblearn pipeline, you prevent data leakage and get a realistic estimate of your model's performance.
Conclusion
In this lesson, we tackled the critical issue of class imbalance. We went beyond simply identifying the problem and implemented a robust, industry-standard solution.
Key Takeaways:
- Class Imbalance Biases Models: Standard models trained on imbalanced data tend to favor the majority class, making them ineffective for rare-event prediction like loan defaults.
- SMOTE Creates Synthetic Data: SMOTE addresses imbalance by generating new, synthetic minority class instances, providing a richer dataset for the model to learn from.
- Prevent Data Leakage with Pipelines: The single most important implementation detail is to never apply resampling before splitting your data. The correct method is to integrate the sampler (e.g., SMOTE) and the classifier into a pipeline, ensuring resampling only happens on the training data within each cross-validation fold.
imblearn.pipelineis the Right Tool: ThePipelinefrom theimbalanced-learnlibrary is specifically designed for this purpose and is the standard tool for building leak-proof resampling workflows.
Next Steps:
We now have a complete, robust, and validated pipeline for training a model on our data. In the next lesson, Comparative Modeling and Selection, we will put this pipeline to work. We will use it to train and evaluate not only our baseline LogisticRegression model but also a more complex XGBoost model. We will then compare their performance using our business-focused metrics to select the final champion model for our project.