1 import numpy as np 4 class Perceptron (object): """Perceptron classifier. 6 7 Parameters 8 9 eta : float 10 Learning r
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am
1 import numpy as np 4 class Perceptron (object): """Perceptron classifier. 6 7 Parameters 8 9 eta : float 10 Learning r
class Perceptron(object):
"""Perceptron classifier.
Parameters
------------
eta : float
Learning rate (between 0.0 and 1.0)
n_iter : int
Passes over the training dataset.
random_state : int
Random number generator seed for random weight
initialization.
Attributes
-----------
w_ : 1d-array
Weights after fitting.
errors_ : list
Number of misclassifications (updates) in each epoch.
"""
def __init__(self, eta=0.01, n_iter=50, random_state=1):
self.eta = eta
self.n_iter = n_iter
self.random_state = random_state
def fit(self, X, y):
"""Fit training data.
Parameters
----------
X : {array-like}, shape = [n_examples, n_features]
Training vectors, where n_examples is the number of examples
and
n_features is the number of features.
y : array-like, shape = [n_examples]
Target values.
Returns
-------
self : object
"""
rgen = np.random.RandomState(self.random_state)
self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 +
X.shape[1])
self.errors_ = []
for _ in range(self.n_iter):
errors = 0
for xi, target in zip(X, y):
update = self.eta * (target - self.predict(xi))
self.w_[1:] += update * xi
self.w_[0] += update
errors += int(update != 0.0)
self.errors_.append(errors)
# my do-nothing code
IK = 2020
# my do-nothing code
return self
def net_input(self, X):
"""Calculate net input"""
return np.dot(X, self.w_[1:]) + self.w_[0]
def predict(self, X):
"""Return class label after unit step"""
return np.where(self.net_input(X) >= 0.0, 1, -1)
ppn = Perceptron(eta=0.0001, n_iter=20, random_state=1)
ppn.fit(X, y)
plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_,
marker='o')
plt.xlabel('Epochs')
plt.ylabel('Number of updates')
plt.show()
(i) Find the largest value of 𝜂 for which the process
takes more than 20 iterations to converge. Explain how you found
that 𝜂
(ii) Are you able to find 𝜂>1 for which the process
fails to converge in less than 30 iterations?
(iii) Find two different settings for the random state, that give
different convergence patterns, for the same 𝜂.
1 import numpy as np 4 class Perceptron (object): """Perceptron classifier. 6 7 Parameters 8 9 eta : float 10 Learning rate (between 0.0 and 1.0) n_iter : int 11 12 Passes over the training dataset. 13 random_state : int 14 Random number generator seed for random weight initialization. 15 16 17 Attributes 18 19 w_ : 1d-array 20 Weights after fitting. 21 errors_ : list 22 Number of misclassifications (updates) in each epoch. 23 24 25 def __init__(self, eta=0.01, n_iter=50, random_state=1): 26 self.eta = eta 27 self.n_iter = n_iter 28 self.random_state random_state □23 45
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 def fit(self, X, y): """Fit training data. Parameters X : {array-like}, shape [n_examples, n_features] Training vectors, where n_examples is the number of examples and n_features is the number of features. y : array-like, shape = [n_examples] Target values. Returns self: object |||||| rgen = np. random. RandomState(self.random_state) self.w_ = rgen.normal(loc=0.0, scale=0.01, size=1 + X.shape [1]) self.errors_ = [] for in range(self.n_iter): errors = 0 for xi, target in zip(X, y): update = self.eta * (target - self.predict(xi)) self.w_[1:] += update * xi self.w_[0] += update errors += int(update != 0.0) self.errors_.append(errors)
59 # my do-nothing code IK = 2020 60 61 # my do-nothing code 62 63 return self 64 65 def net_input (self, X): 66 """"Calculate net input""" 67 return np.dot(X, self.w_[1:]) + self.w_ [0] 68 69 def predict (self, X): 70 """Return class label after unit step""""" 71 return np.where(self.net_input (X) >= 0.0, 1, -1) Work on the above cell and modify the code so that: (i) The fit function stops when no more iterations are necessary. (ii) The trained perceptron contains as an attribute not only its weights, but also the number of iterations it took for training (iii) The perceptron maintains a history of its weights, i.e. the set of weights after each point is processed. To modify the code please insert your code with clear comments surrounding it, similarly to "my do-nothing code". Make sure you evaluate the cell again, so that following cells will be using the modified perceptron.
1 2 ppn = Perceptron (eta=0.0001, n_iter=20, random_state=1) 3 4 ppn.fit(x, y) 5 6 plt.plot(range(1, len(ppn.errors_) + 1), ppn.errors_, marker='0') 7 plt.xlabel('Epochs') 8 plt.ylabel('Number of updates') 9 10 11 plt.show() 12
5 www 1 0 2.5 5.0 7.5 10.0 12.5 15.0 17.5 20.0 Epochs Running the above code, you can verify if your modification in question 1 works correctly. The point of this question is to experiment with the different hyperparameters. Here are some specific questions: (i) Find the largest value of n for which the process takes more than 20 iterations to converge. Explain how you found that (ii) Are you able to find n > 1 for which the process fails to converge in less than 30 iterations? (iii) Find two different settings for the random state, that give different convergence patterns, for the same n. Please give your answers in the cell below. Number of updates N