from random import choice from numpy import array, dot, random #Declare activation function def stepFunction(value): if value <= 0: return 0 else: return 1 #Define training data training_data = [ (array([0,0,1]), 0), (array([0,1,1]), 0), (array([1,0,1]), 1), (array([1,1,1]), 1) ] #Define test data test_data = [ (array([0,0,0])), (array([0,1,0])), (array([1,1,1])), (array([0,0,1])), (array([1,0,0])) ] w = random.rand(3) errors = [] eta = 0.2 n = 1000 print("Training Perceptron for {} iterations").format(n) print("Starting weights: {}").format(w) for i in xrange(n): x, expected = choice(training_data) result = dot(w, x) error = expected - stepFunction(result) errors.append(error) w += eta * error * x print ".", print "" print "Training completed" print("Weights after training: {}").format(w) print("Running trained network against test data") for x in test_data: result = dot(x, w) print("{}: {} -> {}").format(x[:3], result, stepFunction(result))