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

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

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