<jupyter_start><jupyter_text>Predicting Credit Card Default with Neural Networks<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Back with the credit card default 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['high_school'] = (ccd['education'] == 3).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>Split and standarize 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=101)

## Standarize
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
scaler.fit(X_train[numerical_features])
X_train.loc[:, numerical_features] = scaler.transform(X_train[numerical_features])
# Standarize also the testing set
X_test.loc[:, numerical_features] = scaler.transform(X_test[numerical_features])<jupyter_output>C:\Users\direc\Anaconda3\envs\mybook\lib\site-packages\pandas\core\indexing.py:543: SettingWithCopyWarning: 
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s<jupyter_text>Building the neural network for classification<jupyter_code>from keras.models import Sequential
nn_classifier = Sequential()
from keras.layers import Dense
n_input = X_train.shape[1]
n_units_hidden = 64
nn_classifier.add(Dense(units=n_units_hidden, activation='relu', input_shape=(n_input,)))
# add 2nd hidden layer
nn_classifier.add(Dense(units=n_units_hidden, activation='relu'))
# add 3th hidden layer
nn_classifier.add(Dense(units=n_units_hidden, activation='relu'))
# add 4th hidden layer
nn_classifier.add(Dense(units=n_units_hidden, activation='relu'))
# add 5th hidden layer
nn_classifier.add(Dense(units=n_units_hidden, activation='relu'))
# output layer
nn_classifier.add(Dense(1, activation='sigmoid'))<jupyter_output><empty_output><jupyter_text>Training the network<jupyter_code>## compiling step
nn_classifier.compile(loss='binary_crossentropy', optimizer='adam')
nn_classifier.summary()
nn_classifier.save_weights('class_initial_w.h5')
batch_size = 64
n_epochs = 150
nn_classifier.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size)<jupyter_output>Epoch 1/150
25000/25000 [==============================] - 1s 30us/step - loss: 0.4635
Epoch 2/150
25000/25000 [==============================] - 0s 17us/step - loss: 0.4467
Epoch 3/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4432
Epoch 4/150
25000/25000 [==============================] - 0s 16us/step - loss: 0.4414
Epoch 5/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4399
Epoch 6/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4389
Epoch 7/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4373
Epoch 8/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4370
Epoch 9/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4344
Epoch 10/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4332
Epoch 11/150
25000/25000 [==============================] - 0s 15us/step - loss: 0.4330
Epoch 12/150
25000/25000 [===============[...]<jupyter_text>Evaluating predictions<jupyter_code>## Getting the probabilities
y_pred_train_prob = nn_classifier.predict(X_train)
y_pred_test_prob = nn_classifier.predict(X_test)

## Classifications from predictions
y_pred_train = (y_pred_train_prob > 0.5).astype(int)
y_pred_test = (y_pred_test_prob > 0.5).astype(int)
from sklearn.metrics import accuracy_score
train_acc = accuracy_score(y_true=y_train, y_pred=y_pred_train)
test_acc = accuracy_score(y_true=y_test, y_pred=y_pred_test)
print("Train Accuracy: {:0.3f} \nTest Accuracy: {:0.3f}".format(train_acc, test_acc))<jupyter_output>Train Accuracy: 0.923 
Test Accuracy: 0.740<jupyter_text>Re-training the network with less epochs<jupyter_code>## load the initial weights
nn_classifier.load_weights('class_initial_w.h5')
batch_size = 64
n_epochs = 50
nn_classifier.compile(loss='binary_crossentropy', optimizer='adam')
nn_classifier.fit(X_train, y_train, epochs=n_epochs, batch_size=batch_size)
## Getting the probabilities
y_pred_train_prob = nn_classifier.predict(X_train)
y_pred_test_prob = nn_classifier.predict(X_test)

## Classifications from predictions
y_pred_train = (y_pred_train_prob > 0.5).astype(int)
y_pred_test = (y_pred_test_prob > 0.5).astype(int)

## Calculating accuracy
train_acc = accuracy_score(y_true=y_train, y_pred=y_pred_train)
test_acc = accuracy_score(y_true=y_test, y_pred=y_pred_test)
print("Train Accuracy: {:0.3f} \nTest Accuracy: {:0.3f}".format(train_acc, test_acc))<jupyter_output>Train Accuracy: 0.852 
Test Accuracy: 0.786
{'fraction_non_alphanumeric': 0.17742188726644686, 'fraction_numerical': 0.05291254514052442, 'mean_word_length': 7.294270833333333, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 30, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '3665', 'license': 'MIT', 'n_tokens_mistral': 2556, 'n_tokens_neox': 2280, 'n_words': 457, 'path': 'Chapter6/ch6-2-classification-with-neural-networks.ipynb', 'repo_name': 'PacktPublishing/-Hands-On-Predictive-Analytics-with-Python', 'size': '6369'}