aboutsummaryrefslogtreecommitdiff
path: root/linear_regression/polynomial.py
diff options
context:
space:
mode:
Diffstat (limited to 'linear_regression/polynomial.py')
-rwxr-xr-xlinear_regression/polynomial.py23
1 files changed, 11 insertions, 12 deletions
diff --git a/linear_regression/polynomial.py b/linear_regression/polynomial.py
index a62474a..662014f 100755
--- a/linear_regression/polynomial.py
+++ b/linear_regression/polynomial.py
@@ -27,13 +27,17 @@ def dh3():
return(1/len(x)*np.sum((h(x)-y)*(x**2)))
# Perform the gradient decent
-fig, ax = plt.subplots()
-frame=0 # Current frame (plot animation)
+fig, ax = plt.subplots(dpi=300)
+ax.set_xlim([0, 7])
+ax.set_ylim([0, 5])
+ax.plot(x,y,"ro")
+h_data,=ax.plot(x,h(x))
alpha=0.005 # Proportion of the gradient to take into account
accuracy=0.000001 # Accuracy of the decent
done=False
def decent(i):
- global w1,w2,w3,x,y,frame
+ global w1,w2,w3,x,y
+ skip_frame=0 # Current frame (plot animation)
while True:
w1_old=w1
w1_new=w1-alpha*dh1()
@@ -47,14 +51,9 @@ def decent(i):
if abs(w1_new-w1_old) <= accuracy and abs(w2_new-w2_old) <= accuracy and abs(w2_new-w2_old) <= accuracy:
done=True
- frame+=1
- if frame >=1000:
- frame=0
- ax.clear()
- ax.set_xlim([0, 7])
- ax.set_ylim([0, 5])
- ax.plot(x,y,"ro")
- ax.plot(x,h(x))
+ skip_frame+=1
+ if skip_frame >=1000:
+ h_data.set_ydata(h(x))
break
def IsDone():
@@ -65,5 +64,5 @@ def IsDone():
yield i
anim=FuncAnimation(fig,decent,frames=IsDone,repeat=False)
-anim.save('polynomial.gif',dpi=80,writer="imagemagick")
+anim.save('polynomial.gif',writer="imagemagick",dpi=300)