Credit Card Customer Churn Detection Using Machine Learning Algorithms
Credit Card Fraud Data
https://data.world/vlad/credit-card-fraud-detection
The rapid growth of the banking industry has allowed consumers to be more discerning about the banks they want to maintain relationships with. Thus, customer retention has become a significant concern for many financial institutions. One particular area where customer retention is particularly significant is in the realm of credit cards. High churn rates, the rate at which customers stop doing business with an entity, can lead to significant revenue losses and higher acquisition costs for new customers. This project aims to predict credit card customer churn, to help banks identify and retain customers at risk of churning.
Customer churn in the banking sector, particularly in credit cards, is a persistent issue. Predicting churn can be a complex task due to the multitude of factors that can influence a customer’s decision to leave, including customer service quality, better offerings from competitors, changes in customer financial circumstances, and more. Despite the advent of advanced data analytics techniques, many banks still struggle to predict and mitigate customer churn effectively. This project will focus on this problem, attempting to develop a model that can accurately predict customer churn and thus provide valuable insights to help banks retain their valuable credit card customers.
To understand the factors that contribute to credit card customer churn: The first step in addressing the problem is understanding its root causes. We will investigate various factors like customer demographics, card usage, and other relevant variables to gain insights into what drives churn.
To develop a predictive model for credit card customer churn: Leveraging machine learning techniques, we will develop a model that can predict the likelihood of a customer churning. This objective will involve data preprocessing, model selection, training, and validation steps.
To provide recommendations for customer churn reduction strategies: Based on the findings from the predictive model and our understanding of churn factors, we will propose strategies that banks could implement to reduce churn rates and improve customer loyalty..
What are the key factors influencing credit card customer churn?: Identifying the main predictors of churn will be the first step in developing our predictive model and provides a foundation for our churn reduction recommendations.
How accurately can we predict credit card customer churn?: This question will guide our model development and evaluation process. It will involve assessing the performance of our predictive model using suitable metrics.
What strategies can banks implement to reduce churn rates among credit card customers?: This question will be addressed in the final part of our research, where we will use our findings to suggest actionable strategies for churn reduction.
By investigating these research questions, this project aims to contribute to the understanding of credit card customer churn and provide tangible benefits to the banking industry by suggesting potential solutions.
# Loading necessary libraries
library(readxl)
library(ggplot2)
library(dplyr)
library(corrplot)
library(hexbin)
library(plyr)
library(tidyr)
library(purrr)
library(gridExtra)
library(ggrepel)
library(pastecs)
library(caret)
#library(ROSE)
library(randomForest)
library(e1071)
library(rpart)
library(rpart.plot)
c_data <- read.csv('BankChurners.csv')
head(c_data, 3)
## CLIENTNUM Attrition_Flag Customer_Age Gender Dependent_count
## 1 768805383 Existing Customer 45 M 3
## 2 818770008 Existing Customer 49 F 5
## 3 713982108 Existing Customer 51 M 3
## Education_Level Marital_Status Income_Category Card_Category Months_on_book
## 1 High School Married $60K - $80K Blue 39
## 2 Graduate Single Less than $40K Blue 44
## 3 Graduate Married $80K - $120K Blue 36
## Total_Relationship_Count Months_Inactive_12_mon Contacts_Count_12_mon
## 1 5 1 3
## 2 6 1 2
## 3 4 1 0
## Credit_Limit Total_Revolving_Bal Avg_Open_To_Buy Total_Amt_Chng_Q4_Q1
## 1 12691 777 11914 1.335
## 2 8256 864 7392 1.541
## 3 3418 0 3418 2.594
## Total_Trans_Amt Total_Trans_Ct Total_Ct_Chng_Q4_Q1 Avg_Utilization_Ratio
## 1 1144 42 1.625 0.061
## 2 1291 33 3.714 0.105
## 3 1887 20 2.333 0.000
## Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_1
## 1 9.3448e-05
## 2 5.6861e-05
## 3 2.1081e-05
## Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_2
## 1 0.99991
## 2 0.99994
## 3 0.99998
names(c_data)
## [1] "CLIENTNUM"
## [2] "Attrition_Flag"
## [3] "Customer_Age"
## [4] "Gender"
## [5] "Dependent_count"
## [6] "Education_Level"
## [7] "Marital_Status"
## [8] "Income_Category"
## [9] "Card_Category"
## [10] "Months_on_book"
## [11] "Total_Relationship_Count"
## [12] "Months_Inactive_12_mon"
## [13] "Contacts_Count_12_mon"
## [14] "Credit_Limit"
## [15] "Total_Revolving_Bal"
## [16] "Avg_Open_To_Buy"
## [17] "Total_Amt_Chng_Q4_Q1"
## [18] "Total_Trans_Amt"
## [19] "Total_Trans_Ct"
## [20] "Total_Ct_Chng_Q4_Q1"
## [21] "Avg_Utilization_Ratio"
## [22] "Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_1"
## [23] "Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_2"
# Remove duplicates
c_data <- unique(c_data)
# Check for null values in each column
null_counts <- sapply(c_data, function(x) sum(is.na(x)))
print(null_counts)
## CLIENTNUM
## 0
## Attrition_Flag
## 0
## Customer_Age
## 0
## Gender
## 0
## Dependent_count
## 0
## Education_Level
## 0
## Marital_Status
## 0
## Income_Category
## 0
## Card_Category
## 0
## Months_on_book
## 0
## Total_Relationship_Count
## 0
## Months_Inactive_12_mon
## 0
## Contacts_Count_12_mon
## 0
## Credit_Limit
## 0
## Total_Revolving_Bal
## 0
## Avg_Open_To_Buy
## 0
## Total_Amt_Chng_Q4_Q1
## 0
## Total_Trans_Amt
## 0
## Total_Trans_Ct
## 0
## Total_Ct_Chng_Q4_Q1
## 0
## Avg_Utilization_Ratio
## 0
## Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_1
## 0
## Naive_Bayes_Classifier_Attrition_Flag_Card_Category_Contacts_Count_12_mon_Dependent_count_Education_Level_Months_Inactive_12_mon_2
## 0
# Drop unnecessary columns
c_data <- c_data[, -c(1, 22, 23)]
# Calculate kurtosis
kurtosis <- kurtosis(c_data$Months_on_book)
# Print kurtosis value
print(paste("Kurtosis of Months on book features is:", kurtosis))
## [1] "Kurtosis of Months on book features is: 0.398638886235621"
Distribution of the Total Transaction Amount (Last 12 months):
# Box plot and histogram
p1 <- ggplot(c_data, aes(x = "", y = Total_Trans_Amt)) +
geom_boxplot() +
labs(x = NULL, y = "Total Transaction Amount") +
theme_minimal()
p2 <- ggplot(c_data, aes(x = Total_Trans_Amt)) +
geom_histogram() +
labs(x = "Total Transaction Amount", y = "Count") +
theme_minimal()
grid.arrange(p1, p2, nrow = 2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
# Identify the column names of categorical variables and factors
categorical_columns <- sapply(c_data, is.character)
categorical_column_names <- names(c_data[categorical_columns])
# Print the column names of categorical variables and factors
print(categorical_column_names)
## [1] "Attrition_Flag" "Gender" "Education_Level" "Marital_Status"
## [5] "Income_Category" "Card_Category"
# Convert values of Attrition_Flag to 0 and 1
c_data$Attrition_Flag <- ifelse(c_data$Attrition_Flag == "Existing Customer", 0, 1)
names(c_data)
## [1] "Attrition_Flag" "Customer_Age"
## [3] "Gender" "Dependent_count"
## [5] "Education_Level" "Marital_Status"
## [7] "Income_Category" "Card_Category"
## [9] "Months_on_book" "Total_Relationship_Count"
## [11] "Months_Inactive_12_mon" "Contacts_Count_12_mon"
## [13] "Credit_Limit" "Total_Revolving_Bal"
## [15] "Avg_Open_To_Buy" "Total_Amt_Chng_Q4_Q1"
## [17] "Total_Trans_Amt" "Total_Trans_Ct"
## [19] "Total_Ct_Chng_Q4_Q1" "Avg_Utilization_Ratio"
str(c_data)
## 'data.frame': 10127 obs. of 20 variables:
## $ Attrition_Flag : num 0 0 0 0 0 0 0 0 0 0 ...
## $ Customer_Age : int 45 49 51 40 40 44 51 32 37 48 ...
## $ Gender : chr "M" "F" "M" "F" ...
## $ Dependent_count : int 3 5 3 4 3 2 4 0 3 2 ...
## $ Education_Level : chr "High School" "Graduate" "Graduate" "High School" ...
## $ Marital_Status : chr "Married" "Single" "Married" "Unknown" ...
## $ Income_Category : chr "$60K - $80K" "Less than $40K" "$80K - $120K" "Less than $40K" ...
## $ Card_Category : chr "Blue" "Blue" "Blue" "Blue" ...
## $ Months_on_book : int 39 44 36 34 21 36 46 27 36 36 ...
## $ Total_Relationship_Count: int 5 6 4 3 5 3 6 2 5 6 ...
## $ Months_Inactive_12_mon : int 1 1 1 4 1 1 1 2 2 3 ...
## $ Contacts_Count_12_mon : int 3 2 0 1 0 2 3 2 0 3 ...
## $ Credit_Limit : num 12691 8256 3418 3313 4716 ...
## $ Total_Revolving_Bal : int 777 864 0 2517 0 1247 2264 1396 2517 1677 ...
## $ Avg_Open_To_Buy : num 11914 7392 3418 796 4716 ...
## $ Total_Amt_Chng_Q4_Q1 : num 1.33 1.54 2.59 1.4 2.17 ...
## $ Total_Trans_Amt : int 1144 1291 1887 1171 816 1088 1330 1538 1350 1441 ...
## $ Total_Trans_Ct : int 42 33 20 20 28 24 31 36 24 32 ...
## $ Total_Ct_Chng_Q4_Q1 : num 1.62 3.71 2.33 2.33 2.5 ...
## $ Avg_Utilization_Ratio : num 0.061 0.105 0 0.76 0 0.311 0.066 0.048 0.113 0.144 ...
categorical_cols <- c("Gender", "Education_Level", "Marital_Status", "Income_Category", "Card_Category")
c_data[categorical_cols] <- lapply(c_data[categorical_cols], as.factor)
# Create a formula for one-hot encoding
formula <- as.formula(paste("factor(Attrition_Flag) ~", paste(categorical_cols, collapse = "+")))
# Create dummy variables using dummyVars
dummy_data <- predict(dummyVars(formula, data = c_data), newdata = c_data)
# Combine numerical and one-hot encoded data
combined_data <- cbind(c_data[, !(names(c_data) %in% categorical_cols)], dummy_data)
# Split the data into training and testing sets
set.seed(42)
train_indices <- createDataPartition(combined_data$Attrition_Flag, p = 0.7, list = FALSE)
train_data <- combined_data[train_indices, ]
test_data <- combined_data[-train_indices, ]
# Separate predictors (x) and target variable (y) in the training and testing sets
X_train <- train_data[, !(names(train_data) %in% "Attrition_Flag")]
y_train <- train_data$Attrition_Flag
X_test <- test_data[, !(names(test_data) %in% "Attrition_Flag")]
y_test <- test_data$Attrition_Flag
names(X_train)
## [1] "Customer_Age" "Dependent_count"
## [3] "Months_on_book" "Total_Relationship_Count"
## [5] "Months_Inactive_12_mon" "Contacts_Count_12_mon"
## [7] "Credit_Limit" "Total_Revolving_Bal"
## [9] "Avg_Open_To_Buy" "Total_Amt_Chng_Q4_Q1"
## [11] "Total_Trans_Amt" "Total_Trans_Ct"
## [13] "Total_Ct_Chng_Q4_Q1" "Avg_Utilization_Ratio"
## [15] "Gender.F" "Gender.M"
## [17] "Education_Level.College" "Education_Level.Doctorate"
## [19] "Education_Level.Graduate" "Education_Level.High School"
## [21] "Education_Level.Post-Graduate" "Education_Level.Uneducated"
## [23] "Education_Level.Unknown" "Marital_Status.Divorced"
## [25] "Marital_Status.Married" "Marital_Status.Single"
## [27] "Marital_Status.Unknown" "Income_Category.$120K +"
## [29] "Income_Category.$40K - $60K" "Income_Category.$60K - $80K"
## [31] "Income_Category.$80K - $120K" "Income_Category.Less than $40K"
## [33] "Income_Category.Unknown" "Card_Category.Blue"
## [35] "Card_Category.Gold" "Card_Category.Platinum"
## [37] "Card_Category.Silver"
# Random Forest Classifier
rf_model <- randomForest(x = X_train, y = as.factor(y_train), class.factors = levels(as.factor(y_train)))
# Train the SVM model
svm_model <- svm(x = X_train, y = as.factor(y_train))
# Train the Decision Tree model
dt_model <- rpart(y_train ~ ., data = X_train, method = "class")
# Make predictions on the test set
rf_predictions <- predict(rf_model, X_test)
# Convert y_test to have the same levels as rf_predictions
y_test <- factor(y_test, levels = levels(rf_predictions))
# Calculate accuracy and confusion matrix
rf_accuracy <- sum(rf_predictions == y_test) / length(y_test)
rf_confusion <- confusionMatrix(rf_predictions, y_test)
# Print accuracy and confusion matrix
print(paste("Random Forest Accuracy:", rf_accuracy))
## [1] "Random Forest Accuracy: 0.953258722843976"
print("Random Forest Confusion Matrix:")
## [1] "Random Forest Confusion Matrix:"
print(rf_confusion$table)
## Reference
## Prediction 0 1
## 0 2509 119
## 1 23 387
# SVM
# Make predictions on the test set
svm_predictions <- predict(svm_model, X_test)
# Evaluate the model performance
svm_accuracy <- sum(svm_predictions == y_test) / length(y_test)
# Create the confusion matrix
svm_confusion <- confusionMatrix(svm_predictions, y_test)
print(paste("SVM Accuracy:", svm_accuracy))
## [1] "SVM Accuracy: 0.910138248847926"
print("SVM Confusion Matrix:")
## [1] "SVM Confusion Matrix:"
print(svm_confusion$table)
## Reference
## Prediction 0 1
## 0 2490 231
## 1 42 275
# Decision Tree
# Predict class labels on the test set
dt_predictions <- predict(dt_model, newdata = X_test, type = "class")
# Evaluate the model performance
dt_accuracy <- sum(dt_predictions == y_test) / length(y_test)
dt_confusion <- confusionMatrix(dt_predictions, y_test)
print(paste("Decision Tree Accuracy:", dt_accuracy))
## [1] "Decision Tree Accuracy: 0.929229756418697"
print("Decision Tree Confusion Matrix:")
## [1] "Decision Tree Confusion Matrix:"
print(dt_confusion$table)
## Reference
## Prediction 0 1
## 0 2449 132
## 1 83 374
# Create a data frame to store the performance metrics
performance <- data.frame(Model = c("Random Forest", "SVM", "Decision Tree"),
Accuracy = numeric(3),
Precision = numeric(3),
Recall = numeric(3),
F1_Score = numeric(3))
# Random Forest
rf_accuracy <- sum(rf_predictions == y_test) / length(y_test)
rf_confusion <- confusionMatrix(rf_predictions, y_test)
rf_precision <- rf_confusion$byClass["Pos Pred Value"]
rf_recall <- rf_confusion$byClass["Sensitivity"]
rf_f1_score <- 2 * (rf_precision * rf_recall) / (rf_precision + rf_recall)
performance[1, c("Accuracy", "Precision", "Recall", "F1_Score")] <- c(rf_accuracy, rf_precision, rf_recall, rf_f1_score)
# SVM
svm_accuracy <- sum(svm_predictions == y_test) / length(y_test)
svm_confusion <- confusionMatrix(svm_predictions, y_test)
svm_precision <- svm_confusion$byClass["Pos Pred Value"]
svm_recall <- svm_confusion$byClass["Sensitivity"]
svm_f1_score <- 2 * (svm_precision * svm_recall) / (svm_precision + svm_recall)
performance[2, c("Accuracy", "Precision", "Recall", "F1_Score")] <- c(svm_accuracy, svm_precision, svm_recall, svm_f1_score)
# Decision Tree
dt_accuracy <- sum(dt_predictions == y_test) / length(y_test)
dt_confusion <- confusionMatrix(dt_predictions, y_test)
dt_precision <- dt_confusion$byClass["Pos Pred Value"]
dt_recall <- dt_confusion$byClass["Sensitivity"]
dt_f1_score <- 2 * (dt_precision * dt_recall) / (dt_precision + dt_recall)
performance[3, c("Accuracy", "Precision", "Recall", "F1_Score")] <- c(dt_accuracy, dt_precision, dt_recall, dt_f1_score)
# Print the performance metrics
print(performance)
## Model Accuracy Precision Recall F1_Score
## 1 Random Forest 0.9532587 0.9547184 0.9909163 0.9724806
## 2 SVM 0.9101382 0.9151047 0.9834123 0.9480297
## 3 Decision Tree 0.9292298 0.9488570 0.9672196 0.9579503
◯ There are 16.07% of customers who have churned.
◯ The
proportion of gender count is almost equally distributed (52.9% male and
47.1%) compare to proportion of existing and attributed customer count
(83.9% and 16.1%) which is highly imbalanced
◯ The proportion of
attrited customers by gender there are 14.4% more male than female who
have churned
◯ Customers who have churned are highly educated - A
high proportion of education level of attrited customer is Graduate
level (29.9%), followed by Post-Graduate level (18.8%)
◯ A high
proportion of marital status of customers who have churned is Married
(43.6%), followed by Single (41.1%) compared to Divorced (7.4%) and
Unknown (7.9%) status - Marital stuats of the attributed customers are
highly clustered in Married status and Single
◯ As you can see from
the proportion of income category of attrited customer, it is highly
concentrated around $60K - $80K income (37.6%), followed by Less than
$40K income (16.7%) compare to attrited customers with higher annual
income of 80K-120K(14.9%) and over $120K + (11.5%). I assume that
customers with higher income doesn’t likely to leave their credit card
services than meddle-income customer