Reference no: EM133369648
Selected a 2-class classification problem with the "Wine quality" dataset. I have Balanced the data, so that you have equal numbers of data points from each class, e.g., by duplicating randomly chosen members of the minority class and adding a little random noise. I have Use 70% of the data for training, and 30% for testing, ensuring that both sets are balanced. I have Trained a shallow feedforward neural network (with sigmoidal node functions and one hidden layer with twice as many nodes as the input dimensionality) using back-propagation with ADAM optimizer. Following is the code for the same:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import tensorflow as tf
import matplotlib.pyplot as plt
# Load and preprocess the wine quality data
df = pd.read_csv("winequality.csv")
df["quality"] = df["quality"].apply(lambda x: 1 if x >= 6 else 0)
df = df.sample(frac=1).reset_index(drop=True)
# Split the data into training and testing sets
train_df, test_df = train_test_split(df, test_size=0.3)
# Balance the data by duplicating randomly chosen members of the minority class
class_0 = train_df[train_df["quality"] == 0]
class_1 = train_df[train_df["quality"] == 1]
if len(class_0) > len(class_1):
class_0 = class_0.sample(len(class_1), replace=True)
else:
class_1 = class_1.sample(len(class_0), replace=True)
train_df = pd.concat([class_0, class_1])
# Normalize the data
scaler = MinMaxScaler()
train_data = scaler.fit_transform(train_df.drop("quality", axis=1).values)
test_data = scaler.transform(test_df.drop("quality", axis=1).values)
train_labels = train_df["quality"].values
test_labels = test_df["quality"].values
# Define the neural network architecture
input_dim = train_data.shape[1]
hidden_dim = 2 * input_dim
model = tf.keras.Sequential([
tf.keras.layers.Dense(hidden_dim, activation='sigmoid', input_shape=(input_dim,)),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
# Train the model using back-propagation with ADAM optimizer
history = model.fit(train_data, train_labels, epochs=100, batch_size=64, validation_data=(test_data, test_labels))
# Plot the training and testing accuracy over time
plt.plot(history.history['accuracy'], label='training accuracy')
plt.plot(history.history['val_accuracy'], label='testing accuracy')
plt.legend()
plt.show()
# Evaluate the model on the test data
test_loss, test_acc = model.evaluate(test_data, test_labels)
print("Test loss:", test_loss)
print("Test accuracy:", test_acc)
Now I want to Repeat the experiment ten times, each time starting with a different set of randomly initialized weights; store these initial weights for future and Show the confusion matrices for it(for training data and test data). How to add code for this in the above code?
how to write code for this in PYTHON language?