Question: How do I improve my Python code and is it correct? If not, can you help me fix it ? Is this the best outcome?
How do I improve my Python code and is it correct? If not, can you help me fix it
Is this the best outcome? I got an which I assume is accuracy. Please only edit steps with the ### YOUR CODE HERE label. 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
### YOUR CODE HERE
# Opens the file in readonly mode
with openfilenamer as file: # Open the file
csvreader readerfile
if skip:
nextcsvreader # Skip header
for row in csvreader:
if not row:
continue
dataset.appendfloatvalue for value in row # Convert values to float
return dataset
Step
Split the dataset into Xtrain, Ytrain, Xtest, Ytest sets.
def traintestsplitdataset split:
### YOUR CODE HERE
# Shuffle and split the dataset
nprandom.shuffledataset
trainsize intsplit lendataset
traindata dataset:trainsize
testdata datasettrainsize:
# Split features X and labels y
Xtrain row: for row in traindata
ytrain row for row in traindata
Xtest row: for row in testdata
ytest row for row in testdata
return Xtrain, ytrain, Xtest, ytest
Step
Defining the Perceptron class that contains the weights, bias, learning rate and 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:
# Step function for perceptron
### YOUR CODE HERE
result if x else # Use a step function
return result
Step
Defining the predict function with the inputs, weights and bias values.
def predictinputs weights, bias:
### YOUR CODE HERE
weightedsum npdotinputs weights bias
return activationfunctionweightedsum
Step
Define the train function.
def trainXtrain, ytrain, learningrate, epochs, weights, bias:
prediction None
error None
for in rangeepochs:
### YOUR CODE HERE
for i in rangelenXtrain:
prediction predictXtraini weights, bias # Calculate prediction
error ytraini prediction # Calculate the error
weights learningrate error nparrayXtraini # Update weights
bias learningrate error # Update bias
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
correct sum for actual, predicted in zipy yhat if actual predicted
accuracy correct leny
return accuracy
Step
Implemented the Perceptron Nerual Network.
# 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 # Use of the data for training and for testing
custombias # Initial bias value
customlearningrate # Learning rate for adjusting weights
customepochs # Number of epochs to train the model
###
# 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
