|
| 1 | +import numpy as np |
| 2 | +import pandas as pd |
| 3 | +from sklearn.model_selection import train_test_split |
| 4 | +from sklearn.preprocessing import StandardScaler |
| 5 | +import tensorflow as tf |
| 6 | +from tensorflow import keras |
| 7 | +from tensorflow.keras import layers |
| 8 | + |
| 9 | +# Load the Pima Indians Diabetes dataset |
| 10 | +file_path = "pimaindiandiabetes.csv" # Replace with the actual file path |
| 11 | +columns = ["Pregnancies", "Glucose", "BloodPressure", "SkinThickness", "Insulin", "BMI", "DiabetesPedigreeFunction", "Age", "Outcome"] |
| 12 | +data = pd.read_csv(file_path, names=columns) |
| 13 | + |
| 14 | +# Split the data into features and target |
| 15 | +X = data.drop("Outcome", axis=1) |
| 16 | +y = data["Outcome"] |
| 17 | + |
| 18 | +# Split the data into training and testing sets |
| 19 | +X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) |
| 20 | + |
| 21 | +# Standardize the features (mean=0, std=1) |
| 22 | +scaler = StandardScaler() |
| 23 | +X_train = scaler.fit_transform(X_train) |
| 24 | +X_test = scaler.transform(X_test) |
| 25 | + |
| 26 | +# Create an ANN model with backpropagation |
| 27 | +model = keras.Sequential([ |
| 28 | + layers.Input(shape=(X_train.shape[1],)), |
| 29 | + layers.Dense(64, activation='relu'), |
| 30 | + layers.Dense(32, activation='relu'), |
| 31 | + layers.Dense(1, activation='sigmoid') |
| 32 | +]) |
| 33 | + |
| 34 | +# Compile the model |
| 35 | +model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) |
| 36 | + |
| 37 | +# Train the model |
| 38 | +model.fit(X_train, y_train, epochs=50, batch_size=32, validation_split=0.2) |
| 39 | + |
| 40 | +# Evaluate the model on the test data |
| 41 | +_, accuracy = model.evaluate(X_test, y_test) |
| 42 | +print(f"Accuracy on Pima Indians Diabetes dataset: {accuracy * 100:.2f}%") |
0 commit comments