Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
# coding:utf-8
import numpy as np
import math
import matplotlib.pyplot as plt
import random
dim = 2
data = [((-0.5,-0.3), -1), ((-0.4,-0.2), -1), ((-0.5,0.3), -1) , ((1.0, 1.0), 1), ((1.0, 0.0), 1), ((-0.5, 0.8), 1)]
def phi(x):
return x
def main():
weight = np.matrix([random.random() for i in range(dim)]).T
ita = 1.0
flag = True
while flag:
flag = False
for i, x in enumerate(data):
p = weight.T.dot(np.matrix(phi(x[0])).T)[0,0] * x[1]
if p > 0:
continue
flag = True
weight = weight + np.matrix(phi(x[0])).T * x[1]
plt.scatter([x[0][0] for x in data if x[1] == 1], [x[0][1] for x in data if x[1] == 1])
plt.scatter([x[0][0] for x in data if x[1] == -1], [x[0][1] for x in data if x[1] == -1], c="red")
x = np.arange(-1,1,0.01)
y = -weight[0,0]/weight[1,0] * x
plt.plot(x,y)
plt.show()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment