In [2]:
%matplotlib inline
import math,sys,os,numpy as np
from numpy.random import random
from matplotlib import pyplot as plt, rcParams, animation, rc
from __future__ import print_function, division
from ipywidgets import interact, interactive, fixed
from ipywidgets.widgets import *
rc('animation', html='html5')
rcParams['figure.figsize'] = 3, 3
%precision 4
np.set_printoptions(precision=4, linewidth=100)

In [3]:
def lin(a,b,x): return a*x+b

In [4]:
a=3.
b=8.

In [5]:
n=30
x = random(n)
y = lin(a,b,x)

In [6]:
x


Out[6]:
array([ 0.3769,  0.464 ,  0.7885,  0.701 ,  0.8044,  0.9866,  0.1421,  0.8328,  0.1882,  0.7113,
        0.8444,  0.1012,  0.3276,  0.432 ,  0.0421,  0.6596,  0.1021,  0.6102,  0.3445,  0.8364,
        0.6871,  0.3524,  0.6478,  0.7032,  0.5126,  0.1416,  0.0184,  0.571 ,  0.7679,  0.8854])

In [7]:
y


Out[7]:
array([  9.1308,   9.3919,  10.3654,  10.1029,  10.4132,  10.9597,   8.4263,  10.4983,   8.5647,
        10.1339,  10.5332,   8.3037,   8.9828,   9.296 ,   8.1264,   9.9788,   8.3063,   9.8307,
         9.0334,  10.5091,  10.0613,   9.0571,   9.9433,  10.1097,   9.5379,   8.4247,   8.0552,
         9.713 ,  10.3038,  10.6563])

In [8]:
plt.scatter(x,y)


Out[8]:
<matplotlib.collections.PathCollection at 0x114b78bd0>

In [9]:
def sse(y,y_pred): return ((y-y_pred)**2).sum()
def loss(y,a,b,x): return sse(y, lin(a,b,x))
def avg_loss(y,a,b,x): return np.sqrt(loss(y,a,b,x)/n)

In [10]:
a_guess=-1.
b_guess=1.
avg_loss(y, a_guess, b_guess, x)


Out[10]:
9.1480

In [11]:
lr = 0.01

In [ ]: