Skip to content

Commercial banks receive a lot of applications for credit cards. Many of them get rejected for many reasons, like high loan balances, low income levels, or too many inquiries on an individual's credit report, for example. Manually analyzing these applications is mundane, error-prone, and time-consuming (and time is money!). Luckily, this task can be automated with the power of machine learning and pretty much every commercial bank does so nowadays. In this workbook, you will build an automatic credit card approval predictor using machine learning techniques, just like real banks do.

The Data

The data is a small subset of the Credit Card Approval dataset from the UCI Machine Learning Repository showing the credit card applications a bank receives. This dataset has been loaded as a pandas DataFrame called cc_apps. The last column in the dataset is the target value.

# Import necessary libraries
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import GridSearchCV

# Load the dataset
cc_apps = pd.read_csv("cc_approvals.data", header=None) 
cc_apps.head()
cc_apps.info()
cc_apps.isnull().sum().sum()
#object_columns = cc_apps.select_dtypes(include='object').columns.tolist()#print object dtypes to convert it to one_hot encoding
#print(object_columns) code didn't work
cc_apps.columns
cc_apps = pd.get_dummies(cc_apps,columns=[0,1,3,4,5,6,7,8,9,11],drop_first=True)
cc_apps.head()
#preparing data for modeling
X=cc_apps.drop(columns=13).values
y=cc_apps[13].values
scaler=StandardScaler()
features=scaler.fit_transform(X)
X_train,X_test,y_train,y_test=train_test_split(features,y,test_size=0.25,random_state=42)
#LogisiticRegression
lg = LogisticRegression(random_state=42)
lg.fit(X_train,y_train)
lg_pred = lg.predict(X_test)
cf = confusion_matrix(y_test,lg_pred)
print(cf)
TP=cf[0,0]
FP = cf[0,1]
FN=cf[1,0]
TN=cf[1,1]
accuracy=(TP+TN)/(TP+FP+FN+TN)
print(f"The accuracy using logistic regression is:{accuracy}")
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import confusion_matrix

#knn classifier
knn = KNeighborsClassifier(n_neighbors=20)
knn.fit(X_train, y_train)
knn_pred = knn.predict(X_test)
cf2 = confusion_matrix(y_test, knn_pred)
print(cf2)
TP = cf2[0, 0]
FP = cf2[0, 1]
FN = cf2[1, 0]
TN = cf2[1, 1]
accuracy1 = (TP + TN) / (TP + FP + FN + TN)
print(f"The accuracy using knn is, {accuracy1}")
#RandomForest
rf = RandomForestClassifier(random_state=42)
rf.fit(X_train,y_train)
rf_pred = rf.predict(X_test)
cf3 = confusion_matrix(y_test,rf_pred)
print(cf3)
TP = cf3[0,0]
FP=cf3[0,1]
FN = cf3[1,0]
TN = cf3[1,1]
accuracy2 = (TP+TN)/(TN+FP+FN+TN)
print(f"The accuracy using Random_Forest is,{accuracy2}")
#hypertuning Logistic Regression but I am lazy to do it
param_grid = {
    'C': [0.001, 0.01, 0.1, 1, 10, 100],
    'penalty': ['l1', 'l2']  # Note: 'l1' penalty requires solver='liblinear'
}
#instantiate CV search
grid_search = GridSearchCV(estimator=lg, param_grid=param_grid, scoring='accuracy', cv=9)
grid_search.fit(X_train, y_train)
#get best_score and parameters
print("Best Hyperparameters for Logistic Regression:", grid_search.best_params_)
print(f"Best Score for Logistic Regression: {grid_search.best_score_:.4f}")
#hypertuning Random_Forest
param_grid1= {
    'n_estimators': [50, 100, 150],
    'max_depth': [None, 5, 10],
    'min_samples_split': [2, 5]
}
grid_search1=GridSearchCV(estimator=rf,param_grid=param_grid1,scoring="accuracy",cv=9)
grid_search1.fit(X_train,y_train)#fit the model
print("Best Hyperparamters for Random_Forest",grid_search1.best_params_)
print(f"Best Score for RandomForest is:{grid_search1.best_score_:.4f}")
best_score = 0.8781