-
Notifications
You must be signed in to change notification settings - Fork 1
/
arrhythmia.py
155 lines (126 loc) · 5.75 KB
/
arrhythmia.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix
from keras.utils.np_utils import to_categorical
import warnings
warnings.filterwarnings('ignore')
train_df=pd.read_csv('~/HeartDisease/ArrhythmiaDataset/mitbih_train.csv',header=None)
test_df=pd.read_csv('~/HeartDisease/ArrhythmiaDataset/mitbih_test.csv',header=None)
from sklearn.utils import resample
df_1=train_df[train_df[187]==1]
df_2=train_df[train_df[187]==2]
df_3=train_df[train_df[187]==3]
df_4=train_df[train_df[187]==4]
df_0=(train_df[train_df[187]==0]).sample(n=20000,random_state=42)
df_1_upsample=resample(df_1,replace=True,n_samples=20000,random_state=123)
df_2_upsample=resample(df_2,replace=True,n_samples=20000,random_state=124)
df_3_upsample=resample(df_3,replace=True,n_samples=20000,random_state=125)
df_4_upsample=resample(df_4,replace=True,n_samples=20000,random_state=126)
train_df=pd.concat([df_0,df_1_upsample,df_2_upsample,df_3_upsample,df_4_upsample])
equilibre=train_df[187].value_counts()
c=train_df.groupby(187,group_keys=False).apply(lambda train_df : train_df.sample(1))
target_train = train_df[187]
target_test = test_df[187]
y_train = to_categorical(target_train)
y_test = to_categorical(target_test)
X_train = train_df.iloc[:, :186].values
X_test = test_df.iloc[:, :186].values
X_train = X_train.reshape(len(X_train), X_train.shape[1], 1)
X_test = X_test.reshape(len(X_test), X_test.shape[1], 1)
def network(X_train, y_train, X_test, y_test):
im_shape = (X_train.shape[1], 1)
inputs_cnn = Input(shape=(im_shape), name='inputs_cnn')
conv1_1 = Convolution1D(64, (6), activation='relu', input_shape=im_shape)(inputs_cnn)
conv1_1 = BatchNormalization()(conv1_1)
pool1 = MaxPool1D(pool_size=(3), strides=(2), padding="same")(conv1_1)
conv2_1 = Convolution1D(64, (3), activation='relu', input_shape=im_shape)(pool1)
conv2_1 = BatchNormalization()(conv2_1)
pool2 = MaxPool1D(pool_size=(2), strides=(2), padding="same")(conv2_1)
conv3_1 = Convolution1D(64, (3), activation='relu', input_shape=im_shape)(pool2)
conv3_1 = BatchNormalization()(conv3_1)
pool3 = MaxPool1D(pool_size=(2), strides=(2), padding="same")(conv3_1)
flatten = Flatten()(pool3)
dense_end1 = Dense(64, activation='relu')(flatten)
dense_end2 = Dense(32, activation='relu')(dense_end1)
main_output = Dense(5, activation='softmax', name='main_output')(dense_end2)
model = Model(inputs=inputs_cnn, outputs=main_output)
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
callbacks = [EarlyStopping(monitor='val_loss', patience=8),
ModelCheckpoint(filepath='best_model.h5', monitor='val_loss', save_best_only=True)]
history = model.fit(X_train, y_train, epochs=40, callbacks=callbacks, batch_size=32,
validation_data=(X_test, y_test))
model.load_weights('best_model.h5')
return (model, history)
def evaluate_model(history, X_test, y_test, model):
scores = model.evaluate((X_test), y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1] * 100))
print(history)
fig1, ax_acc = plt.subplots()
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.title('Model - Accuracy')
plt.legend(['Training', 'Validation'], loc='lower right')
plt.show()
fig2, ax_loss = plt.subplots()
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.title('Model- Loss')
plt.legend(['Training', 'Validation'], loc='upper right')
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.show()
target_names = ['0', '1', '2', '3', '4']
y_true = []
for element in y_test:
y_true.append(np.argmax(element))
prediction_proba = model.predict(X_test)
prediction = np.argmax(prediction_proba, axis=1)
cnf_matrix = confusion_matrix(y_true, prediction)
from keras.layers import Dense, Convolution1D, MaxPool1D, Flatten, Dropout
from keras.layers import Input
from keras.models import Model
from keras.layers.normalization import BatchNormalization
from keras.callbacks import EarlyStopping, ModelCheckpoint
model, history = network(X_train, y_train, X_test, y_test)
evaluate_model(history,X_test,y_test,model)
y_pred=model.predict(X_test)
import itertools
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
# Compute confusion matrix
cnf_matrix = confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1))
np.set_printoptions(precision=2)
# Plot non-normalized confusion matrix
plt.figure(figsize=(10, 10))
plot_confusion_matrix(cnf_matrix, classes=['N', 'S', 'V', 'F', 'Q'],normalize=True,
title='Confusion matrix, with normalization')
plt.show()