-
Notifications
You must be signed in to change notification settings - Fork 0
/
liver_disease_detection_machine_learning.py
273 lines (198 loc) · 7.95 KB
/
liver_disease_detection_machine_learning.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
# -*- coding: utf-8 -*-
"""PCA_Liver_disease_article.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1M6PyB8Awmb-osk4ZrxMPuHzKeQQAKI0b
"""
import pandas as pd
import seaborn as sns
sns.set(rc={'figure.figsize':(8,8)})
import numpy as np
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
import tensorflow as tf
from sklearn.metrics import confusion_matrix, accuracy_score
dataset = pd.read_csv('/mice_dat_pca.csv')
dataset.head(10)
X = dataset.iloc[:, 1:].values
y = dataset.iloc[:, 0].values
"""Plot the histogram of the terget value"""
sns.histplot(y)
"""Generate synthetic data points"""
from imblearn.over_sampling import SMOTE
sm = SMOTE(random_state=0)
X_res, y_res = sm.fit_resample(X, y)
sns.histplot(y_res)
scaler_orig = StandardScaler()
X_orig_norm = scaler_orig.fit_transform(X)
pca = PCA(n_components=2)
X_proj = pca.fit_transform(X_orig_norm)
sns.scatterplot(x = X_proj[:, 0], y = X_proj[:, 1], hue = y)
scaler_smote = StandardScaler()
X_res_norm = scaler_smote.fit_transform(X_res)
pca_smote = PCA(n_components=2)
X_sm_proj = pca_smote.fit_transform(X_res_norm)
sns.scatterplot(x = X_sm_proj[:, 0], y = X_sm_proj[:, 1], hue = y_res)
"""Data splitting into test and train """
X_train, X_test, y_train, y_test = train_test_split(X_res, y_res, random_state = 0, test_size = 0.2)
"""Now we normalize X_train and X_test separately to avoid information leakage"""
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
"""SEE tutorial: https://www.datacamp.com/community/tutorials/understanding-logistic-regression-python
Classification using ANN (I reduced the number of neurons to avoid excessive overfitting)
"""
ann = tf.keras.models.Sequential()
ann.add(tf.keras.layers.Dense(units= 6, activation='relu'))
ann.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
ann.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
ann.fit(X_train, y_train, batch_size = 32, epochs = 30)
y_pred_ANN = np.round(ann.predict(X_test), 0)
print(confusion_matrix(y_test, y_pred_ANN))
print(accuracy_score(y_test, y_pred_ANN))
"""Desicion trees"""
from sklearn.tree import DecisionTreeClassifier
classifier = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)
y_pred_dt = classifier.predict(X_test)
print(confusion_matrix(y_test, y_pred_dt))
print(accuracy_score(y_test, y_pred_dt))
"""Random Forest"""
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
classifier.fit(X_train, y_train)
y_pred_rf = classifier.predict(X_test)
print(confusion_matrix(y_test, y_pred_rf))
print(accuracy_score(y_test, y_pred_rf))
import sklearn
sklearn.__version__
classifier.get_params(deep=True)
"""Support Vector Machine """
from sklearn import svm
classifier = svm.SVC(C=10, kernel='rbf', random_state = 0)
classifier.fit(X_train, y_train)
y_pred_svm = classifier.predict(X_test)
print(confusion_matrix(y_test, y_pred_svm))
print(accuracy_score(y_test, y_pred_svm))
"""ROC curve for SVM
"""
import matplotlib.pyplot as plt
from itertools import cycle
from sklearn.metrics import roc_curve, auc
from scipy import interp
from sklearn.metrics import roc_auc_score
"""https://www.datatechnotes.com/2019/11/how-to-create-roc-curve-in-python.html
ROC curve for SVM
"""
# Compute ROC curve and ROC area for each class
y_true = y_test # ground truth labels
y_pred = y_pred_svm # predicted probabilities generated by sklearn classifier
fpr, tpr, thresholds = roc_curve(y_true,y_pred)
roc_auc = roc_auc_score(y_true,y_pred)
print("AUC of ROC Curve:", roc_auc)
plt.plot(fpr, tpr)
plt.title("ROC Curve for SVM (0.9674)")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.show()
plt.plot(fpr, tpr, label='ROC curve(area = %.2f)' %roc_auc)
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Random guess')
plt.title('ROC curve for SVM')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.grid()
plt.legend()
plt.show()
"""ROC for ANN"""
# Compute ROC curve and ROC area for each class/ ANN
y_true = y_test # ground truth labels
y_pred = y_pred_ANN # predicted probabilities generated by sklearn classifier
fpr, tpr, thresholds = roc_curve(y_true,y_pred)
roc_auc = roc_auc_score(y_true,y_pred)
print("AUC of ROC Curve:", roc_auc)
plt.plot(fpr, tpr)
plt.title("ROC Curve for ANN (0.8906)")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.show()
plt.plot(fpr, tpr, label='ROC curve(area = %.2f)' %roc_auc)
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Random guess')
plt.title('ROC curve for ANN')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.grid()
plt.legend()
plt.show()
"""ROC for Random Forest"""
# Compute ROC curve and ROC area for each class/ rf
y_true = y_test # ground truth labels
y_pred = y_pred_rf # predicted probabilities generated by sklearn classifier
fpr, tpr, thresholds = roc_curve(y_true,y_pred)
roc_auc = roc_auc_score(y_true,y_pred)
print("AUC of ROC Curve:", roc_auc)
plt.plot(fpr, tpr)
plt.title("ROC Curve for RF (0.98597)")
plt.xlabel("False Positive Rate")
plt.ylabel("True Positive Rate")
plt.show()
plt.plot(fpr, tpr, label='ROC curve(area = %.2f)' %roc_auc)
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r', label='Random guess')
plt.title('ROC curve for RF')
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.grid()
plt.legend()
plt.show()
"""Acuuracy, F1 score, Precision"""
from sklearn import metrics
# Model Accuracy: how often is the classifier correct?
print("Accuracy:",metrics.accuracy_score(y_test, y_pred_rf))
# Model Precision: what percentage of positive tuples are labeled as such?
print("Precision:",metrics.precision_score(y_test, y_pred_rf))
# Model Recall: what percentage of positive tuples are labelled as such?
print("Recall:",metrics.recall_score(y_test, y_pred_rf))
""" K-fold cross-validated paired t test : RV vs. SVM
"""
clf1 = RandomForestClassifier(random_state=1)
clf2 = svm.SVC(random_state=1)
score1 = clf1.fit(X_train, y_train).score(X_test, y_test)
score2 = clf2.fit(X_train, y_train).score(X_test, y_test)
print('Random forest accuracy: %.2f%%' % (score1*100))
print('SVM accuracy: %.2f%%' % (score2*100))
from mlxtend.evaluate import paired_ttest_kfold_cv
t, p = paired_ttest_kfold_cv(estimator1=clf1,
estimator2=clf2,
X=X, y=y,
random_seed=1)
print('t statistic: %.3f' % t)
print('p value: %.3f' % p)
""" K-fold cross-validated paired t test : SVM vs. ANN
"""
clf1 = DecisionTreeClassifier(criterion = 'entropy', random_state = 0)
clf2 = svm.SVC(random_state=1)
score1 = clf1.fit(X_train, y_train).score(X_test, y_test)
score2 = clf2.fit(X_train, y_train).score(X_test, y_test)
print('Random forest accuracy: %.2f%%' % (score1*100))
print('SVM accuracy: %.2f%%' % (score2*100))
from mlxtend.evaluate import paired_ttest_kfold_cv
t, p = paired_ttest_kfold_cv(estimator1=clf1,
estimator2=clf2,
X=X, y=y,
random_seed=1)
print('t statistic: %.3f' % t)
print('p value: %.3f' % p)
"""K-fold cross-validated paired t test : RF vs. ANN"""
from mlxtend.evaluate import paired_ttest_kfold_cv
t, p = paired_ttest_kfold_cv(estimator1=clf1,
estimator2=clf2,
X=X, y=y,
random_seed=1)
print('t statistic: %.3f' % t)
print('p value: %.3f' % p)
clf1 = RandomForestClassifier(random_state=1)
clf2 = ann(criterion = 'entropy', random_state = 0)
score1 = clf1.fit(X_train, y_train).score(X_test, y_test)
score2 = clf2.fit(X_train, y_train).score(X_test, y_test)
print('Random forest accuracy: %.2f%%' % (score1*100))
print('SVM accuracy: %.2f%%' % (score2*100))