This repository showcases the creation of an Artificial Neural Network (ANN) using PyTorch to predict diabetes based on the Pima Indian Diabetes Dataset. The project covers all essential steps, from data preprocessing to model evaluation, and also provides a Docker setup for easy replication.
- Project Overview
- Project Structure
- Getting Started
- Running with Docker
- Key Steps in the Project
- Results
- Data Preprocessing: Loaded the dataset, handled missing values, and labeled the target variable.
- Modeling: Built a custom neural network with PyTorch, trained it, and evaluated its performance.
- Visualization: Visualized the results using a confusion matrix heatmap.
- data/: Contains the dataset used in the project.
- notebooks/: Jupyter notebooks with the code and explanations.
- requirements.txt: Python dependencies required to run the project.
- README.md: Overview of the project (this file).
Make sure you have Python 3.x and pip installed on your system.
-
Clone the repository:
git clone https://github.com/your-username/PYTORCH-DIABETES-PREDICTION.git cd PYTORCH-DIABETES-PREDICTION -
Install the required Python packages:
pip install -r requirements.txt
-
Run the Jupyter notebook:
jupyter notebook notebooks/Creating_ANN_with_PyTorch.ipynb
Build the Docker image using the Dockerfile:
docker build -t pytorch-jupyter .Run a container from the image you just built:
docker run -it --name pytorch-jupyter-container -p 8888:8888 pytorch-jupyterAfter running the container, you will see a URL in the terminal with a token, something like this:
http://127.0.0.1:8888/?token=<your-token-here>Loaded the dataset, checked for missing values, and replaced the numeric target variable with descriptive labels ("Diabetic" and "No Diabetic").
import pandas as pd
# Load the dataset
df = pd.read_csv('data/diabetes.csv')
# Check for missing values
print(df.isnull().sum())
# Replace numeric target with descriptive labels
df['Outcome'] = df['Outcome'].replace({1: "Diabetic", 0: "No Diabetic"})Split the DataFrame into independent features (X) and the target variable (y).
# Separate features and target variable
X = df.drop('Outcome', axis=1).values
y = df['Outcome'].valuesDivided the dataset into training and testing sets using an 80-20 split.
from sklearn.model_selection import train_test_split
# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)Converted the training and testing data into PyTorch tensors for model training.
import torch
# Convert data to PyTorch tensors
X_train = torch.FloatTensor(X_train)
X_test = torch.FloatTensor(X_test)
y_train = torch.LongTensor(y_train)
y_test = torch.LongTensor(y_test)Created a custom neural network model class (ANN_Model) with two hidden layers using PyTorch.
import torch.nn as nn
import torch.nn.functional as F
# Define the neural network model
class ANN_Model(nn.Module):
def __init__(self, input_features=8, hidden1=20, hidden2=20, out_features=2):
super().__init__()
self.f_connected1 = nn.Linear(input_features, hidden1)
self.f_connected2 = nn.Linear(hidden1, hidden2)
self.out = nn.Linear(hidden2, out_features)
def forward(self, x):
x = F.relu(self.f_connected1(x))
x = F.relu(self.f_connected2(x))
x = self.out(x)
return xInitialized the model and set the random seed for reproducibility.
# Set the random seed for reproducibility
torch.manual_seed(20)
# Instantiate the model
model = ANN_Model()Defined the loss function (CrossEntropyLoss) and the optimizer (Adam) to guide the model training.
# Define the loss function and optimizer
loss_function = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)Trained the model over 500 epochs, recording the loss at each epoch and updating the model's parameters using backpropagation.
# Train the model
epochs = 500
final_losses = []
for i in range(epochs):
i = i + 1
y_pred = model.forward(X_train)
loss = loss_function(y_pred, y_train)
final_losses.append(loss)
if i % 10 == 1:
print(f"Epoch number: {i} and the loss: {loss.item()}")
optimizer.zero_grad()
loss.backward()
optimizer.step()Set the model to evaluation mode to ensure correct behavior during testing.
# Set the model to evaluation mode
model.eval()Made predictions on the test data and stored them.
# Make predictions on the test data
predictions = []
with torch.no_grad():
for i, data in enumerate(X_test):
y_pred = model(data)
predictions.append(y_pred.argmax().item())Calculated the confusion matrix to evaluate the performance of the model's predictions.
from sklearn.metrics import confusion_matrix
# Calculate the confusion matrix
cm = confusion_matrix(y_test, predictions)
print(cm)Visualized the confusion matrix using a heatmap to better understand prediction accuracy.
import seaborn as sns
import matplotlib.pyplot as plt
# Visualize the confusion matrix
plt.figure(figsize=(10,6))
sns.heatmap(cm, annot=True, fmt="d")
plt.xlabel('Actual Values')
plt.ylabel('Predicted Values')
plt.show()Computed the accuracy of the model's predictions on the test data.
from sklearn.metrics import accuracy_score
# Calculate the accuracy score
score = accuracy_score(y_test, predictions)
print(f'Accuracy: {score}')