text_plain_output_1.png
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)
print(Y_train.shape)