# -*- coding: utf-8 -*-
"""Evaluating feed forward deep network for regression using KFold cross  validation

Automatically generated by Colab.

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

import numpy as np
import tensorflow as tf
from sklearn.model_selection import KFold
from sklearn.datasets import make_regression
from sklearn.preprocessing import StandardScaler
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import Adam
from sklearn.metrics import mean_squared_error

X, y = make_regression(n_samples=1000, n_features=10, noise=0.1)

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

def create_model(input_dim):
    model = Sequential()
    model.add(Dense(64, input_dim=input_dim, activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(1))
    model.compile(optimizer=Adam(), loss='mse')
    return model

kf = KFold(n_splits=5, shuffle=True, random_state=42)
fold_no = 1
mse_scores = []
for train_index, val_index in kf.split(X_scaled):


    X_train, X_val = X_scaled[train_index], X_scaled[val_index]
    y_train, y_val = y[train_index], y[val_index]

    model = create_model(input_dim=X_train.shape[1])

    model.fit(X_train, y_train, epochs=50, batch_size=32, verbose=0)

    y_pred = model.predict(X_val)
    mse = mean_squared_error(y_val, y_pred)
    mse_scores.append(mse)

    print(f"Fold {fold_no}, MSE: {mse:.4f}")
    fold_no += 1




print(f"Average MSE across all folds: {np.mean(mse_scores):.4f}")
import matplotlib.pyplot as plt
plt.scatter(y_val, y_pred)
plt.xlabel("Actual Values")
plt.ylabel("Predicted Values")
plt.title("Actual vs Predicted Values")
plt.show()

