Question: 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
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
