Skip to content

Instantly share code, notes, and snippets.

@moratorium08
Created September 6, 2016 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moratorium08/fed0a039dc561165c44c403d51bac650 to your computer and use it in GitHub Desktop.
Save moratorium08/fed0a039dc561165c44c403d51bac650 to your computer and use it in GitHub Desktop.
# 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