0

Here is an implementation for Perceptron

class Perceptron:
    def __init__(self, eta=.1, n_iter=10, model_w=[.0, .0], model_b=.0):
        self.eta = eta
        self.n_iter = n_iter
        self.model_w = model_w
        self.model_b = model_b
def predict(self, x):
    if np.dot(self.model_w, x) + self.model_b >= 0:
        return 1
    else:
        return -1

def update_weights(self, idx, model_w, model_b):
    w = model_w
    b = model_b
    w += self.eta * y_train[idx] * x_train[idx]
    b += self.eta * y_train[idx]
    return w, b

def fit(self, x, y):
    if len(x) != len(y):
        print('error')
        return False
    for i in range(self.n_iter):
        for idx in range(len(x)):
            if y[idx] != self.predict(x[idx]):
                self.model_w, self.model_b = self.update_weights(idx,
                                        self.model_w, self.model_b)

Does this code

Perceptron(eta=.1, n_iter=10)

mean the model trains 10 epochs?

JJJohn
  • 221
  • 2
  • 9
  • Questions about programming issues are generally off-topic here. It seems to me that this is one of those questions. Please, take a look at https://ai.stackexchange.com/help/on-topic for more details about our scope. – nbro Jul 10 '21 at 22:29
  • @nbro Thanks for the comments. This is not about programming issues. The code works well. This is about terminology. The code is an easy-to-understand example of the algorithm. – JJJohn Jul 11 '21 at 01:14
  • 1
    yes that's 10 epochs, for idx makes 1 epoch, for i makes 10 epochs – Dan D. Jul 13 '21 at 11:00

0 Answers0