Skip to content

Instantly share code, notes, and snippets.

@zinark
Created March 20, 2018 10:23
Show Gist options
  • Select an option

  • Save zinark/249719907e550ffc4829a6b8ef8469cb to your computer and use it in GitHub Desktop.

Select an option

Save zinark/249719907e550ffc4829a6b8ef8469cb to your computer and use it in GitHub Desktop.
convergence
import math
def sign (x):
if x > 0: return 1
if x < 0: return -1
return 0
def is_misclassified (x, w, b, y):
val = x[0]*w[0] + x[1]*w[1] + b
print ("x=", x, "w=", w, "b=", b, "y=", y, "mis_classification=", sign(y) != sign(val))
if sign(y) != sign(val): return True
return False
def update (x, w, b, y):
new_w = [
w[0] + x[0] * y,
w[1] + x[1] * y
]
new_b = b + y
print ("updated! w and b ", new_w, new_b)
return new_w, new_b
b=0
w=[0, 0]
k=0
xs = [
[-1,1],
[0,-1],
[10,1]
]
ys = [1,-1, 1]
for s in range(50):
print ("loooooop_____", s)
for i in range(3):
x = xs[i]
y = ys[i]
#print ("i=",i, x, y)
is_mc = is_misclassified (x, w, b, y)
if is_mc:
w,b = update (x, w, b, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment