image_output_1.png
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D

from keras.models import Sequential, Model, load_model

from sklearn.model_selection import GridSearchCV, RandomizedSearchCV

from sklearn.preprocessing import MinMaxScaler

from tensorflow.keras import regularizers

from tensorflow.keras.layers import Conv2D, MaxPool2D, Activation, Dropout, BatchNormalization, LeakyReLU

from tensorflow.keras.layers import Input, Dense, Flatten, GlobalAveragePooling2D, concatenate

from tensorflow.keras.models import Model, Sequential

from tensorflow.keras.optimizers import Adam, RMSprop

from tensorflow.keras.wrappers.scikit_learn import KerasClassifier

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import seaborn as sns

df = pd.read_csv('/kaggle/input/iris/Iris.csv')

df = df.dropna()
df = df.replace([np.inf, -np.inf], np.nan)
df = df.dropna()
df = df.drop(['Id'], axis=1)

scaler = MinMaxScaler()
cols_to_scale = df.columns[:-1]
df_norm = pd.DataFrame(scaler.fit_transform(df[cols_to_scale]), columns=cols_to_scale)
df_norm = pd.concat([df_norm, df.iloc[:, -1]], axis=1)

df = df_norm

df.shape

y_train_ex = to_categorical(y_train_ex)
y_test_ex = to_categorical(y_test_ex)

def create_model(neurons, dropout_rate, kernel_regularizer, learning_rate):
    input_shape = (X_train_ex.shape[1],)
    model = Sequential()
    model.add(Dense(neurons, activation='relu', input_shape=input_shape))
    model.add(Dropout(dropout_rate))
    model.add(Dense(neurons // 2, activation='relu'))
    model.add(Dropout(dropout_rate))
    model.add(Dense(neurons // 4, activation='relu'))
    model.add(Dropout(dropout_rate))
    model.add(Dense(neurons // 8, activation='relu', kernel_regularizer=regularizers.l2(kernel_regularizer)))
    model.add(Dropout(dropout_rate))
    model.add(Dense(3, activation='softmax'))
    opt = Adam(learning_rate=learning_rate)
    model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
    return model
model = KerasClassifier(build_fn=create_model, verbose=0)
neurons = [64, 128, 256, 512, 1024, 2048]
dropout_rate = [0, 0.25, 0.5, 0.75]
kernel_regularizer = [0.01, 0.001, 0.0001]
learning_rate = [0.01, 0.05, 0.001, 0.005, 0.0001, 0.0005]
batch_size = [16, 32, 64]
epochs = [50, 100, 150, 300, 500, 1000]
param_grid = dict(neurons=neurons, dropout_rate=dropout_rate, kernel_regularizer=kernel_regularizer, learning_rate=learning_rate, batch_size=batch_size, epochs=epochs)
n_iter_search = 50
random_search = RandomizedSearchCV(model, param_distributions=param_grid, n_iter=n_iter_search, cv=5, n_jobs=-1, scoring='accuracy')
random_search.fit(X_train_ex, y_train_ex)

neurons = 1024
dropout_rate = 0.5
kernel_regularizer = 0.0001
learning_rate = 0.0001
batch_size = 32
epochs = 1000
input_shape = (X_train_ex.shape[1],)
model = Sequential()
model.add(Dense(neurons, activation='relu', input_shape=input_shape))
model.add(Dropout(dropout_rate))
model.add(Dense(neurons // 2, activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(neurons // 4, activation='relu'))
model.add(Dropout(dropout_rate))
model.add(Dense(neurons // 8, activation='relu', kernel_regularizer=regularizers.l2(kernel_regularizer)))
model.add(Dropout(dropout_rate))
model.add(Dense(3, activation='softmax'))
opt = Adam(learning_rate=learning_rate)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
history = model.fit(X_train_ex, y_train_ex, validation_data=(X_test_ex, y_test_ex), epochs=epochs, batch_size=batch_size)
'\nEpoch 1000/1000\n4/4 [==============================] - 0s 20ms/step - loss: 0.0115 - accuracy: 1.0000 - val_loss: 0.0114 - val_accuracy: 1.0000\n'

y_pred_proba = model.predict(X_test_ex)
y_pred = np.argmax(y_pred_proba, axis=1)

y_test_ex2 = y_test_ex
y_test_ex = np.argmax(y_test_ex, axis=1)

df_y_pred = pd.DataFrame(y_pred, columns=['Predicted'])
df_y_test = pd.DataFrame(list(y_test_ex), columns=['Actual'])
fig, ax = plt.subplots(1, 2, figsize=(15, 5))
sns.countplot(x=df_y_test['Actual'], palette='Set2', ax=ax[0])
sns.countplot(x=df_y_pred['Predicted'], palette='Set2', ax=ax[1])
fig.show()