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

Automatically generated by Colab.

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

import numpy as np
from sklearn.model_selection import KFold
from sklearn.datasets import load_iris
from sklearn.preprocessing import LabelEncoder
from keras.models import Sequential
from keras.layers import Dense
from keras.utils import to_categorical
from sklearn.metrics import accuracy_score
data = load_iris()
X = data.data
y = data.target
y = to_categorical(y, num_classes=3)
kf = KFold(n_splits=5, shuffle=True, random_state=42)
fold_accuracies = []
for train_index, test_index in kf.split(X):
    X_train, X_test = X[train_index], X[test_index]
    y_train, y_test = y[train_index], y[test_index]

    model = Sequential()
    model.add(Dense(64, input_dim=X_train.shape[1], activation='relu'))
    model.add(Dense(32, activation='relu'))
    model.add(Dense(3, activation='softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

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

    y_pred = model.predict(X_test)
    y_pred_classes = np.argmax(y_pred, axis=1)
    y_true = np.argmax(y_test, axis=1)

    accuracy = accuracy_score(y_true, y_pred_classes)
    fold_accuracies.append(accuracy)


average_accuracy = np.mean(fold_accuracies)
print(fold_accuracies)
print(f'Average Accuracy: {average_accuracy:.4f}')

