# -*- coding: utf-8 -*-
"""Implementing regularization to avoid overfitting in binary classification

Automatically generated by Colab.

Original file is located at
    https://colab.research.google.com/drive/1Cef6TY4UreI4bjAEovxQ6zb6GHHvCzAr
"""

import numpy as np
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import regularizers
# Generate synthetic binary classification data
X, y = make_classification(n_samples=1000, n_features=20,
n_informative=15,
                           n_redundant=5, random_state=42)
# Split into train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, random_state=42)
# Build neural network with L2 regularization
model = Sequential([
    Dense(32, activation='relu', input_shape=(20,),
kernel_regularizer=regularizers.l2(0.01)),
    Dense(16, activation='relu',
kernel_regularizer=regularizers.l2(0.01)),
    Dense(1, activation='sigmoid')
])
# Compile model
model.compile(optimizer='adam', loss='binary_crossentropy',
metrics=['accuracy'])
# Train model
model.fit(X_train, y_train, epochs=50, batch_size=32,
validation_split=0.2, verbose=1)
# Evaluate on test data
loss, accuracy = model.evaluate(X_test, y_test)
print("Test Accuracy:", accuracy)
# Make predictions
predictions = model.predict(X_test)
print("Predictions (first 10 samples):",
np.round(predictions[:10]).flatten())











#WITH diabetes -PAT DATA 

import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras import regularizers
from tensorflow.keras.optimizers import Adam
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt

df = pd.read_csv('/content/diabetes.csv')
print(df.head())

X = df.drop(columns=['Outcome'])
y = df['Outcome']
X.fillna(X.mean(), inplace=True)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)

model = Sequential()
model.add(tf.keras.Input(shape=(X_train.shape[1],)))
model.add(Dense(64, activation='relu'))
model.add(Dense(32, activation='relu', kernel_regularizer=regularizers.l1(0.01)))
model.add(Dense(16, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])
l1 = model.fit(X_train, y_train, epochs=20, batch_size=32, validation_data=(X_test, y_test))

loss, accuracy = model.evaluate(X_test, y_test)
print(f"Test Loss: {loss}")
print(f"Test Accuracy: {accuracy}")

y_pred = (model.predict(X_test) > 0.6).astype(int)
print(f"Manual Accuracy: {accuracy_score(y_test, y_pred)}")
print(f"Predicted Labels: {y_pred[:10].flatten()}")
print(f"True Labels: {y_test.values[:10]}")

print("L1 Test Accuracy:", model.evaluate(X_test, y_test)[1])

plt.figure(figsize=(12,5))
plt.subplot(1,2,1)
plt.plot(l1.history['accuracy'], label='L1 Train')
plt.plot(l1.history['val_accuracy'], label='L1 Val')
plt.title('Accuracy Comparison')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()

plt.subplot(1,2,2)
plt.plot(l1.history['loss'], label='L1 Train')
plt.plot(l1.history['val_loss'], label='L1 Val')
plt.title('Loss Comparison')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()


