Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 3

Loan Data Preprocessing Strategy

Recommended resources

Introduction

Hello and welcome to the second module of our project. In the previous lesson, we established a professional evaluation framework for our loan default model. We moved beyond simple accuracy to cost-sensitive metrics like Sensitivity, Specificity, and most importantly, Expected Monetary Loss (EML). We also identified a key characteristic of our data: as a rare event, loan default creates a class imbalance, with far more "good" loans than "bad" ones.

This lesson marks our transition from planning to hands-on data work. A well-defined strategy for data preparation is the bedrock of a robust model. Today, we will develop a comprehensive strategy for handling the three most common challenges in real-world datasets: missing values, outliers, and the class imbalance we've already noted. Your experience with data science projects will affirm that these cleaning and preparation steps are not just procedural checks, but strategic decisions that profoundly impact model performance and reliability.

By the end of this 60-minute lesson, you will be able to develop and justify a data preparation strategy for a credit risk dataset, covering missing values, outliers, and class imbalance.

Loan Default Prediction Using Machine Learning Workflow
This workflow illustrates the end-to-end process of a loan default prediction project. Our focus today is squarely on the "Data Preprocessing" step, which is foundational to everything that follows.

1. A Strategic Approach to Missing Values

The first step in data preparation is to identify and handle missing data. Our goal is not simply to fill every blank cell, but to make reasoned decisions based on the business context and the nature of the data.

First, let's watch a brief segment of a project walkthrough to see how missing data is identified and handled in a practical credit risk scenario.

Credit Risk Modeling Using Machine Learning – Full Python Data Science Project (Step-by-Step)

This video quickly demonstrates the identification of missing values and a pragmatic business decision for handling them.

Watch the video from 05:55 to 06:58 and from 10:31 to 11:24. Notice the justification for dropping rows with missing 'saving accounts' or 'checking accounts' data. This is a great example of a business-driven data preparation choice.

As the video shows, sometimes the best strategy is deletion. For saving_accounts and checking_accounts, the absence of data is highly significant. A bank would likely not approve a loan for an applicant whose account information is unavailable. Trying to impute—or guess—this information could introduce serious noise and risk. In this case, dropping the records is a defensible decision that mimics a real-world business rule.

However, we can't drop records for every missing value, especially if it means losing a large portion of our dataset. For other features, we need a more nuanced imputation strategy. A professional approach involves creating a tiered strategy based on the percentage of missing data for each feature.

Building a Credit Score Model: Handling Missing Values and Outliers

This article provides an excellent, structured strategy for handling missing values based on how much data is missing. This is a far more robust approach than applying a single method to all features.

Please read the sections 'Check Missing Value Percentage', 'Divide Numerical & Categorical Features', 'Handling Strategies Based on Missing Rate', 'Handling Missing Numerical Features', and 'Handling Missing Categorical Features'. Focus on the logic behind the three tiers (<5%, 5-30%, >30%) and the different methods applied to each.

Based on this reading, here is the professional strategy we will adopt:

  1. Quantify the Problem: First, we'll calculate the percentage of missing values for every feature.
  2. Separate Feature Types: We'll divide our features into numerical and categorical groups, as they require different imputation methods.
  3. Apply a Tiered Strategy:
    • Low Missing Rate (<5%): For these features, simple and fast imputation is effective.
      • Numerical: Impute with the median. We prefer the median over the mean because financial data (like income or loan amounts) is often skewed, and the median is robust to extreme values.
      • Categorical: Impute with the mode (the most frequent value).
    • Moderate Missing Rate (5% - 30%): Here, a simple imputation might introduce bias. A more sophisticated approach is better.
      • Numerical: Use model-based imputation. For example, we can train a small regression model (like LightGBM, as shown in the article) to predict the missing values based on other features in the dataset. This is more accurate than just using the median.
      • Categorical: Can also use predictive imputation or fill with a special category like "Unknown", which the model can learn from.
    • High Missing Rate (>30%): A feature missing this much data is often a candidate for removal. Imputing such a large amount of data is unreliable. However, before dropping it, we must check its correlation with the target variable. If it's highly predictive, we might retain it and use an advanced imputation method or treat its absence as a separate category.

This structured approach ensures that our choices are efficient, justified, and tailored to the specific characteristics of each feature.


2. Tackling Outliers in Financial Data

