Skip to content

Instantly share code, notes, and snippets.

@Anton-Cao
Created March 22, 2019 20:04
Show Gist options
  • Save Anton-Cao/eb99f061701c0a294b7a234ccecd40ac to your computer and use it in GitHub Desktop.
Save Anton-Cao/eb99f061701c0a294b7a234ccecd40ac to your computer and use it in GitHub Desktop.
Pseudocode for perceptron algorithm
def perceptron(data, labels):
"""
Pseudo-python for perceptron algorithm
"""
d, n = data.dims # data is d x n array
th = [0] * n # initialize theta to vector of zeros
th_0 = 0
while True:
done = True
for i in range(d):
if labels[i] * (th * data[i] + th_0) <= 0: # check for misclassification
th += labels[i] * data[i]
th_0 += labels[i]
done = False
if done: break # stop when no mistakes are made on entire dataset
return (th, th_0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment