Skip to content

Instantly share code, notes, and snippets.

@chokkan
Created October 10, 2012 13:34
Show Gist options
  • Save chokkan/3865688 to your computer and use it in GitHub Desktop.
Save chokkan/3865688 to your computer and use it in GitHub Desktop.
Perceptron
def update(w, x, y):
a = 0.
for i in range(len(w)):
a += w[i] * x[i]
if a * y <= 0:
for i in range(len(w)):
w[i] += y * x[i]
def classify(w, x):
a = 0.
for i in range(len(w)):
a += w[i] * x[i]
return (0. < a)
if __name__ == '__main__':
w = [0.] * 9
D = (
((1, 1, 1, 1, 1, 1, 0, 0, 0), +1),
((1, 0, 0, 0, 1, 1, 1, 1, 1), -1),
)
update(w, D[0][0], D[0][1])
update(w, D[1][0], D[1][1])
print classify(w, D[0][0])
print classify(w, D[1][0])
print w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment