Login
Order Now
Support
Python Task to Predict the Syndrome Existence

Python Task to Predict the Syndrome Existence

  • 17th Jun, 2022
  • 16:00 PM

from keras.layers import Dense, Dropout, LSTM, Embedding
from keras.preprocessing.sequence import pad_sequences
from keras.models import Sequential
import pandas as pd
import numpy as np

def read_data():
    min_len = -1
    max_len = -1
    X_train = []
    Y_train = []
    X_test = []
    Y_test = []

    for i in range(1, 7):
        loc = 'without syndrome/control' + str(i) + '.xlsx'
        excel_data = pd.read_excel(loc)
        data = excel_data['HR'].tolist()
        if min_len == -1 or min_len > len(data):
            min_len = len(data)
        if max_len == -1 or max_len < len(data):
            max_len = len(data)
        X_train.append(data)
        Y_train.append(0)

    for i in range(1, 7):
        loc = 'with syndrome/NAS' + str(i) + '.xlsx'
        excel_data = pd.read_excel(loc)
        data = excel_data['HR'].tolist()
        if min_len == -1 or min_len > len(data):
            min_len = len(data)
        if max_len == -1 or max_len < len(data):
            max_len = len(data)
        X_train.append(np.array(data))
        Y_train.append(1)

    for i in range(1, 3):
        loc = 'test/test' + str(i) + '.xlsx'
        excel_data = pd.read_excel(loc)
        data = excel_data['HR'].tolist()
        if min_len == -1 or min_len > len(data):
            min_len = len(data)
        if max_len == -1 or max_len < len(data):
            max_len = len(data)
        X_test.append(data)
        Y_test.append(i)


    truncated = []
    for xt in X_train:
        truncated.append(xt[:min_len]);

    truncated_test = []
    for xt in X_test:
        truncated_test.append(xt[:min_len]);

    return min_len, np.array(truncated), np.array(Y_train), np.array(truncated_test), np.array(Y_test)


def create_model(input_length):
    print('Creating model...')
    model = Sequential()
    model.add(Embedding(input_dim=300, output_dim=50, input_length = input_length))
    model.add(LSTM(256, activation='sigmoid', return_sequences=True))
    model.add(Dropout(0.5))
    model.add(LSTM(256, activation='sigmoid'))
    model.add(Dropout(0.5))
    model.add(Dense(1, activation='sigmoid'))

    print('Compiling...')
    model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
    return model


if __name__ == '__main__':
    min_len, X_train, Y_train, X_test, Y_test = read_data()

    print(min_len)

    model = create_model(min_len)

    print('Fitting model...')

    hist = model.fit(X_train, Y_train, batch_size=64, epochs=10, validation_split=0.1, verbose=1)

    score, acc = model.evaluate(X_test, Y_test, batch_size=1)

    print('Test score:', score)
    print('Test accuracy:', acc)
 

Share this post

assignment helpassignment helperassignment expertsassignment writing services