<jupyter_start><jupyter_text>Overfitting vs Underfitting Dropout neurons help with overfitting and avoid bias  Preprocessing Data<jupyter_code>import warnings
import numpy as np
import pandas as pd
import seaborn as sn
import tensorflow as tf
from tensorflow import keras
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix, classification_report

warnings.filterwarnings("ignore")

df = pd.read_csv("11_Sonar_dataset.csv", header=None) # this file doesn't have headers or column names

print(df.shape)
print(df.columns)
print(df[60].value_counts())
df.sample(5)
X = df.drop(60, axis="columns")
y = df[60]

y = pd.get_dummies(y, drop_first=True) # apply one hot encoding
y.value_counts()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
X_train.shape<jupyter_output><empty_output><jupyter_text>Overfitting Model<jupyter_code>model = keras.Sequential([
    keras.layers.Dense(60, input_shape=(60, ), activation="relu"),
    keras.layers.Dense(30, activation="relu"),
    keras.layers.Dense(15, activation="relu"),
    keras.layers.Dense(1, activation="sigmoid")
])
model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

model.fit(X_train, y_train, epochs=100, batch_size=8) # mini_batch method
model.evaluate(X_test, y_test)
y_pred = model.predict(X_test).reshape(-1)
y_pred = np.round(y_pred)
print("pred, actual", "\n", np.c_[y_pred, y_test][:10])
print(classification_report(y_test, y_pred))<jupyter_output>precision    recall  f1-score   support

           0       0.70      0.80      0.74        20
           1       0.79      0.68      0.73        22

    accuracy                           0.74        42
   macro avg       0.74      0.74      0.74        42
weighted avg       0.74      0.74      0.74        42<jupyter_text>Model with Dropout Layer<jupyter_code>model_new = keras.Sequential([
    keras.layers.Dense(60, input_shape=(60, ), activation="relu"),
    keras.layers.Dropout(0.5),
    keras.layers.Dense(30, activation="relu"),
    keras.layers.Dropout(0.5),
    keras.layers.Dense(15, activation="relu"),
    keras.layers.Dropout(0.5),
    keras.layers.Dense(1, activation="sigmoid")
])
model_new.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

model_new.fit(X_train, y_train, epochs=100, batch_size=8)
model_new.evaluate(X_test, y_test)
y_pred_new = model_new.predict(X_test).reshape(-1)
y_pred_new = np.round(y_pred_new)
print(classification_report(y_test, y_pred_new))<jupyter_output>precision    recall  f1-score   support

           0       0.76      0.80      0.78        20
           1       0.81      0.77      0.79        22

    accuracy                           0.79        42
   macro avg       0.79      0.79      0.79        42
weighted avg       0.79      0.79      0.79        42
{'fraction_non_alphanumeric': 0.11679336558396683, 'fraction_numerical': 0.0525224602626123, 'mean_word_length': 3.232456140350877, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '569042', 'license': 'MIT', 'n_tokens_mistral': 1094, 'n_tokens_neox': 1020, 'n_words': 205, 'path': 'Python Tutorial Tensorflow/11_Dropout_regularization.ipynb', 'repo_name': 'PaulPan00/donkey_wrapper', 'size': '2894'}