In [1]:
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torch.utils.data as Data
import matplotlib.pyplot as plt
import numpy as np
import imageio
Krok 1. Tworzenie zbioru danych¶
In [2]:
import pandas as pd
df = pd.read_csv('c:/1/Stroke_Prediction_NUM.csv')
# , nrows=1000,parse_dates=['Date'],usecols = ['Unnamed: 0','Date','Consumption']
print(df.shape)
print(df.dtypes)
print(df['Unnamed: 0'].max())
print(df.columns)
df.head(3)
Out[2]:
In [3]:
df.describe(include=[np.number]).columns
Out[3]:
In [4]:
import torch
import torch.nn as nn
# Create dummy input and target tensors (data)
x = torch.tensor((df['Hypertension'],df['Heart_Disease'],df['Avg_Glucose'],df['BMI'],df['Age_years'],df['Gender_C'],df['Ever_Married_C'],df['Type_Of_Work_C'],df['Residence_C'],df['Smoking_Status_C'],df['Age_years_10_C']), dtype=torch.float)
y = torch.tensor(df['Stroke'], dtype=torch.float)
In [5]:
x =torch.transpose(x.flip(0),0,1)
y = y.reshape(-1,1)
y = y.view(y.shape[0],1)
In [6]:
x
Out[6]:
In [7]:
y
Out[7]:
In [8]:
print('x.shape: ',x.shape)
print('y.shape: ',y.shape)
In [9]:
# Create a model
model = nn.Sequential(nn.Linear(11, 21),
nn.ReLU(),
nn.Linear(21, 1),
nn.Sigmoid())
In [10]:
# Construct the loss function
criterion = torch.nn.MSELoss()
# Construct the optimizer (Stochastic Gradient Descent in this case)
optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
In [11]:
# Gradient Descent
for epoch in range(100):
# Forward pass: Compute predicted y by passing x to the model
y_pred = model(x)
# Compute and print loss
loss = criterion(y_pred, y)
print('epoch: ', epoch,' loss: ', loss.item())
# Zero gradients, perform a backward pass, and update the weights.
optimizer.zero_grad()
# perform a backward pass (backpropagation)
loss.backward()
# Update the parameters
optimizer.step()
In [12]:
y_pred
Out[12]:
