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 and with the marking ### YOUR CODE. Thank you.
Step
Import the libraries.
import numpy as np
from csv import reader
from random import seed
from random import randrange
Step
Load the csv file.
def loadcsvfilename skip False:
dataset list
with openfilenamer as file:
csvreader readerfile
if skip:
nextcsvreader # Skip header
for row in csvreader:
if not row:
continue
dataset.appendrow
return dataset
Step
Split the dataset into Xtrain, Ytrain, Xtest, Ytest sets.
def traintestsplitdataset split:
Xtrain, ytrain, Xtest, ytest functions.traintestsplitdataset split
return Xtrain, ytrain, Xtest, ytest
Step
Defining the Perceptron class that contains the weights, bias, learning rate and epochs.
def initself inputsize, bias, learningrate, epochs:
self.weights npzerosinputsize
self.bias bias
self.learningrate learningrate
self.epochs epochs
class Perceptron:
def initself inputsize, bias, learningrate, epochs:
self.weights npzerosinputsize
self.bias bias
self.learningrate learningrate
self.epochs epochs
Step
Define the activation function.
def activationfunctionx:
return functions.activationfunctionx
Step
Defining the predict function with the inputs, weights and bias values.
def predictinputs weights, bias:
###
### YOUR CODE HERE
###
return activationfunctionweightedsum
Step
Define the train function.
def trainXtrain, ytrain, learningrate, epochs, weights, bias:
prediction None
error None
for in rangeepochs:
###
### YOUR CODE HERE
weights, bias functions.trainXtrain, ytrain, learningrate, weights, bias, prediction, error
###
return weights, bias
Step
Define the accuracy for the perceptron.
def perceptronaccuracyy yhat:
# overwrite the accuracy value with your own code
accuracy
###
### YOUR CODE HERE
accuracy functions.perceptronaccuracyy yhat
###
return accuracy
Step
Implemented the Perceptron Nerual Network.
appendprediction
printfInput: Xtesti Predicted: prediction Actual: ytesti
# Test for Accuracy
perceptronaccuracyytest, yhat
# Set the seed
seed
# Load the csv file
filename 'moons.csv
dataset loadcsvfilename skipTrue
# 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
customsplit
# The bias term is a constant value added to the weighted sum of inputs
custombias
# The learning rate controls how much the weights are adjusted during training
customlearningrate
# The number of epochs defines how many times the perceptron will iterate over the training data
customepochs
# Set your values here
###
### YOUR CODE HERE
customsplit, custombias, customlearningrate, customepochs functions.confighyperparameters
###
# Split the dataset for both training and testing
Xtrain, ytrain, Xtest, ytest traintestsplitdataset splitcustomsplit
perceptron Perceptroninputsize biascustombias, learningratecustomlearningrate, epochscustomepochs
# Training
weights, bias trainXtrain, ytrain, perceptron.learningrate, perceptron.epochs, perceptron.weights, perceptron.bias
# Predictions
yhat
# Testing
for i in rangelenXtest:
prediction predictXtesti weights, bias
yhat.appendprediction
printfInput: Xtesti Predicted: prediction Actual: ytesti
# Test for Accuracy
perceptronaccuracyytest, yhat
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
