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
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()# Import necessary library for visualization
import matplotlib.pyplot as plt
import seaborn as sns
# Select numeric columns
numeric_columns = cc_apps.select_dtypes(include=[np.number]).columns
# Generate boxplots for numeric variables
plt.figure(figsize=(12, 8))
cc_apps[numeric_columns].boxplot()
plt.title('Boxplot of Numeric Variables')
plt.xticks(rotation=45)
plt.show()# Generate histograms for numeric variables
plt.figure(figsize=(12, 8))
for i, column in enumerate(numeric_columns, 1):
plt.subplot(len(numeric_columns), 1, i)
sns.histplot(cc_apps[column], kde=True)
plt.title(f'Histogram of {column}')
plt.xlabel(column)
plt.ylabel('Frequency')
plt.tight_layout()
plt.show()cc_apps.describe()cc_apps.columns = cc_apps.columns.astype(str)
cc_apps['13'] = cc_apps['13'].replace({'+': 1, '-': 0})
cc_apps.head()from sklearn.model_selection import train_test_split, cross_val_score
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import RobustScaler, OneHotEncoder
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
import numpy as np
# 1) Carga tus datos en X, y
X = cc_apps.drop("13", axis=1) # target debe ser binaria: 0/1
y = cc_apps["13"]
# 2) Divide en train y test (20% test)
X_train, X_test, y_train, y_test = train_test_split(
X, y,
test_size=0.2,
random_state=42,
stratify=y
)
# 3) Identifica columnas numéricas y categóricas
numeric_cols = X.select_dtypes(include=['int64','float64']).columns
categorical_cols = X.select_dtypes(include=['object','category']).columns
# 4) Define el preprocesador
preprocessor = ColumnTransformer([
('num', RobustScaler(), numeric_cols),
('cat', OneHotEncoder(drop='first', sparse=False, handle_unknown='ignore'), categorical_cols)
])
# 5) Crea el pipeline con Regresión Logística
pipeline = Pipeline([
('pre', preprocessor),
('clf', LogisticRegression(max_iter=1000))
])
# 6) Validación cruzada en train (6 folds) con accuracy
cv_scores = cross_val_score(
pipeline,
X_train,
y_train,
cv=6,
scoring='accuracy'
)
print("Accuracy CV por fold:", np.round(cv_scores, 4))
print("Accuracy CV promedio:", np.round(cv_scores.mean(), 4))
# 7) Entrena sobre todo el entrenamiento y evalúa en el test set
pipeline.fit(X_train, y_train)
y_pred = pipeline.predict(X_test)
acc_test = accuracy_score(y_test, y_pred)
print(f"Accuracy en test set: {acc_test:.4f}")
best_score = acc_test