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.metrics import confusion_matrix
# Load the dataset
cc_apps_1 = pd.read_csv("cc_approvals.data", header=None)
cc_apps_1.head()
# Check out the dataset
cc_apps_1.info()
cc_apps_1.isna().sum().sum()# Replace the '?'s with NaN in dataset-> in column 1
cc_apps_nans_replaced = cc_apps_1.replace("?", np.NaN)
cc_apps = cc_apps_nans_replaced#change to categorical data
cc_apps[0] = cc_apps[0].astype('category').cat.codes
cc_apps[3] = cc_apps[3].astype('category').cat.codes
cc_apps[4] = cc_apps[4].astype('category').cat.codes
cc_apps[5] = cc_apps[5].astype('category').cat.codes
cc_apps[6] = cc_apps[6].astype('category').cat.codes
cc_apps[8] = cc_apps[8].astype('category').cat.codes
cc_apps[9] = cc_apps[9].astype('category').cat.codes
cc_apps[11] = cc_apps[11].astype('category').cat.codes
cc_apps[13] = cc_apps[13].astype('category').cat.codes
cc_apps#fill all nans w Mode
from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy='most_frequent')
imputer.fit(cc_apps)
df_imputed = imputer.transform(cc_apps)
df_imputed = pd.DataFrame(df_imputed, columns=cc_apps.columns)
df_imputed.info()#change all dtypes to numeric
df_num = df_imputed.apply(pd.to_numeric, errors='coerce')
df_num.info()# split the data
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
X = df_num.iloc[:, :-1].values
y = df_num.iloc[:, -1].values # Changed this line to get a 1D array
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=23)
# scale the data
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(x_train)
# build model
model_scaled = LogisticRegression()
model_scaled.fit(x_train_scaled, y_train)#scale our test data
X_test_scaled = scaler.transform(x_test)#Model Accuracy
train_score = model_scaled.score(x_train_scaled, y_train)
print('Training accuracy is ', train_score)
test_score=model_scaled.score(X_test_scaled, y_test)
print('Test accuracy is ', test_score)#confusion matrix
y_pred = model_scaled.predict(X_test_scaled)
cm=confusion_matrix(y_test,y_pred)
cmbest_score = model_scaled.score(X_test_scaled, y_test)
print("Accuracy of logistic regression: ", best_score)