How to write code for this in python language

Assignment Help Computer Engineering
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?

Reference no: EM133369648

Questions Cloud

How can employees apply their knowledge of human development : How can employees apply their knowledge of human development when working with clients with developmental issues? Provide five examples.
Determine the probability of observing a point pattern : Determine the probability of observing a point pattern with at least this degree of clustering under IRP/CSR. When the VMR is calculated, display a quadrat
Explain how compliance can affect the investigation : List 10 types of performance data that can be used when determining new improvement directions. For each item on your list, explain how the data might
What kind of internet channel is it : HOTL 9760 Niagara College GDS & OTA Discussion What kind of internet channel is it, how do you know that? Expedia.com, itravel2000.com, Priceline.com and Kayak
How to write code for this in python language : Repeat the experiment ten times, each time starting with a different set of randomly initialized weights; store these initial weights for future
Whay do you believe that congressional leadership : Given the previous Lecture Module on Factions, and today's current state of Congress, do you believe that Congressional Leadership adequately represents
Why do astronomers look at night sky in multiple wavelengths : Why do astronomers look at the night sky in multiple wavelengths? What are some difficulties they have to overcome to use they various wavelengths?
List the pros and cons associated with the tool you found : ISSC 262 American Public University Search the internet for a tool used to conduct port scanning and Locate an incident in which the tool was used to exploit
Describe how human rights and principles of social justice : Describe how Human Rights and principles of social justice have informed current understandings and delivery of inclusive education.

Reviews

Write a Review

Computer Engineering Questions & Answers

  Calculate and display the revenue and profit on each item

A program is required to read a series of product sales records from an input file specified by the user.

  Briefly explain the features that need to be constructed

Briefly explain the features that need to be constructed. Note that depending on your problem, the data matrix you use may be different

  Reflect on what types of data they might collect

ITC4311 Columbia Southern University - reflect on what types of data they might collect and how it can be used to benefit their operation

  Is the channel slow or fast fading

Suppose that a car is moving through a suburban environment that has a wireless channel with a coherence time of 10 ms and a coherence bandwidth.

  What constitutes the foundation of internet communication

What constitutes the foundation of Internet communication? What is the role of the IP protocol? What is a protocol stack and why is it layered?

  Create a swing program with a windows look and feel

Create a Swing program with a Windows look and feel to display data about your favorite musical artists.

  Why is the shell called a command interpreter

Why is the shell called a command interpreter? What is the one thing that is common to directories, devices, terminals, and printers?

  Write assembly procedure that returns sum of two integers

CAO201: Computer Architecture & Operating Systems Assessment - Computer Architecture Assignment, Laureate International Universities, U.S.

  What are the common biometric techniques

Some common biometric techniques include: Fingerprint recognition. Select one of these biometric techniques and explain the benefits and the vulnerabilities.

  Using microsoft visio or an open source alternative such as

write a two to three page paper in which you using microsoft visio or an open source alternative such as dia create a

  Discuss the key component of technical communication

When discussing technical and business communications, we generally think of memos, email, presentations, and the like. We do not always immediately think.

  How to use word processing to format a multi-page document

Explore how to use word processing to format a multi-page document complete with headers and/or footers, page numbers, line spacing, etc.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd