title: Having Problems With Implementing the Perceptron Algorithm in Python
body: <p>I'm having a problem debugging the following code, for some reason The perceptron stops updating itself after a couple of steps with random values as the weights. I have tried not using a class for my work and edited everything to the bare minimum, but still had the same problem. I have also checked the Perceptron.train(), and it works just fine. So, I'm guessing the main problem is with the train function itself. I am kind of new to python programming so any help would be apreciated guys.
      import random
      import Plot as plt
      import numpy as np</p>
  
  <pre><code>#-----Function Of the line that seperates the two different Data Types-----$
  def f(x):
      return x
  
  #-----Activation Function-----#
  def act(x):
      if x &gt;= 0:
          return 1.0
      return 0.0
  
  class Point:
  
      def __init__(self, x, y):
          self.X = x
          self.Y = y
          if y &gt; f(x):
              self.Target = 1.0
          else:
              self.Target = 0.0
  
  
  class Perceptron:
  
      def __init__(self, n, actFunc = act, lr = 0.2):
          self.Weights = [0 for i in range(n)]
          self.ActFunc = actFunc
          self.LR = lr
  
      def guess(self, inputs):
          valSum = 0
          for i in range(len(inputs)):
              valSum += self.Weights[i] * inputs[i]
          return self.ActFunc(valSum)
  
      def train(self, inputs, target):
          cal = self.guess(inputs)
          err = target - cal
          for i in range(0, len(self.Weights)):
              self.Weights[i] += self.LR * err * inputs[i]
  
      def printWeights(self):
          for i in range(len(self.Weights)):
              print("WEIGHT[" + str(i) + "] = " + str(self.Weights[i]))
          print("")
  
      def lineFunc(self):
          # y = w0 + w1x + w2y
          # (1 - w2)y = w0 + w1x
          # y = w0/(1-w2) + w1/(1 - w2)x
          w0 = self.Weights[0]
          w1 = self.Weights[1]
          w2 = self.Weights[2]
          return (str(w0/(1 - w2)) + " + " + str(w1/(1 - w2)) + " * x")
  
  #-----INITIALISING DATA------#
  brain = Perceptron(3)
  
  n = 20
  points = [Point(random.uniform(-10, 10), random.uniform(-10, 10)) for x in range(n)]
  
  t = 1000
  
  #-----Training-----#
  for i in range(t):
      point = points[random.randrange(0, n)]
      brain.train([1, point.X, point.Y], point.Target)
      brain.printWeights()
      print(brain.lineFunc())
  </code></pre>