-
Notifications
You must be signed in to change notification settings - Fork 1
/
LoanPrediction.py
207 lines (156 loc) · 7.02 KB
/
LoanPrediction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import pandas as pd
import numpy as np
from numpy import mean
import csv
import random
import matplotlib.pyplot as plt
import scipy
from random import seed
from sklearn import svm
from sklearn.linear_model import Perceptron
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier
# Imputing missing values and scaling values
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import accuracy_score
# Machine Learning Models
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor, GradientBoostingClassifier, RandomForestClassifier
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsRegressor
import xgboost as xgb
from xgboost import XGBClassifier
from xgboost import cv
# Hyperparameter tuning
from sklearn.model_selection import RandomizedSearchCV, GridSearchCV
from sklearn.model_selection import RepeatedStratifiedKFold
from sklearn.model_selection import KFold
from sklearn.model_selection import RepeatedKFold
from sklearn.model_selection import cross_val_score
# Seaborn for visualization
import seaborn as sns
sns.set(font_scale=2)
data = pd.read_csv('SBAsubset.csv')
data = data.drop(['LoanNr_ChkDgt', 'Name', 'City', 'Zip', 'BankState', 'ApprovalDate', 'DisbursementDate',
'DisbursementGross'], axis=1)
# Initial predictor focused on whether default or not - classification problem
data = data.drop(['ChgOffDate', 'BalanceGross', 'ChgOffPrinGr', ], axis=1)
# Create classification by default or not
default_mapping = {"P I F": 0, "CHGOFF": 1}
data['MIS_Status'] = data['MIS_Status'].map(default_mapping)
def clean_currency(x):
if isinstance(x, str):
return x.replace('$', '').replace(',', '')
return x
# Clean up dollar amount to work out cover level
data['GrAppv'] = data['GrAppv'].apply(clean_currency).astype('float')
data['SBA_Appv'] = data['SBA_Appv'].apply(clean_currency).astype('float')
data['ApprovalRatio'] = data['SBA_Appv'] / data['GrAppv']
data = data.drop(['GrAppv', 'SBA_Appv'], axis=1)
data = data.drop(['State', 'Bank'], axis=1)
data = data.dropna(subset=['MIS_Status'])
data['MIS_Status'] = data['MIS_Status'].astype(int)
acceptance = ["Y", "N", "0", "1"]
data = data[data['LowDoc'].isin(acceptance)]
data = data[data['RevLineCr'].isin(acceptance)]
"""binary_mapping = {"Y": 1, "N": 0}
data['RevLineCr'] = data['RevLineCr'].map(binary_mapping)
data['LowDoc'] = data['LowDoc'].map(binary_mapping)
"""
# Prefer One-hot Encoding because there is no logic in the numerical encoding of Revolving Lines or some of the other
# columns
# obj_df["OHC_Code"] = np.where(obj_df["engine_type"].str.contains("ohc"), 1, 0)
data = pd.get_dummies(data, columns=['LowDoc', 'RevLineCr'], prefix=['LowDoc', 'RevLine'])
data['LowDoc_Yes'] = data['LowDoc_Y']
data['RevLine_Yes'] = data['RevLine_1'] + data['RevLine_Y']
data = data.drop(['LowDoc_0', 'LowDoc_Y', 'LowDoc_N', 'RevLine_0', 'RevLine_N', 'RevLine_1', 'RevLine_Y'], axis=1)
data.loc[data['FranchiseCode'] != 1, 'FranchiseCode'] = 0
# Filling in missing data on UrbanRural based on industry type
data['Sector'] = data['NAICS'].astype(str).str[:2]
list = data['Sector'].unique().tolist()
def mode(x):
values, counts = np.unique(x, return_counts = True)
m = counts.argmax()
return values[m]
guess_urbanrural = data.groupby(['Sector'])['UrbanRural'].apply(mode)
for i in list:
data.loc[(data['UrbanRural'] == 2) & (data['Sector'] == i), 'UrbanRural'] = guess_urbanrural.loc[i]
"""data = pd.get_dummies(data, columns=['UrbanRural'], prefix=['UrbanRural'])"""
# Job growth as % of no of employee
data['JobGrowth'] = data['CreateJob'] / data['NoEmp']
data = data.drop(['CreateJob', 'RetainedJob'], axis=1)
# Absolute number of employees into bins
"""data['EmpBand'] = pd.cut(data['NoEmp'], 5)
data[['EmpBand', 'MIS_Status']].groupby(['EmpBand'], as_index=False).mean().sort_values(by='EmpBand', ascending=True)"""
"""data.loc[data['NoEmp'] <= 16, 'NoEmp'] = 0
data.loc[(data['NoEmp'] > 16) & (data['NoEmp'] <= 32), 'NoEmp'] = 1
data.loc[(data['NoEmp'] > 32) & (data['NoEmp'] <= 48), 'NoEmp'] = 2
data.loc[(data['NoEmp'] > 48) & (data['NoEmp'] <= 64), 'NoEmp'] = 3
data.loc[data['NoEmp'] > 64, 'NoEmp'] = 4"""
# Normalising loan terms...
data = data.drop(['NAICS', 'ApprovalFY', 'Sector'], axis=1)
data.info()
"""def ames_eda(df):
eda_df = {}
eda_df['null_sum'] = df.isnull().sum()
eda_df['null_pct'] = df.isnull().mean()
eda_df['dtypes'] = df.dtypes
eda_df['count'] = df.count()
eda_df['mean'] = df.mean()
eda_df['median'] = df.median()
eda_df['min'] = df.min()
eda_df['max'] = df.max()
return pd.DataFrame(eda_df)
"""
targets = data['MIS_Status']
features = data.drop(['MIS_Status'], axis=1)
X, X_test, y, y_test = train_test_split(features, targets, test_size=0.3)
print(X.shape)
print(X_test.shape)
print(y.shape)
print(y_test.shape)
# Create the scaler object with a range of 0-1, remove influence of units
scaler = MinMaxScaler(feature_range=(0, 1))
# Fit on the training data
scaler.fit(X)
# Transform both the training and testing data
X = scaler.transform(X)
X_test = scaler.transform(X_test)
# Convert y to one-dimensional array (vector)
y = np.array(y).reshape((-1,))
y_test = np.array(y_test).reshape((-1,))
svc = SVC()
svc.fit(X, y)
# Y_pred = knn.predict(X_test)
acc_svc = round(svc.score(X, y) * 100, 2)
print('SV Classification: %0.4f' % acc_svc)
knn = KNeighborsClassifier(metric='euclidean', n_neighbors=3)
knn.fit(X, y)
# Y_pred = knn.predict(X_test)
acc_knn = round(knn.score(X, y) * 100, 2)
print('K-Nearest Neighbors Classification: %0.4f' % acc_knn)
repeatedkfold = RepeatedKFold(n_splits=5, n_repeats=3, random_state=1)
cv_results = cross_val_score(knn, X, y, cv=repeatedkfold, scoring='accuracy')
print(mean(cv_results))
gnb = GaussianNB()
gnb.fit(X, y)
# Y_pred = knn.predict(X_test)
acc_gnb = round(gnb.score(X, y) * 100, 2)
print('Naive Bias Classification: %0.4f' % acc_svc)
random_forest = RandomForestClassifier(max_features='log2', n_estimators=1000)
random_forest.fit(X, y)
# Y_pred = random_forest.predict(X_test)
acc_random_forest = round(random_forest.score(X, y) * 100, 2)
print('Random Forest Classifier: %0.4f' % acc_random_forest)
repeatedkfold = RepeatedKFold(n_splits=5, n_repeats=3, random_state=1)
cv_results = cross_val_score(random_forest, X, y, cv=repeatedkfold, scoring='accuracy')
print(mean(cv_results))
gradient_boosted = GradientBoostingClassifier(n_estimators=100)
gradient_boosted.fit(X, y)
# Y_pred = gradient_boosted.predict(X_test)
acc_gradient_boosted = round(gradient_boosted.score(X, y) * 100, 2)
print('Gradient Boosted Classification: %0.4f' % acc_gradient_boosted)
repeatedkfold = RepeatedKFold(n_splits=5, n_repeats=3, random_state=1)
cv_results = cross_val_score(gradient_boosted, X, y, cv=repeatedkfold, scoring='accuracy')
print(mean(cv_results))