0% found this document useful (0 votes)
12 views2 pages

BTVN6 Code

The document outlines a Python script for building and training a neural network model to predict diabetes using the Pima Indians Diabetes dataset. It includes steps for data loading, preprocessing, model creation, training, evaluation, and plotting learning curves. The model is compiled with binary crossentropy loss and trained for 50 epochs, with test accuracy printed at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

BTVN6 Code

The document outlines a Python script for building and training a neural network model to predict diabetes using the Pima Indians Diabetes dataset. It includes steps for data loading, preprocessing, model creation, training, evaluation, and plotting learning curves. The model is compiled with binary crossentropy loss and trained for 50 epochs, with test accuracy printed at the end.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#1.

import library
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import classification_report, confusion_matrix,
ConfusionMatrixDisplay
from tensorflow import keras

#2.Load and prepare dataset


url ="https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-
diabetes.data.csv"
columns =["Pregnancies" ,"Glucose" , "BloodPressure" , "SkinThickness" ,
"Insulin" , "BMI" ,
"DiabetesPedigreeFunction" , "Age" , "OutCome"]
df = pd.read_csv(url , names=columns)
print(df)

# Separate features and labels


X = df.drop("OutCome", axis=1).values
y = df["OutCome"].values

# 3.Train/Valid/Test split (64% train, 16% valid, 20% test)


X_temp, X_test, y_temp, y_test =
train_test_split(X,y,test_size=0.2,random_state=42)
X_train, X_valid, y_train, y_valid = train_test_split(X_temp, y_temp,
test_size=0.2, random_state=42)

#4. Standardize the data


scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_valid = scaler.transform(X_valid)
X_test = scaler.transform(X_test)
X.shape[1]

#5.Build the neural network model


model = keras.models.Sequential([
keras.layers.Dense(32,activation="relu", input_shape=[X.shape[1]]),
keras.layers.Dense(16,activation="relu"),
keras.layers.Dense(1,activation="sigmoid")

])

model.summary()
#6. Compile the model
model.compile(loss="binary_crossentropy",
optimizer="adam",
metrics=["accuracy"])

#7.Train the model


history = model.fit(X_train,y_train,epochs=50, validation_data=(X_valid,y_valid))
#8.Evalute model on test set
loss, accuracy = model.evaluate (X_test,y_test)
print(f"Test Accuracy: {accuracy:.4f}")

#9.Plot accuracy and loss over epochs


def plot_learning_curves(history):
pd.DataFrame(history.history).plot(figsize=(10,5))
plt.grid(True)
plt.gca().set_ylim(0,1)
plt.xlabel("Epochs")
plt.title("Accuracy & Loss Over Epochs")
plt.show()

plot_learning_curves(history)

You might also like