text_plain_output_5.png text_plain_output_4.png application_vnd.jupyter.stderr_output_3.png text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from keras.layers import Dense, LSTM, Dropout, Conv1D, Conv2D, MaxPooling2D, Flatten

from keras.models import Sequential

from os import listdir

from os.path import isfile, join

import numpy

import numpy as np

import pandas as pd

import scipy.io as sio

number_of_classes = 4
def change(x):
    answer = np.zeros(np.shape(x)[0])
    for i in range(np.shape(x)[0]):
        max_value = max(x[i, :])
        max_index = list(x[i, :]).index(max_value)
        answer[i] = max_index
    return answer.astype(int)

mypath = '../input/mitbihecgtraining2017/training2017/'
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f)) and f[0] == 'A']
bats = [f for f in onlyfiles if f[7] == 'm']
mats = [f for f in bats if np.shape(sio.loadmat(mypath + f)['val'])[1] >= 9000]
check = np.shape(sio.loadmat(mypath + mats[0])['val'])[1]
check = 9000
X = np.zeros((len(mats), check))
for i in range(len(mats)):
    X[i, :] = sio.loadmat(mypath + mats[i])['val'][0, :9000]

target_train = np.zeros((len(mats), 1))
Train_data = pd.read_csv(mypath + 'REFERENCE.csv', sep=',', header=None, names=None)
for i in range(len(mats)):
    if Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'N':
        target_train[i] = 0
    elif Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'A':
        target_train[i] = 1
    elif Train_data.loc[Train_data[0] == mats[i][:6], 1].values == 'O':
        target_train[i] = 2
    else:
        target_train[i] = 3
Label_set = np.zeros((len(mats), number_of_classes))
for i in range(np.shape(target_train)[0]):
    dummy = np.zeros(number_of_classes)
    dummy[int(target_train[i])] = 1
    Label_set[i, :] = dummy

train_len = 0.8
X_train = X[:int(train_len * len(mats)), :]
Y_train = Label_set[:int(train_len * len(mats)), :]
X_val = X[int(train_len * len(mats)):, :]
Y_val = Label_set[int(train_len * len(mats)):, :]
n = 20
m = 450
c = 1
X_train = numpy.reshape(X_train, (X_train.shape[0], n, m, c))
X_val = numpy.reshape(X_val, (X_val.shape[0], n, m, c))
image_size = (n, m, c)

batch_size = 32
model = Sequential()
model.add(Conv2D(64, (3, 3), activation='relu', input_shape=image_size, padding='same'))
model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

model.add(Flatten())
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation='relu'))
model.add(Dense(number_of_classes, activation='softmax'))
model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X_train, Y_train, epochs=25, batch_size=batch_size, validation_split=0.3, verbose=1, shuffle=False)
model.evaluate(X_val, Y_val)