Created
February 23, 2024 19:54
-
-
Save isedgar/2e1386e88a233389586257d87f78b95e to your computer and use it in GitHub Desktop.
An example of the perceptron algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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