In [68]:
Logistic Regression Model of next payment default based on credit bureau information.

#Justin Gabb
In [1]:
from sklearn.ensemble import RandomForestClassifier as rfc
from sklearn.neighbors import KNeighborsClassifier as knc
from scipy import stats

from sklearn.model_selection import cross_val_score
from sklearn.model_selection import train_test_split 

import pandas as pd
import numpy as np
import seaborn as sns
sns.set_style("whitegrid")

import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)

random_seed = 123

%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 10
plt.rcParams['xtick.labelsize'] = 10
plt.rcParams['ytick.labelsize'] = 10

def seaborn_dist_plot(data, column_num1, column_num2, filter_value1, filter_value2):
    plt.figure(figsize=(15,5))
    filter1 = data.iloc[:,column_num2] == filter_value1
    result1 = data.iloc[:,column_num1][filter1]
    filter2 = data.iloc[:,column_num2] == filter_value2
    result2 = data.iloc[:,column_num1][filter2]
    sns.histplot(result1, color='coral', kde=False, label='0 Class', alpha=0.5)
    sns.histplot(result2, color='turquoise', kde=False, label='1 Class', alpha=0.5)
    plt.legend()
    plt.title('count # of {0} with {1} values'.format(data.iloc[:,column_num1].name, data.iloc[:,column_num2].name))
    return plt.show()

#Column filter function, pass dataset and integer value for column number as iloc index value
#Returns filter results, shape of results, and index values. 
def column_filter(data, column_num, filter_value):
    index_list = []
    filter1 = data.iloc[:,column_num] == filter_value      #configure filter parameter using: ==, >, <
    filter_result = data.iloc[:,column_num][filter1]
    shape1 = filter_result.shape
    index_list = filter_result.index.tolist()
    return filter_result, shape1, index_list

def box_plot(data, column_num1, column_num2):
    plt.figure(figsize=(8, 10))
    x = data.iloc[:,column_num2]
    y = data.iloc[:,column_num1]
    sns.boxplot(x, y)
    plt.title('Boxplot of {0} by {1}'.format(x.name, y.name))
    return plt.show()
In [3]:
path = ("C:/Users/Z33/Desktop/Data Science/U of T - Data Science/Data Science 4 - Machine Learning/Assignment 1 - Classification/default of credit card clients.xls")
credit_data = pd.read_excel(path, header=1, index_col=(0))

Data Exploration & Analysis

In [4]:
col_names = credit_data.columns
col_names
Out[4]:
Index(['LIMIT_BAL', 'SEX', 'EDUCATION', 'MARRIAGE', 'AGE', 'PAY_0', 'PAY_2',
       'PAY_3', 'PAY_4', 'PAY_5', 'PAY_6', 'BILL_AMT1', 'BILL_AMT2',
       'BILL_AMT3', 'BILL_AMT4', 'BILL_AMT5', 'BILL_AMT6', 'PAY_AMT1',
       'PAY_AMT2', 'PAY_AMT3', 'PAY_AMT4', 'PAY_AMT5', 'PAY_AMT6',
       'default payment next month'],
      dtype='object')
In [5]:
credit_data.rename(columns={'default payment next month':'next_pmt_default'}, inplace=True)
In [6]:
credit_data.head()
Out[6]:
LIMIT_BAL SEX EDUCATION MARRIAGE AGE PAY_0 PAY_2 PAY_3 PAY_4 PAY_5 ... BILL_AMT4 BILL_AMT5 BILL_AMT6 PAY_AMT1 PAY_AMT2 PAY_AMT3 PAY_AMT4 PAY_AMT5 PAY_AMT6 next_pmt_default
ID
1 20000 2 2 1 24 2 2 -1 -1 -2 ... 0 0 0 0 689 0 0 0 0 1
2 120000 2 2 2 26 -1 2 0 0 0 ... 3272 3455 3261 0 1000 1000 1000 0 2000 1
3 90000 2 2 2 34 0 0 0 0 0 ... 14331 14948 15549 1518 1500 1000 1000 1000 5000 0
4 50000 2 2 1 37 0 0 0 0 0 ... 28314 28959 29547 2000 2019 1200 1100 1069 1000 0
5 50000 1 2 1 57 -1 0 -1 0 0 ... 20940 19146 19131 2000 36681 10000 9000 689 679 0

5 rows × 24 columns

In [83]:
credit_data.describe()
Out[83]:
LIMIT_BAL SEX EDUCATION MARRIAGE AGE PAY_0 PAY_2 PAY_3 PAY_4 PAY_5 ... BILL_AMT4 BILL_AMT5 BILL_AMT6 PAY_AMT1 PAY_AMT2 PAY_AMT3 PAY_AMT4 PAY_AMT5 PAY_AMT6 next_pmt_default
count 30000.000000 30000.000000 30000.000000 30000.000000 30000.000000 30000.000000 30000.000000 30000.000000 30000.000000 30000.000000 ... 30000.000000 30000.000000 30000.000000 30000.000000 3.000000e+04 30000.00000 30000.000000 30000.000000 30000.000000 30000.000000
mean 167484.322667 1.603733 1.853133 1.551867 35.485500 -0.016700 -0.133767 -0.166200 -0.220667 -0.266200 ... 43262.948967 40311.400967 38871.760400 5663.580500 5.921163e+03 5225.68150 4826.076867 4799.387633 5215.502567 0.221200
std 129747.661567 0.489129 0.790349 0.521970 9.217904 1.123802 1.197186 1.196868 1.169139 1.133187 ... 64332.856134 60797.155770 59554.107537 16563.280354 2.304087e+04 17606.96147 15666.159744 15278.305679 17777.465775 0.415062
min 10000.000000 1.000000 0.000000 0.000000 21.000000 -2.000000 -2.000000 -2.000000 -2.000000 -2.000000 ... -170000.000000 -81334.000000 -339603.000000 0.000000 0.000000e+00 0.00000 0.000000 0.000000 0.000000 0.000000
25% 50000.000000 1.000000 1.000000 1.000000 28.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 ... 2326.750000 1763.000000 1256.000000 1000.000000 8.330000e+02 390.00000 296.000000 252.500000 117.750000 0.000000
50% 140000.000000 2.000000 2.000000 2.000000 34.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 19052.000000 18104.500000 17071.000000 2100.000000 2.009000e+03 1800.00000 1500.000000 1500.000000 1500.000000 0.000000
75% 240000.000000 2.000000 2.000000 2.000000 41.000000 0.000000 0.000000 0.000000 0.000000 0.000000 ... 54506.000000 50190.500000 49198.250000 5006.000000 5.000000e+03 4505.00000 4013.250000 4031.500000 4000.000000 0.000000
max 1000000.000000 2.000000 6.000000 3.000000 79.000000 8.000000 8.000000 8.000000 8.000000 8.000000 ... 891586.000000 927171.000000 961664.000000 873552.000000 1.684259e+06 896040.00000 621000.000000 426529.000000 528666.000000 1.000000

8 rows × 24 columns

In [57]:
sns.set(style='ticks')
plt.figure(figsize=(20,20))
sns.pairplot(credit_data, hue='next_pmt_default')
Out[57]:
<seaborn.axisgrid.PairGrid at 0x1b4355d2f40>
<Figure size 1440x1440 with 0 Axes>