text_plain_output_1.png
from keras import callbacks

from keras.layers import Dense, BatchNormalization, Dropout, LSTM

from keras.models import Sequential

from sklearn.metrics import accuracy_score

from sklearn.metrics import matthews_corrcoef

from sklearn.preprocessing import StandardScaler

from sklearn.tree import DecisionTreeClassifier

import numpy as np

import numpy as np # linear algebra

import torch

import torch

import torch.nn as nn

model = DecisionTreeClassifier(random_state=0)
model.fit(X_train, y_train)

from keras.layers import Dense, BatchNormalization, Dropout, LSTM
from keras.models import Sequential
from keras.utils import to_categorical
from keras import callbacks
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
early_stopping = callbacks.EarlyStopping(min_delta=0.001, patience=20, restore_best_weights=True)
model = Sequential()
model.add(Dense(units=9, kernel_initializer='uniform', activation='relu', input_dim=12))
model.add(Dense(units=9, kernel_initializer='uniform', activation='relu'))
model.add(Dense(units=7, kernel_initializer='uniform', activation='relu'))
model.add(Dense(units=5, kernel_initializer='uniform', activation='relu'))
model.add(Dense(units=1, kernel_initializer='uniform', activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit(X_train, y_train, batch_size=32, epochs=500, validation_split=0.2, callbacks=[early_stopping])
val_accuracy = np.mean(history.history['val_accuracy'])

import torch
import torch.nn as nn
class HeartFailureSimpleNNModel(nn.Module):
    def __init__(self):
        super().__init__()
        self.linear1 = nn.Linear(12, 9)
        self.ReLU1 = nn.ReLU()
        self.linear2 = nn.Linear(9, 9)
        self.ReLU2 = nn.ReLU()
        self.linear3 = nn.Linear(9, 7)
        self.ReLU3 = nn.ReLU()
        self.linear4 = nn.Linear(7, 5)
        self.ReLU4 = nn.ReLU()
        self.linear5 = nn.Linear(5, 1)
    def forward(self, x):
        lin1_out = self.linear1(x)
        ReLU_out1 = self.ReLU1(lin1_out)
        ReLU_out2 = self.ReLU2(self.linear2(ReLU_out1))
        ReLU_out3 = self.ReLU3(self.linear3(ReLU_out2))
        ReLU_out4 = self.ReLU4(self.linear4(ReLU_out3))
        return self.linear5(ReLU_out4).sigmoid_()

net = HeartFailureSimpleNNModel()

criterion = nn.BCELoss()
num_epochs = 500
optimizer = torch.optim.Adam(net.parameters(), lr=0.001)
input_tensor = torch.from_numpy(X_train_2).type(torch.FloatTensor)
label_tensor = torch.from_numpy(y_train_2)
label_tensor = label_tensor.float()
for epoch in range(num_epochs):
    output = net(input_tensor.float())
    loss = criterion(output, label_tensor.unsqueeze(1))
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

validation_tensor = torch.from_numpy(X_val).type(torch.FloatTensor)
print(input_tensor)
print(validation_tensor)
out_probs = net(input_tensor).detach().numpy()
print(out_probs)
out_classes = np.round(out_probs)
print(out_classes)
print(label_tensor)
validation_out_probs = net(validation_tensor).detach().numpy()
print(validation_out_probs)