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
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, classification_report
# Load the dataset
cc_apps = pd.read_csv("cc_approvals.data", header=None)
cc_apps.head()# Identify missing values
print(cc_apps.isnull().sum())# Encode categorical variables
encoder = LabelEncoder()
for column in cc_apps.select_dtypes(include=['object']).columns:
cc_apps[column] = encoder.fit_transform(cc_apps[column])
cc_apps.head()# Separate features and target
X = cc_apps.iloc[:, :-1] # All columns except the last
y = cc_apps.iloc[:, -1] # The last column (target)
# Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize the numerical features
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Initialize and train Logistic Regression model
lr_model = LogisticRegression(random_state=42, max_iter=1000)
lr_model.fit(X_train, y_train)
# Evaluate Logistic Regression
lr_predictions = lr_model.predict(X_test)
lr_accuracy = accuracy_score(y_test, lr_predictions)
print(f"Logistic Regression Accuracy: {lr_accuracy}")
print("\nLogistic Regression Classification Report:")
print(classification_report(y_test, lr_predictions))
# Initialize and train Random Forest model
rf_model = RandomForestClassifier(random_state=42, n_estimators=100)
rf_model.fit(X_train, y_train)
# Evaluate Random Forest
rf_predictions = rf_model.predict(X_test)
rf_accuracy = accuracy_score(y_test, rf_predictions)
print(f"Random Forest Accuracy: {rf_accuracy}")
print("\nRandom Forest Classification Report:")
print(classification_report(y_test, rf_predictions))
print("Model Comparison:")
print(f"Logistic Regression Accuracy: {lr_accuracy}")
print(f"Random Forest Accuracy: {rf_accuracy}")
if lr_accuracy > rf_accuracy:
print("Logistic Regression performed better.")
else:
print("Random Forest performed better.")
# Grid search for Logistic Regression
lr_param_grid = {'C': [0.01, 0.1, 1, 10, 100]}
lr_grid_search = GridSearchCV(LogisticRegression(random_state=42, max_iter=1000), lr_param_grid, cv=5, scoring='accuracy')
lr_grid_search.fit(X_train, y_train)
# Best Logistic Regression
best_lr_model = lr_grid_search.best_estimator_
best_lr_accuracy = lr_grid_search.best_score_
print(f"Best Logistic Regression Accuracy: {best_lr_accuracy}")
# Grid search for Random Forest
rf_param_grid = {
'n_estimators': [50, 100, 150],
'max_depth': [None, 10, 20],
'min_samples_split': [2, 5, 10]
}
rf_grid_search = GridSearchCV(RandomForestClassifier(random_state=42), rf_param_grid, cv=5, scoring='accuracy')
rf_grid_search.fit(X_train, y_train)
# Best Random Forest
best_rf_model = rf_grid_search.best_estimator_
best_rf_accuracy = rf_grid_search.best_score_
print(f"Best Random Forest Accuracy: {best_rf_accuracy}")
# Save the best model's accuracy
best_score = max(lr_accuracy, rf_accuracy, best_lr_accuracy, best_rf_accuracy)
print(f"Best Model's Accuracy: {best_score}")