<jupyter_start><jupyter_text>Credit Card Default: Model Tuning and Improving Performance  Importing libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os

pd.options.mode.chained_assignment = None
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Loading and preparing the dataset<jupyter_code># Loading the dataset
DATA_DIR = '../data'
FILE_NAME = 'credit_card_default.csv'
data_path = os.path.join(DATA_DIR, FILE_NAME)
ccd = pd.read_csv(data_path, index_col="ID")
ccd.rename(columns=lambda x: x.lower(), inplace=True)
ccd.rename(columns={'default payment next month':'default'}, inplace=True)

# getting the groups of features
bill_amt_features = ['bill_amt'+ str(i) for i in range(1,7)]
pay_amt_features = ['pay_amt'+ str(i) for i in range(1,7)]
numerical_features = ['limit_bal','age'] + bill_amt_features + pay_amt_features

# Creating creating binary features
ccd['male'] = (ccd['sex'] == 1).astype('int')
ccd['grad_school'] = (ccd['education'] == 1).astype('int')
ccd['university'] = (ccd['education'] == 2).astype('int')
ccd['married'] = (ccd['marriage'] == 1).astype('int')

# simplifying pay features 
pay_features= ['pay_' + str(i) for i in range(1,7)]
for x in pay_features:
    ccd.loc[ccd[x] <= 0, x] = 0

# simplifying delayed features
delayed_features = ['delayed_' + str(i) for i in range(1,7)]
for pay, delayed in zip(pay_features, delayed_features):
    ccd[delayed] = (ccd[pay] > 0).astype(int)
    
# creating a new feature: months delayed
ccd['months_delayed'] = ccd[delayed_features].sum(axis=1)<jupyter_output><empty_output><jupyter_text>Splitting  and standarizing the dataset<jupyter_code>numerical_features = numerical_features + ['months_delayed']
binary_features = ['male','married','grad_school','university']
X = ccd[numerical_features + binary_features]
y = ccd['default'].astype(int)

## Split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=5/30, random_state=25)

## Standarize
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X[numerical_features])
X_train.loc[:, numerical_features] = scaler.transform(X_train[numerical_features])
X_test.loc[:, numerical_features] = scaler.transform(X_test[numerical_features])<jupyter_output><empty_output><jupyter_text>Optimizing more than one parameter  Reference model<jupyter_code>from sklearn.model_selection import cross_val_score
from sklearn.ensemble import RandomForestClassifier
ref_rf = RandomForestClassifier(n_estimators=25,
                                max_features=4,
                                max_depth=4,
                                random_state=61)

ref_rf_scores = cross_val_score(ref_rf, X_train, y_train, scoring='roc_auc', cv=10)
print("Mean AUC for reference model: {:0.4f}".format(ref_rf_scores.mean()))<jupyter_output>Mean AUC for reference model: 0.7589<jupyter_text>Grid Search CV<jupyter_code>from sklearn.model_selection import GridSearchCV
param_grid = {"n_estimators":[25,100,200,400],
              "max_features":[4,10,19],
              "max_depth":[4,8,16,20]}
rf = RandomForestClassifier(random_state=17)
grid_search = GridSearchCV(estimator=rf,
                           param_grid=param_grid,
                           scoring='roc_auc',
                           cv=5,
                           verbose=1,
                           n_jobs=4)
grid_search.fit(X_train, y_train)
gs_results = pd.Series(grid_search.cv_results_['mean_test_score'], index=grid_search.cv_results_['params'])
gs_results.sort_values(ascending=False)
from sklearn.metrics import precision_recall_curve
## Fitting the initial (not tuned) model:
ref_rf.fit(X_train, y_train)

## Getting the probabilites
y_prob_tunned = grid_search.predict_proba(X_test)[:,1]
y_prob_not_tunned = ref_rf.predict_proba(X_test)[:,1]

## Values for plotting the curves
prec_tuned, recall_tuned, _ = precision_recall_curve(y_test, y_prob_tunned)
prec_not_tuned, recall_not_tuned, _ = precision_recall_curve(y_test, y_prob_not_tunned)
fig, ax = plt.subplots(figsize=(8,5))
ax.plot(prec_tuned, recall_tuned, label='Tuned Model')
ax.plot(prec_not_tuned, recall_not_tuned, label='Not Tuned Model')
ax.set_title('Precision-recall curves', fontsize=16)
ax.set_xlabel('Precision', fontsize=14)
ax.set_ylabel('Recall', fontsize=14)
ax.set_xlim(0.3,0.7); ax.set_ylim(0.1,0.9)
ax.legend(); ax.grid();<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12622439893143367, 'fraction_numerical': 0.018032056990204808, 'mean_word_length': 5.40941512125535, 'pattern_counts': {'":': 3, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '547988', 'license': 'MIT', 'n_tokens_mistral': 1644, 'n_tokens_neox': 1574, 'n_words': 295, 'path': 'Chapter08/ch8-credit-card-def-model-tuning.ipynb', 'repo_name': 'arifmudi/Hands-On-Predictive-Analytics-with-Python', 'size': '4492'}