Skip to content

Instantly share code, notes, and snippets.

@isedgar
Created February 23, 2024 19:54
Show Gist options
  • Save isedgar/2e1386e88a233389586257d87f78b95e to your computer and use it in GitHub Desktop.
Save isedgar/2e1386e88a233389586257d87f78b95e to your computer and use it in GitHub Desktop.
An example of the perceptron algorithm
samples = [[1,2], [2,0], [3,1], [2,2]]
labels = [1,1,-1,-1]
def dot(a, b):
return sum([a[j]*b[j] for j in range(len(a))])
w, b = [0,0], 0
for n in range(100):
n_updates = 0
for i in range(len(labels)):
x, y = samples[i], labels[i]
if y*(dot(w, x) + b) <= 0:
w = [w[j] + y*x[j] for j in range(len(w))]
b = b + y
n_updates = n_updates + 1
if n_updates == 0: break
print(w, b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment