# Python Perceptron

This is my finished perceptron written in python. Is there anything that I can improve/suggestions? I'm a beginner with python so anything would be helpful!

import random

class Train:
def __init__(self):
self.train = [[10,5,3,False],[3,2,16,True],[4,15,11,False]]
#Initialize training set
self.fired = False #activate when over thresh
self.threshold = 1.0
self.x1 = 1 #weights
self.x2 = -1
self.x3 = 0.5
self.learningRate = 0.1 #not used
#true if fired state == answers in training set
self.correct = [False,False,False]
self.desire ='' #input value

def trainIt(self):
#while one or more of the answers are wrong
while self.correct == False or self.correct == False or self.correct == False:
for i in range(3):
#Perceptron formula
if self.train[i]*self.x1+self.train[i]*self.x2+self.train[i]*self.x3 < self.threshold:
#under thresh, dont fire
self.fired = False
else:
#fire perceptron
self.fired = True

if self.fired == self.train[i]:
#if fired state is correct, log it
self.correct[i] = True
else:
#my subsitution for the learning rate
self.x1 += random.randrange(-1,2)
self.x2 += random.randrange(-1,2)
self.x3 += random.randrange(-1,2)
#prints weights
print("W1: "+str(self.x1))
print("W2: "+str(self.x2))
print("W3: "+str(self.x3))

self.Test()

def Test(self):
#random point to test perceptron!
self.test = [3,20,8]
#reset some values
self.fired = False
self.correct = [False,False,False]

#fire if over threshold
if self.test*self.x1+self.test*self.x2 < self.threshold:
self.fired = False
else:
self.fired = True

print("Test Result: "+str(self.fired))

• Generally you don't put a capital letter for a function name so I would probably modify def Test(self): to def test(self): – 13ros27 Dec 16 '17 at 15:15