Question: Can you help me with my finished code? It is not working as of right now. Please only edit steps 2 , 3 , 4

Can you help me with my finished code? It is not working as of right now. Please only edit steps 2,3,4,6,7,8 and 9 with the marking ### YOUR CODE. Thank you.
Step 1
Import the libraries.
import numpy as np
from csv import reader
from random import seed
from random import randrange
Step 2
Load the csv file.
def load_csv(filename, skip = False):
dataset = list()
with open(filename,'r') as file:
csv_reader = reader(file)
if skip:
next(csv_reader) # Skip header
for row in csv_reader:
if not row:
continue
dataset.append(row)
return dataset
Step 3
Split the dataset into X_train, Y_train, X_test, Y_test sets.
def train_test_split(dataset, split):
X_train, y_train, X_test, y_test = functions.train_test_split(dataset, split)
return X_train, y_train, X_test, y_test
Step 4
Defining the Perceptron class that contains the weights, bias, learning rate and epochs.
def __init__(self, input_size, bias, learning_rate, epochs):
self.weights = np.zeros(input_size)
self.bias = bias
self.learning_rate = learning_rate
self.epochs = epochs
class Perceptron:
def __init__(self, input_size, bias, learning_rate, epochs):
self.weights = np.zeros(input_size)
self.bias = bias
self.learning_rate = learning_rate
self.epochs = epochs
Step 5
Define the activation function.
def activation_function(x):
return functions.activation_function(x)
Step 6
Defining the predict function with the inputs, weights and bias values.
def predict(inputs, weights, bias):
###
### YOUR CODE HERE
###
return activation_function(weighted_sum)
Step 7
Define the train function.
def train(X_train, y_train, learning_rate, epochs, weights, bias):
prediction = None
error = None
for _ in range(epochs):
###
### YOUR CODE HERE
weights, bias = functions.train(X_train, y_train, learning_rate, weights, bias, prediction, error)
###
return weights, bias
Step 8
Define the accuracy for the perceptron.
def perceptron_accuracy(y, y_hat):
# overwrite the accuracy value with your own code
accuracy =0
###
### YOUR CODE HERE
accuracy = functions.perceptron_accuracy(y, y_hat)
###
return accuracy
Step 9
Implemented the Perceptron Nerual Network.
append(prediction)
print(f"Input: {X_test[i]}, Predicted: {prediction}, Actual: {y_test[i]}")
# Test for Accuracy
perceptron_accuracy(y_test, y_hat)
# Set the seed
seed(1)
# Load the csv file
filename = 'moons.csv'
dataset = load_csv(filename, skip=True)
# Configure the perception with the bias, learning rate and epochs
# Note the initial values are dummy and must changed for an accurate network
# The split value for the training and test sets
custom_split =0
# The bias term is a constant value added to the weighted sum of inputs
custom_bias =-1
# The learning rate controls how much the weights are adjusted during training
custom_learning_rate =-1
# The number of epochs defines how many times the perceptron will iterate over the training data
custom_epochs =-1
# Set your values here
###
### YOUR CODE HERE
custom_split, custom_bias, custom_learning_rate, custom_epochs = functions.config_hyperparameters()
###
# Split the dataset for both training and testing
X_train, y_train, X_test, y_test = train_test_split(dataset, split=custom_split)
perceptron = Perceptron(input_size=2, bias=custom_bias, learning_rate=custom_learning_rate, epochs=custom_epochs)
# Training
weights, bias = train(X_train, y_train, perceptron.learning_rate, perceptron.epochs, perceptron.weights, perceptron.bias)
# Predictions
y_hat =[]
# Testing
for i in range(len(X_test)):
prediction = predict(X_test[i], weights, bias)
y_hat.append(prediction)
print(f"Input: {X_test[i]}, Predicted: {prediction}, Actual: {y_test[i]}")
# Test for Accuracy
perceptron_accuracy(y_test, y_hat)
Can you help me with my finished code? It is not

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!