Skip to content

Instantly share code, notes, and snippets.

@motivic
Last active October 31, 2016 18:05
Show Gist options
  • Select an option

  • Save motivic/80b399c7735bafe1da2ab21df978d0ea to your computer and use it in GitHub Desktop.

Select an option

Save motivic/80b399c7735bafe1da2ab21df978d0ea to your computer and use it in GitHub Desktop.
Learning from Data: Perceptron Exercise
import numpy as np
# Find f(x), where f is the line through p1 and p2
def f(p1, p2, x):
return (p2[1]-p1[1])/(p2[0]-p1[0])*(x-p1[0])+p1[1]
# Get random points p1 and p2
p1, p2 = np.random.rand(2, 2)
line = lambda x: f(p1, p2, x)
def label(p):
return np.sign(p[1] - line(p[0]))
# Get number of training sample points to define
n_train = int(input("Input the size of the training set\n"))
def run_sample():
w = np.array([0.0, 0.0, 0.0])
train_sample = np.random.rand(n_train, 2)
n_label = np.array([label(p) for p in train_sample])
misclassified = list(range(n_train))
num_iters = 0
while misclassified:
num_iters += 1
np.random.shuffle(misclassified)
i = misclassified[0]
w[0] += n_label[i]*train_sample[i][0]
w[1] += n_label[i]*train_sample[i][1]
w[2] += n_label[i]
temp = []
for i in range(n_train):
if n_label[i] != np.sign(w[0]*train_sample[i][0]+w[1]*train_sample[i][1]+w[2]):
temp.append(i)
misclassified = temp
return num_iters, w
iters = []
num_wrong = 0
for _ in range(1000):
n, w = run_sample()
iters.append(n)
p = np.random.rand(2)
if label(p) != np.sign(w[0]*p[0]+w[1]*p[1]+w[2]):
num_wrong += 1
print(np.average(iters))
print(num_wrong)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment