-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPimaPrediction.py
320 lines (224 loc) · 7.52 KB
/
PimaPrediction.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#%% [markdown]
# # Predicting Diabetes
# ## Import Libraries
#
#%%
import pandas as pd # pandas is a dataframe library
import matplotlib.pyplot as plt # matplotlib.pyplot plots data
import numpy as np # numpy provides N-dim object support
#%% [markdown]
# ## Load and review data
#%%
df = pd.read_csv("./data/pima-data.csv") # load Pima data
#%%
df.head(5)
#%%
def check(df, size=11):
"""
Function plots a graphical correlation matrix for each pair of columns in the dataframe.
Input:
df: pandas DataFrame
size: vertical and horizontal size of the plot
Displays:
matrix of correlation between columns. Blue-cyan-yellow-red-darkred => less to more correlated
0------------------------->1
Expect a darkred line running from top to bottom right
"""
corr = df.corr() # data frame correlation function
fig, ax = plt.subplots(figsize=(size, size))
ax.matshow(corr) # color code the rectangles by correlation value
plt.xticks(range(len(corr.columns)), corr.columns) # draw x tick marks
plt.yticks(range(len(corr.columns)), corr.columns) # draw y tick marks
#%%
check(df)
#%%
del df['skin']
#%% [markdown]
# ## Check Data Types
#%%
diabetes_map = {True:1, False:0}
#%%
df['diabetes'] = df['diabetes'].map(diabetes_map)
#%%
df.head(5)
#%% [markdown]
# ## Spliting the data
# 70% for training, 30% for testing
#%%
from sklearn.model_selection import train_test_split
feature_col_names = ['num_preg', 'glucose_conc', 'diastolic_bp', 'thickness', 'insulin', 'bmi', 'diab_pred', 'age']
predicted_class_names = ['diabetes']
x = df[feature_col_names].values # predictor feature columns (8 X m)
y = df[predicted_class_names].values # predicted class (1=true, 0=false) column (1 X m)
split_test_size = 0.30
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=split_test_size, random_state=42)
# test_size = 0.3 is 30%, 42 is the answer to everything
#%% [markdown]
# We check to ensure we have the desired 70% train, 30% test split of the data
#%%
print("{0:0.2f}% in training set".format((len(x_train)/len(df.index))*100))
print("{0:0.2f}% in test set".format((len(x_test)/len(df.index))*100))
#%% [markdown]
# ## Post-split Data Preparation
#%% [markdown]
# ### Impute with the mean
#%%
from sklearn.preprocessing import Imputer
# Impute with mean all 0 readings
fill_0 = Imputer(missing_values=0, strategy="mean", axis=0)
x_train = fill_0.fit_transform(x_train)
x_test = fill_0.fit_transform(x_test)
#%% [markdown]
# ## Training Initial Algorithm = Naive Bayes
#%%
from sklearn.naive_bayes import GaussianNB
# create Gaussian Naive Bayes model object and train it with the data
nb_model = GaussianNB()
nb_model.fit(x_train, y_train.ravel())
#%% [markdown]
# ### Performance on Training data
#%%
# predict values using the training data
nb_predict_train = nb_model.predict(x_train)
# import the performance metrics library
from sklearn import metrics
# Accuracy
print("Accurary: {0:.4f}".format(metrics.accuracy_score(y_train, nb_predict_train)))
print("")
#%% [markdown]
# ### Performance on Testing data
#%%
# predict values using the training data
nb_predict_test = nb_model.predict(x_test)
# import the performance metrics library
# from sklearn import metrics
# Accuracy
print("Accurary: {0:.4f}".format(metrics.accuracy_score(y_test, nb_predict_test)))
print("")
#%% [markdown]
# ### Metrics
#%%
print("Confusion Matrix")
print("{0}".format(metrics.confusion_matrix(y_test, nb_predict_test)))
print("")
print("Classification Report")
print(metrics.classification_report(y_test, nb_predict_test))
#%% [markdown]
# ## Retrain = Random Forest
#%%
from sklearn.ensemble import RandomForestClassifier
rf_model = RandomForestClassifier(random_state=42) # Create random forest object
rf_model.fit(x_train, y_train.ravel())
#%% [markdown]
# ### Performance on Training data
#%%
# predict values using the training data
rf_predict_train = rf_model.predict(x_train)
# import the performance metrics library
from sklearn import metrics
# Accuracy
print("Accurary: {0:.4f}".format(metrics.accuracy_score(y_train, rf_predict_train)))
print("")
#%% [markdown]
# ### Performance on Testing data
#%%
# predict values using the testing data
rf_predict_test = rf_model.predict(x_test)
# import the performance metrics library
from sklearn import metrics
# Accuracy
print("Accurary: {0:.4f}".format(metrics.accuracy_score(y_test, rf_predict_test)))
print("")
#%% [markdown]
# ### Metrics
#%%
print("Confusion Matrix")
print("{0}".format(metrics.confusion_matrix(y_test, rf_predict_test)))
print("")
print("Classification Report")
print(metrics.classification_report(y_test, rf_predict_test))
#%% [markdown]
# ## Retrain = Logistic Regression
#%%
from sklearn.linear_model import LogisticRegression
lf_model = LogisticRegression(C=0.7, class_weight="balanced", random_state=42)
lf_model.fit(x_train, y_train.ravel())
#%% [markdown]
# ### Performance on Training data
#%%
# predict values using the training data
lf_predict_train = lf_model.predict(x_train)
# import the performance metrics library
from sklearn import metrics
# Accuracy
print("Accurary: {0:.4f}".format(metrics.accuracy_score(y_train, lf_predict_train)))
print("")
#%% [markdown]
# ### Performance on Testing data
#%%
# predict values using the training data
lf_predict_test = lf_model.predict(x_test)
# import the performance metrics library
from sklearn import metrics
# Accuracy
print("Accurary: {0:.4f}".format(metrics.accuracy_score(y_test, lf_predict_test)))
print("")
#%% [markdown]
# ### Metrics
#%%
print("Confusion Matrix")
print("{0}".format(metrics.confusion_matrix(y_test, lf_predict_test)))
print("")
print("Classification Report")
print(metrics.classification_report(y_test, lf_predict_test))
#%% [markdown]
# ### Setting regularization parameter
#%%
C_start = 0.1
C_end = 10
C_inc = 0.1
C_values, recall_scores = [], []
C_val = C_start
best_recall_score = 0
while (C_val < C_end):
C_values.append(C_val)
lr_model_loop = LogisticRegression(C=C_val, random_state=42)
lr_model_loop.fit(x_train, y_train.ravel())
lr_predict_loop_test = lr_model_loop.predict(x_test)
recall_score = metrics.recall_score(y_test, lr_predict_loop_test)
recall_scores.append(recall_score)
if (recall_score > best_recall_score):
best_recall_score = recall_score
best_lr_predict_test = lr_predict_loop_test
C_val = C_val + C_inc
best_score_C_val = C_values[recall_scores.index(best_recall_score)]
print("1st max value of {0:.3f} occured at C={1:.3f}".format(best_recall_score, best_score_C_val))
plt.plot(C_values, recall_scores, "-")
plt.xlabel("C value")
plt.ylabel("recall score")
#%% [markdown]
# ## Retrain with class_weight='balanced' and C=0.3
#%%
from sklearn.linear_model import LogisticRegression
lr_model = LogisticRegression(C=best_score_C_val, class_weight="balanced", random_state=42)
lr_model.fit(x_train, y_train.ravel())
#%% [markdown]
# ### Performance on Testing data
#%%
# predict values using the training data
lr_predict_test = lr_model.predict(x_test)
# import the performance metrics library
from sklearn import metrics
# Accuracy
print("Accurary: {0:.4f}".format(metrics.accuracy_score(y_test, lr_predict_test)))
print("")
#%% [markdown]
# ### Metrics
#%%
print("Confusion Matrix")
print("{0}".format(metrics.confusion_matrix(y_test, lr_predict_test)))
print("")
print("Classification Report")
print(metrics.classification_report(y_test, lr_predict_test))
print(metrics.recall_score(y_test,lr_predict_test))
#%%