Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import numpy as np class Perceptron ( object ) : Perceptron classifier. Parameters - - - - - - - - -
import numpy as np
class Perceptronobject:
Perceptron classifier.
Parameters
eta : float
Learning rate between and
niter : int
Passes over the training dataset.
randomstate : int
Random number generator seed for random weight
initialization.
Attributes
w : darray
Weights after fitting.
errors : list
Number of misclassifications updates in each epoch.
def initself eta niter randomstate:
self.eta eta
self.niter niter
self.randomstate randomstate
def fitself X y:
Fit training data.
Parameters
X : arraylike shape nexamples, nfeatures
Training vectors, where nexamples is the number of examples and
nfeatures is the number of features.
y : arraylike, shape nexamples
Target values.
Returns
self : object
rgen nprandom.RandomStateselfrandomstate
self.w rgen.normalloc scale size Xshape
self.errors
for in rangeselfniter:
errors
for xi target in zipX y:
update self.eta selfpredictxi target
self.w: update xi
self.w update
errors intupdate
self.errorsappenderrors
###### New code for doing nothing. MEH
thiscodedoesnothing True
######
return self
def netinputself X:
Calculate net input"""
return npdotX self.w: self.w
def predictself X:
Return class label after unit step"""
return npwhereselfnetinputX
There is a significant error in the above perceptron implementation. Work on the above cell and modify the code so that:
i The line containing the error is commented out, and a new line is added with corrected code.
ii The fit function stops when no more iterations are necessary.
iii The trained perceptron contains as an attribute not only its weights, but also the number of iterations it took for training.
iv The perceptron maintains a history of its weights, ie the set of weights after each point is processed.
At each place where you have modified the code, please add clear comments surrounding it similarly to the donothing" code. Make sure you evaluate the cell again, so that following cells will be using the modified perceptron.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started