# Perceptron

The Perceptron is one of the fundamental concepts of Neural Networks. Being based off a single neuron, the perceptron is an algorithm for supervised learning of binary classifiers. It is a type of linear classifier, i.e. a classification algorithm that makes its predictions based on a linear predictor function combining a set of weights with the feature vector. The algorithm allows for online learning, in that it processes elements in the training set one at a time.

My first experience of programming ML concepts was the perceptron, training it to take in two binary inputs and predict the output for AND and OR gates.

#### Python code for Perceptron

TYPE=input("AND/OR")
TYPE=TYPE.upper()
sampleData=[]
if TYPE=="AND":
sampleData=[[0,0,0],[0,1,0],[1,0,0],[1,1,1]]#each item includes input1, input2 and the expected result for an AND gate
elif TYPE=="OR":
sampleData=[[0,0,0],[0,1,1],[1,0,1],[1,1,1]]#each item includes input1, input2 and the expected result for an OR gate
else:
print("Unrecognised value for TYPE.")
exit()
threshold=1
weights=[0,0]#intial weights
learningRate=0.1
changeMade=True
while changeMade==True:
changeMade=False
for item in sampleData:
total=item[0]*weights[0]+item[1]*weights[1]#calculates dot product of two vectors
print("dot product:",total)
print("expected=",item[2])
modifier=0
if total>=threshold and item[2]==0:
modifier=-1 #if dot product>threshold, modifier used to lower weightings
changeMade=True
elif total=threshold:
output=1
else:
output=0
print("input1: "+str(item[0])+" input2: "+str(item[1])+" --> "+str(output)+" (should be "+str(item[2])+")")