-
Notifications
You must be signed in to change notification settings - Fork 6
/
model.py
235 lines (191 loc) · 8.2 KB
/
model.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
import pandas as pd
import pandas_profiling as pp
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import warnings
import os
import plotly.graph_objects as go
import plotly.io as pio
import pickle
from sklearn.utils import resample
# Metrics
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix, auc, roc_curve
# Validation
from sklearn.model_selection import train_test_split, cross_val_score, KFold
from sklearn.pipeline import Pipeline, make_pipeline
# Tuning
from sklearn.model_selection import GridSearchCV
# Feature Extraction
from sklearn.feature_selection import RFE
# Preprocessing
from sklearn.preprocessing import MinMaxScaler, StandardScaler, Normalizer, Binarizer, LabelEncoder
# Models
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.linear_model import LogisticRegression
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.tree import DecisionTreeClassifier
# Ensembles
from sklearn.ensemble import RandomForestClassifier
from sklearn.ensemble import BaggingClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.ensemble import ExtraTreesClassifier
warnings.filterwarnings('ignore')
sns.set_style("whitegrid", {'axes.grid' : False})
pio.templates.default = "plotly_white"
df = pd.read_csv('heart_failure_clinical_records_dataset.csv')
# Analyze Data
def explore_data(df):
print("Number of Instances and Attributes:", df.shape)
print('\n')
print('Dataset columns:',df.columns)
print('\n')
print('Data types of each columns: ', df.info())
# Checking for duplicates
def checking_removing_duplicates(df):
count_dups = df.duplicated().sum()
print("Number of Duplicates: ", count_dups)
if count_dups >= 1:
df.drop_duplicates(inplace=True)
print('Duplicate values removed!')
else:
print('No Duplicate values')
# Split training and validation set
def read_in_and_split_data(data, target):
X = data.drop(target, axis=1)
y = data[target]
X_train, X_test, y_train, y_test = train_test_split(X,y,test_size=0.2, random_state=0)
return X_train, X_test, y_train, y_test
# Spot-Check Algorithms
def GetModel():
Models = []
Models.append(('LR' , LogisticRegression()))
Models.append(('LDA' , LinearDiscriminantAnalysis()))
Models.append(('KNN' , KNeighborsClassifier()))
Models.append(('CART' , DecisionTreeClassifier()))
Models.append(('NB' , GaussianNB()))
Models.append(('SVM' , SVC(probability=True)))
return Models
def ensemblemodels():
ensembles = []
ensembles.append(('AB' , AdaBoostClassifier()))
ensembles.append(('GBM' , GradientBoostingClassifier()))
ensembles.append(('RF' , RandomForestClassifier()))
ensembles.append(( 'Bagging' , BaggingClassifier()))
ensembles.append(('ET', ExtraTreesClassifier()))
return ensembles
# Spot-Check Normalized Models
def NormalizedModel(nameOfScaler):
if nameOfScaler == 'standard':
scaler = StandardScaler()
elif nameOfScaler =='minmax':
scaler = MinMaxScaler()
elif nameOfScaler == 'normalizer':
scaler = Normalizer()
elif nameOfScaler == 'binarizer':
scaler = Binarizer()
pipelines = []
pipelines.append((nameOfScaler+'LR' , Pipeline([('Scaler', scaler),('LR' , LogisticRegression())])))
pipelines.append((nameOfScaler+'LDA' , Pipeline([('Scaler', scaler),('LDA' , LinearDiscriminantAnalysis())])))
pipelines.append((nameOfScaler+'KNN' , Pipeline([('Scaler', scaler),('KNN' , KNeighborsClassifier())])))
pipelines.append((nameOfScaler+'CART', Pipeline([('Scaler', scaler),('CART', DecisionTreeClassifier())])))
pipelines.append((nameOfScaler+'NB' , Pipeline([('Scaler', scaler),('NB' , GaussianNB())])))
pipelines.append((nameOfScaler+'SVM' , Pipeline([('Scaler', scaler),('SVM' , SVC())])))
pipelines.append((nameOfScaler+'AB' , Pipeline([('Scaler', scaler),('AB' , AdaBoostClassifier())]) ))
pipelines.append((nameOfScaler+'GBM' , Pipeline([('Scaler', scaler),('GMB' , GradientBoostingClassifier())]) ))
pipelines.append((nameOfScaler+'RF' , Pipeline([('Scaler', scaler),('RF' , RandomForestClassifier())]) ))
pipelines.append((nameOfScaler+'ET' , Pipeline([('Scaler', scaler),('ET' , ExtraTreesClassifier())]) ))
return pipelines
# Train model
def fit_model(X_train, y_train,models):
# Test options and evaluation metric
num_folds = 10
scoring = 'accuracy'
results = []
names = []
for name, model in models:
kfold = KFold(n_splits=num_folds, random_state=0)
cv_results = cross_val_score(model, X_train, y_train, cv=kfold, scoring=scoring)
results.append(cv_results)
names.append(name)
msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
print(msg)
return names, results
# Save trained model
def save_model(model,filename):
pickle.dump(model, open(filename, 'wb'))
# Performance Measure
def classification_metrics(model, conf_matrix):
print(f"Training Accuracy Score: {model.score(X_train, y_train) * 100:.1f}%")
print(f"Validation Accuracy Score: {model.score(X_test, y_test) * 100:.1f}%")
fig,ax = plt.subplots(figsize=(8,6))
sns.heatmap(pd.DataFrame(conf_matrix), annot = True, cmap = 'YlGnBu',fmt = 'g')
ax.xaxis.set_label_position('top')
plt.tight_layout()
plt.title('Confusion Matrix', fontsize=20, y=1.1)
plt.ylabel('Actual label', fontsize=15)
plt.xlabel('Predicted label', fontsize=15)
plt.show()
print(classification_report(y_test, y_pred))
# ROC_AUC
def roc_auc(y_test, y_pred):
fpr, tpr, thresholds = roc_curve(y_test, y_pred)
plt.figure(figsize=(8,6))
print(f"roc_auc score: {auc(fpr, tpr)*100:.1f}%")
plt.plot(fpr, tpr, color='orange', label='ROC')
plt.plot([0, 1], [0, 1], color='darkblue', linestyle='--')
plt.xlabel('False Positive Rate',fontsize=12)
plt.ylabel('True Positive Rate', fontsize=12)
plt.title('Receiver Operating Characteristic (ROC) Curve', fontsize=20)
plt.legend()
plt.show()
Q1 = df.quantile(0.25)
Q3 = df.quantile(0.75)
IQR = Q3 - Q1
df_out = df[~((df < (Q1 - 1.5 * IQR)) |(df > (Q3 + 1.5 * IQR))).any(axis=1)]
live = df_out[df_out.DEATH_EVENT == 0]
die = df_out[df_out.DEATH_EVENT==1]
die_upsampled = resample(die,
replace=True,
n_samples=len(live),
random_state=0)
upsampled = pd.concat([live, die_upsampled])
# Split data to training and validation set
target = 'DEATH_EVENT'
X_train, X_test, y_train, y_test = read_in_and_split_data(upsampled, target)
#Fit data to model
models = GetModel()
names,results = fit_model(X_train, y_train,models)
# Normalize data to improve accuracy
ScaledModel = NormalizedModel('minmax')
name,results = fit_model(X_train, y_train, ScaledModel)
# Fine tunong
model = GradientBoostingClassifier()
n_estimators = [10, 100, 1000]
learning_rate = [0.001, 0.01, 0.1]
subsample = [0.5, 0.7, 1.0]
max_depth = [3, 7, 9]
#define grid search
grid = dict(learning_rate=learning_rate, n_estimators=n_estimators, subsample=subsample, max_depth=max_depth)
kfold = KFold(n_splits=10, random_state=0)
cv_results = cross_val_score(model, X_train, y_train, cv=10, scoring='accuracy')
grid_search = GridSearchCV(estimator=model, param_grid=grid, n_jobs=-1, cv=10, scoring='accuracy',error_score=0)
grid_result = grid_search.fit(X_train, y_train)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
# Predict unseen data
pipeline = make_pipeline(MinMaxScaler(), GradientBoostingClassifier(learning_rate=0.1, max_depth=9, n_estimators=1000, subsample=0.7))
model = pipeline.fit(X_train, y_train)
y_pred = model.predict(X_test)
conf_matrix = confusion_matrix(y_test,y_pred)
classification_metrics(pipeline, conf_matrix)
roc_auc(y_test, y_pred)
save_model(model, 'model.pkl')