ium_434766/stroke-pytorch-eval.py
s434766 3dfc1beec8
Some checks reported errors
s434766-training/pipeline/head Something is wrong with the build of this commit
jobs
2021-05-07 21:30:35 +02:00

51 lines
1.4 KiB
Python

import torch
import torch.nn as nn
import numpy as np
from os import path
import torch.nn.functional as F
from torch import nn
from torch.autograd import Variable
import torchvision.transforms as transforms
import pandas as pd
from sklearn.metrics import accuracy_score
from sklearn.metrics import mean_squared_error
from sklearn.metrics import classification_report
import matplotlib.pyplot as plt
class LogisticRegressionModel(nn.Module):
def __init__(self, input_dim, output_dim):
super(LogisticRegressionModel, self).__init__()
self.linear = nn.Linear(input_dim, output_dim)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
out = self.linear(x)
return self.sigmoid(out)
np.set_printoptions(suppress=False)
data_test = pd.read_csv("data_test.csv")
FEATURES = ['age','hypertension','heart_disease','ever_married', 'avg_glucose_level', 'bmi']
x_test = data_test[FEATURES].astype(np.float32)
y_test = data_test['stroke'].astype(np.float32)
fTest= torch.from_numpy(x_test.values)
tTest = torch.from_numpy(y_test.values)
model = LogisticRegressionModel(6,1)
model.load_state_dict(torch.load('stroke.pth'))
y_pred = model(fTest)
rmse = mean_squared_error(tTest, y_pred.detach().numpy())
acc = accuracy_score(tTest, np.argmax(y_pred.detach().numpy(), axis=1))
print('-' * 60)
print(classification_report(tTest, y_pred.detach().numpy().round()))
print(f" RMSE: {rmse}")
print(f" Accuracy: {acc}")
print('-' * 60)