Outliers are extreme values that lie far from the rest of the data points. In credit risk data, they can be genuine (e.g., a CEO's salary) or errors (e.g., a typo in age). If left untreated, they can skew statistical calculations and mislead our models.

The article we just reviewed also contains an excellent section on a domain-specific strategy for handling outliers.

Building a Credit Score Model: Handling Missing Values and Outliers

Let's return to the same article to see how to handle outliers with business context in mind.

Read the section 'Detecting and Handling Outliers' and 'Handle Outlier'. Pay close attention to how the strategy changes for different types of features (Financial, Employment, Demographic).

The key insight here is that not all outliers are treated the same. The handling strategy should depend on the feature's meaning.

  • Detection: A standard method for detecting outliers is the Interquartile Range (IQR) method. Any value that falls below Q1 - 1.5 * IQR or above Q3 + 1.5 * IQR is flagged as an outlier.

  • Handling Strategies:

    • Capping (Winsorization): For key financial features like AMT_INCOME_TOTAL or AMT_CREDIT, we don't want to remove high values, as high income or a large loan is important information. Instead, we "cap" the outliers by setting any value above the 99th percentile to the 99th percentile value (and similarly for the 1st percentile). This reduces the outlier's skewing effect while retaining the information that the value is at the extreme high end.
Impact of Outlier Capping on Loan Amount vs. Person Age
This image illustrates the effect of outlier capping. On the left, we see the original data with extreme values for 'Person Age'. On the right, these extreme values have been capped at a maximum threshold, bringing them in line with the main data distribution without deleting them.
*   **Log Transformation:** For highly skewed numerical data, like `DAYS_EMPLOYED`, applying a logarithmic transformation (`np.log1p`) can compress the range of the feature. This makes the distribution more normal, reduces the influence of extreme values, and often helps linear models find better patterns.

*   **Removal:** This is a last resort. We would only remove an outlier if we have strong evidence that it is a data entry error (e.g., an age of 200) and not a genuine, albeit extreme, value.

This context-aware approach is a hallmark of professional data science practice in the financial domain.


3. Addressing Class Imbalance

We've touched on this already: our dataset will have many more "good" (non-default) loans than "bad" (default) ones. This is a problem because most machine learning algorithms are designed to maximize overall accuracy, so they can become biased. A naive model might achieve 95% accuracy by simply predicting "good" for everyone, completely failing to identify the high-risk applicants we care about most.

To develop our strategy, let's first get a clear, conceptual understanding of the problem and the main data-level solutions.

Handling Imbalanced Dataset in Machine Learning: Easy Explanation for Data Science Interviews

This video provides an excellent and easy-to-understand explanation of why class imbalance is a problem and introduces the primary methods for dealing with it at the data level.

Watch from 01:33 to 08:07. Focus on two key points: 1) Why accuracy is misleading for imbalanced data, and 2) The core concepts, pros, and cons of undersampling, oversampling, and SMOTE.

As the video explains, we have several strategies available at the data-sampling level.

  • Random Undersampling:

    • What it is: Deleting random samples from the majority class ("good" loans) until the classes are balanced.
    • Pros: Can make training faster on very large datasets.
    • Cons: High risk of losing important information. We might discard examples of "good" customers who are subtly different from others. This is generally not the preferred approach unless the dataset is massive.
  • Random Oversampling:

    • What it is: Duplicating random samples from the minority class ("bad" loans) until the classes are balanced.
    • Pros: No information is lost.
    • Cons: Since we are just copying data, the model can easily overfit to these repeated examples, leading to poor performance on new, unseen data.
  • SMOTE (Synthetic Minority Oversampling Technique):

    • What it is: A more intelligent form of oversampling. Instead of duplicating existing data, SMOTE creates new, synthetic minority class samples. It does this by selecting a minority data point, finding its nearest neighbors (also in the minority class), and creating a new point along the line segment connecting them.
    • Pros: Creates a richer, more diverse dataset for the model to learn from without simply duplicating information. This helps prevent overfitting and often leads to better generalization.
    • Cons: Can create noise if the minority class samples are very sparse or overlapping with the majority class. However, it is one of the most effective and widely used techniques.

For our project, SMOTE is the recommended strategy. It directly addresses the weakness of simple oversampling and is a standard, robust technique for credit risk modeling. We will plan to integrate SMOTE into our modeling pipeline in the next module.


Conclusion

In this lesson, we have moved from high-level planning to defining a concrete, professional-grade data preparation strategy. We recognized that data cleaning is not a rote exercise but a series of justified decisions that enhance the final model's quality.

Key Takeaways:

  • Missing Values Strategy: Employ a tiered approach. Delete rows only with strong business justification. For imputation, use simple methods (median/mode) for low missing rates and advanced, model-based methods for moderate rates. Drop features with high missing rates unless they are highly predictive.
  • Outlier Handling Strategy: Base your approach on the feature's business context. Use capping (Winsorization) for financial features to limit their influence without losing information, and use log transformations to handle skewed distributions.
  • Class Imbalance Strategy: To combat model bias towards the majority class, we will use a data-level resampling technique. We have chosen SMOTE (Synthetic Minority Oversampling Technique) as it creates new, synthetic data points, which is a more robust approach than simple over- or undersampling.

Next Steps:

With a solid strategy for data preparation in place, we are ready to tackle the more creative side of preprocessing. In the next lesson, we will focus on feature engineering and selection. We'll learn how to create new, powerful predictors from the existing data (e.g., debt-to-income ratio) and select the most impactful features for our model.