-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathann.py
245 lines (201 loc) · 9.9 KB
/
ann.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import time
import datetime
import tensorflow as tf
import keras_tuner
from keras.utils import plot_model
import matplotlib.pyplot as plt
import matplotlib.style
matplotlib.style.use('seaborn-v0_8-whitegrid') # Set global theme style
from src.data_loader import load_data
from src.data_preprocessing import split_data, preprocess_data, transform_features
from src.model_train import *
from src.hpmodel import *
random_state = 42
# Data loader
start_time = time.time()
raw_data_path = './data/raw_loan_data.csv'
metadata_path = './data/metadata.txt'
raw_data, num_features, cat_features, metadata = load_data(raw_data_path, metadata_path)
print(f"Data loading time: {time.time() - start_time:.2f} seconds")
# original indices
original_indices = raw_data.index
# Data split
start_time = time.time()
X_train, X_val, X_test, y_train_loan_status, y_val_loan_status, y_test_loan_status, y_train_score, y_val_score, y_test_score = split_data(raw_data)
# outlier detection
from sklearn.ensemble import IsolationForest
outlier_features = ['person_age', 'person_emp_exp', 'cb_person_cred_hist_length']
outlier_detector = IsolationForest(contamination=0.007, random_state=random_state)
X_train['outliers'] = outlier_detector.fit_predict(X_train[outlier_features])
indices_to_remove = X_train[X_train['outliers'] == -1].index
# Plot box plot
fig, axs = plt.subplots(3, 1, figsize=(12, 12), sharex=True)
for i, feature in enumerate(X_train[['person_age', 'person_emp_exp', 'cb_person_cred_hist_length']].columns):
description = metadata.loc[metadata['Feature'] == feature, 'Description'].values[0]
ax = axs[i]
boxplot = ax.boxplot([X_train[X_train['outliers'] == 1][feature], X_train[X_train['outliers'] == -1][feature]],
labels=['Inliers', 'Outliers'], vert=False)
ax.set_title(f'Box Plot of {description}', fontweight='bold', fontsize=14)
ax.set_xlabel(description)
ax.set_ylabel(f'Outliers Detection')
# # Add mean annotation for inliers
# mean_inliers = boxplot['means'][0].get_xdata()[0]
# ax.annotate(f'Mean: {mean_inliers:.2f}', xy=(mean_inliers, 1), xytext=(mean_inliers + 0.5, 1.1),
# horizontalalignment='center', verticalalignment='center')
# # Add mean annotation for outliers
# mean_outliers = boxplot['means'][1].get_xdata()[0]
# ax.annotate(f'Mean: {mean_outliers:.2f}', xy=(mean_outliers, 2), xytext=(mean_outliers + 1, 2.1),
# horizontalalignment='center', verticalalignment='center')
# Draw vertical line through min values
# min_inliers = boxplot['whiskers'][0].get_xdata()[0]
min_outliers = boxplot['caps'][2].get_xdata()[0]
# ax.axvline(min_inliers, color='r', linestyle='--', label='Min Inliers')
ax.axvline(min_outliers, color='r', linestyle='--', label='Min Outliers')
# Add tick label for red line
# ax.annotate(f'{min_inliers:.2f}', xy=(min_inliers, 0.5), xytext=(min_inliers, 0.5),
# color='red', horizontalalignment='center', verticalalignment='center')
# ax.annotate(f'{min_outliers:.2f}', xy=(min_outliers, 1.5), xytext=(min_outliers, 1.5),
# color='red', horizontalalignment='center', verticalalignment='center')
ax.legend()
fig.tight_layout()
plt.show()
# Remove outliers
X_train.drop(index=indices_to_remove, columns='outliers', inplace=True)
y_train_loan_status.drop(labels=indices_to_remove, inplace=True)
y_train_score.drop(labels=indices_to_remove, inplace=True)
# Remove outliers from validation set
X_val['outliers'] = outlier_detector.predict(X_val[outlier_features])
val_indices_to_remove = X_val[X_val['outliers'] == -1].index
X_val.drop(index=val_indices_to_remove, columns='outliers', inplace=True)
y_val_loan_status.drop(labels=val_indices_to_remove, inplace=True)
y_val_score.drop(labels=val_indices_to_remove, inplace=True)
# Remove outliers from test set
X_test['outliers'] = outlier_detector.predict(X_test[outlier_features])
test_indices_to_remove = X_test[X_test['outliers'] == -1].index
X_test.drop(index=test_indices_to_remove, columns='outliers', inplace=True)
y_test_loan_status.drop(labels=test_indices_to_remove, inplace=True)
y_test_score.drop(labels=test_indices_to_remove, inplace=True)
# plt.figure(figsize=(12, 8))
# plt.scatter(X_train['person_age'], X_train['person_emp_exp'], alpha=0.5)
# plt.title('Scatter Plot of Person Age vs Employment Experience', fontweight='bold', fontsize=14)
# plt.xlabel('Person Age')
# plt.ylabel('Employment Experience')
# plt.show()
# plt.figure(figsize=(12, 8))
# scatter = plt.scatter(X_train['cb_person_cred_hist_length'], X_train['person_age'], c=X_train['loan_intent'].astype('category').cat.codes, alpha=0.5, cmap='viridis')
# plt.title('Scatter Plot of Credit History Length vs Person Age', fontweight='bold', fontsize=14)
# plt.xlabel('Credit History Length')
# plt.ylabel('Person Age')
# plt.colorbar(scatter, label='Loan Intent')
# # Add horizontal line at 60
# plt.axhline(y=60, color='r', linestyle='--', label='Age 60')
# # Add legend
# loan_intent_labels = X_train['loan_intent'].astype('category').cat.categories
# handles = [plt.Line2D([0], [0], marker='o', color='w', markerfacecolor=plt.cm.viridis(i / len(loan_intent_labels)), markersize=10) for i in range(len(loan_intent_labels))]
# handles.append(plt.Line2D([0], [0], color='r', linestyle='--')) # Add horizontal line to legend
# plt.legend(handles, list(loan_intent_labels) + ['Age 60'], title='Loan Intent')
# plt.show()
# Data preprocessing
X_train_transform, X_val_transform, X_test_transform, y_train_score_transform, y_val_score_transform, y_test_score_transform, col_transformer, y_scaler = preprocess_data(
X_train, X_val, X_test, y_train_score, y_val_score, y_test_score, num_features, cat_features)
# keep track features for interpretate
transform_features = transform_features(col_transformer)
# plot features after transform
fig, axs = plt.subplots(4, 3, figsize=(24, 18), sharex=True)
for i, feature in enumerate(transform_features):
ax = axs[i//3, i%3]
boxplot = ax.boxplot(X_test_transform[:, i], showfliers=True, vert=False, showmeans=True, meanline=True)
description = metadata.loc[metadata['Feature'] == feature, 'Description'].values[0]
ax.set_title(f'Box Plot of {description}', fontweight='bold', fontsize=14)
ax.yaxis.set_ticklabels([]) # Set y-tick labels off
# Add mean annotation
# mean = boxplot['means'][0].get_xdata()[0]
# ax.annotate(f'Mean: {mean:.2f}', xy=(mean, 1), xytext=(mean, 1.1),
# arrowprops=dict(facecolor='black', shrink=0.05),
# horizontalalignment='center', verticalalignment='center')
# Add x-axis label
ax.set_xlabel('Value')
# Add legend for mean and median lines
ax.legend([boxplot['means'][0], boxplot['medians'][0]], ['Mean', 'Median'], loc='upper right')
fig.tight_layout()
plt.show()
# Hyperparameter tuning
tuner_log_dir = f"logs/tuner/tuner_{datetime.datetime.now().strftime('%Y%m%d-%H%M%S')}"
tuner_save_check_point = f'save_tuner/checkpoint_{datetime.datetime.now().strftime("%Y%m%d-%H%M%S")}.keras'
# Create TensorBoard callback
tuner_tensorboard_callback = keras.callbacks.TensorBoard(
log_dir=tuner_log_dir,
histogram_freq=1,
profile_batch='500,520'
)
tuner_early_stop_callback = keras.callbacks.EarlyStopping(
monitor='val_output_status_accuracy',
patience=3,
mode='max'
)
tuner_check_point = keras.callbacks.ModelCheckpoint(
filepath='best_tuner.keras',
monitor='val_output_status_accuracy',
save_best_only=True,
)
tuner_callbacks = [tuner_tensorboard_callback, tuner_early_stop_callback, tuner_check_point]
tuner = keras_tuner.GridSearch(
hypermodel=build_hyper_model,
objective=[
keras_tuner.Objective('val_output_status_accuracy', direction='max'),
keras_tuner.Objective('val_output_score_mean_squared_error', direction='min')
],
max_trials=50,
directory=tuner_log_dir,
project_name='loan_default_prediction',
overwrite=True
)
# Tuning hyperparameters
start_time = time.time()
tuner.search(
X_train_transform, y=[y_train_loan_status, y_train_score_transform],
validation_data=(X_val_transform, [y_val_loan_status, y_val_score_transform]), epochs=50,
callbacks=tuner_callbacks
)
# Retrieve and save the top 10 best models
save_dir = 'save_tuner/top_best/'
os.makedirs(save_dir, exist_ok=True)
best_models = tuner.get_best_models(num_models=10)
for i, model in enumerate(best_models):
model.save(f'save_tuner/top_best/{i+1}_{datetime.datetime.now().strftime("%Y%m%d-%H%M%S")}.keras')
save_worst = 'save_tuners/top_worst/'
os.makedirs(save_worst, exist_ok=True)
worst_models = tuner.get_best_models(num_models=-10)
for i, model in enumerate(worst_models):
model.save(f'save_models/top_worst/{i+1}_{datetime.datetime.now().strftime("%Y%m%d-%H%M%S")}.keras')
# load the best model
best_model = tuner.get_best_models(num_models=1)[0]
# calbacks for fit model
fit_log_dir = f"logs/fits/fit_{datetime.datetime.now().strftime('%Y%m%d-%H%M%S')}"
fit_save_check_point = f'save_fits/checkpoint_{datetime.datetime.now().strftime("%Y%m%d-%H%M%S")}.keras'
# Create TensorBoard callback
fit_tensorboard_callback = keras.callbacks.TensorBoard(
log_dir=fit_log_dir,
histogram_freq=1,
profile_batch='500,520'
)
fit_early_stop_callback = keras.callbacks.EarlyStopping(
monitor='val_output_status_accuracy',
patience=3,
mode='max'
)
fit_check_point = keras.callbacks.ModelCheckpoint(
filepath=fit_save_check_point,
monitor='val_output_status_accuracy',
save_best_only=True,
)
fit_callbacks = [fit_tensorboard_callback, fit_early_stop_callback, fit_check_point]
# fit the best model
history = best_model.fit(
X_train_transform, [y_train_loan_status, y_train_score_transform],
validation_data=(X_val_transform, [y_val_loan_status, y_val_score_transform]),
epochs=100, callbacks=fit_callbacks, shuffle=True
)
# model evaluation
evaluation = best_model.evaluate(X_test_transform, [y_test_loan_status, y_test_score_transform], return_dict=True)