End to End Case Study (Classification): Lending Club data
By: Pawan Reddy Ulindala
Tags: Classification, Decision Tree, Python, Machine Learning, Case Study

In fact, 63.15% of the values in the overall data are null values. So, it is very important to carefully deal with these null values as they can significantly affect our results.

Null values visual plot:

Handling null values:

Handling null values is an important task here. In the below code, you can see that there are only 53 columns out of 144 columns that have null values less than 40 percent.

Table-1

In the above table, each row represents the number of columns out of 144 columns with less than a specific percentage of null values. For example, Row 1 represents that there are 52 columns with less than 10% of null values in each column.

We were able to decrease the total number of columns from 144 to 53 by considering columns with less than 40% of null values.

Understanding Features

It is important to understand the features/columns as some of the categorical columns present in the data are in the form of numerical values and vice-versa. I first tried to examine every column but later understood that it will be quite cumbersome to perform these operations to all 53 columns. So, I decided to first eliminate columns which doesn’t add value to the data and then analyze each field.

Checking objects:

Dropping unnecessary objects:

Checking numerical columns:

Dropping unnecessary numerical columns:

After examining the data, we have dropped a total of 18 columns of these 53 that didn’t add value to our data. We were able to decrease number of columns from 53 to 35 and we will still try to decrease the no.of columns.

Converting categorical columns to numerical columns:

We have converted categorical columns to numerical by either performing one-hot encoding or label encoding depending on the kind of data they represent. For example, one hot encoding was performed on [‘home_ownership’,’verification_status’,’purpose’] columns whereas label encoding was performed on ‘grade’ and ‘sub grade’ columns as they are ordinal in nature.

One hot encoding:

Label encoding:

Updating the grade column with label encoded values:

Converting DateTime columns to numerical columns:

The columns [‘issue_d’,’last_pymnt_d’,’last_credit_pull_d’] which are datetime columns are further divided into month and year by using pandas datetime module. The new columns are named as ‘issue_d_year’, ‘issue_d_month’, ‘last_pymnt_d_year’, ‘last_pymnt_d_month’, ‘last_credit_pull_d_year’, ‘last_credit_pull_d_month’ respectively.

Converting objects to numerical columns:

The columns int_rate and term are stored as objects. We have performed necessary string operations to convert them into numerical columns.

Checking correlation: Now that we have converted all the columns to numerical columns, we will check for correlation.

Correlation heatmap

There are few columns with high correlation but these columns haven’t been considered while solving our questions. For example, when trying to classify if the loan will be paid back by the customer we will not consider any future transactions like total_pymnt and total_pymnt_inv. Hence, these columns aren’t dropped here.

Dealing with null values:

Let’s check if there are any null values after significantly cleaning columns.

Null values plot

As we can see, we can still find some null values in the data. We will examine these null values and take the necessary actions.

Let’s check the columns with which have the highest percentage of null values.

Percentage of null values

Some columns have a very little percentage of null values(less than 1%). There we can replace the null values with the median of their respective columns.

For columns that have a high percentage of null values, we will run a model on top of non-null values and predict the missing values in that respective column.

As there are no null values, we will go to the next step i.e., building a machine learning model.

Classification

The goal of our classification task is to identify whether a customer(who is requesting a loan) will be able to repay the loan along with the interest amount. Since we have some columns that contain the information of future transactions w.r.t the date on which the loan is taken(like paying monthly loan installments after taking the loan etc.), we will drop them from the pre-processed dataset to carry on classification tasks.

The goal of our classification task is to identify whether a customer(who is requesting a loan) will default based on his historic transactions with the lender after taking the loan.

Let’s drop few columns that contain information on charged-off loans.

Columns dropped in classification:

[‘total_pymnt’,’total_pymnt_inv’,’total_rec_prncp’,’total_rec_int’,’total_rec_late_fee’,’recoveries’]

The ‘loan_status’ column is used as a target variable to classify a customer based on his records.‘loan_status’ column has 4 unique values all of which are label encoded for ease of representation. This column is labeled as below:

Checking multicollinearity between features using VIF and then dropping columns with a value higher than the threshold.

We have dropped the columns which have a high VIF factor(10 or above).

Model building

After label encoding the target variable, we have split the data to train and test data in the ratio of 70:30.

We used sklearn’s cross_val_score and grid search cv with scoring as f1 score to examine the performance of each model in each fold. The below figure shows the F1 score of each model in 3 folds. The orange line represents the mean F1 score of each model whereas IQR represents the variance of these scores.

From the above figure, we can say that the bagging classifier is the most stable model with the highest mean of weighted F1 scores and least variance.

Building the model using bagging classifier.

The classification report for bagging classifier

This model has an accuracy of 0.89 and an average F1 score of 0.75.

Final Confusion matrix:

Confusion Matrix of Bagging Classifier model

As the bagging Classifier doesn’t have an option for feature importance, we used a decision tree to find feature importances.

Feature Importance’s of Decision Tree model

Conclusion:

In this blog, we have extensively covered pre-processing steps required for this data and then found the best fit model using Grid search and KFolds. I hope that this blog has given you an overall picture of solving a classification problem. For more detailed code, please refer to https://github.com/pawanreddy-u/lendingclub9