import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation, rc
from IPython.display import HTML
# get_ipython().magic('matplotlib inline')
x = np.array([1.5,2,1,3.5,3,4,3,1,2,4,3,2,1,1,1,1,1,1]).reshape(3,6).T
y = np.array([0,0,0,1,1,1])
size = x.shape
data = []
fig, ax = plt.subplots()
ax.scatter(x[:, 0], x[:, 1])
ax.set_xlim(( -5, 5))
ax.set_ylim(( -5, 5))
line, = ax.plot([], [], lw=2)
def init():
line.set_data([], [])
return (line,)
def animate(i):
line.set_data(data[i][0], data[i][1])
return (line,)
w = np.random.randn(3)
# w = np.array([.5, -.1, .2])
w_prev = np.zeros(3)
a = 0.1
# print(x,y,w)
for n in range(100):
if sum(w - w_prev) == 0:
break
w_prev = w
for i in range(size[0]):
pred = np.dot(x[i,:], w)
# print(pred)
if pred > 0:
pred = 1
else:
pred = 0
# print(pred)
w = w + a * (y[i] - pred) * x[i,:]
# print(w)
x1 = np.arange(-5, 5)
y1 = (-w[0] * x1 - w[2]) / w[1]
data.append((x1, y1))
# plt.cla()
# ax.plot(x1, y1)
# ax.draw()
# plt.pause(.1)
# display(fig)
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=len(data))
HTML(anim.to_html5_video())