Linear Fitting using Gradient Descent Algorithm

In [55]:
# y = mx + b
# m is slope, b is y-intercept
def computeErrorForLineGivenPoints(b, m, points):
    totalError = 0
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        totalError += (y - (m * x + b)) ** 2
    return totalError / float(len(points))

def stepGradient(b_current, m_current, points, learningRate):
    b_gradient = 0
    m_gradient = 0
    N = float(len(points))
    for i in range(0, len(points)):
        x = points[i, 0]
        y = points[i, 1]
        b_gradient += -(2/N) * (y - ((m_current * x) + b_current))
        m_gradient += -(2/N) * x * (y - ((m_current * x) + b_current))
    new_b = b_current - (learningRate * b_gradient)
    new_m = m_current - (learningRate * m_gradient)
    return [new_b, new_m]

import random
from numpy.random import randn
from numpy import *
import pandas as pd
import matplotlib.pyplot as plt
random.seed(100)

n = 100
x = randn(n) * 10 + 50
y = x * 1.5 + randn(n) * 10
points = array([x, y]).transpose()

b = 0
m = 0
for i in range(100):
    b, m = stepGradient(b, m, array(points), 0.02)
    if i % 10 == 0:
        print b, m, computeErrorForLineGivenPoints(b, m, points)
# plt.scatter(x, y)
# plt.plot(arange(0,100), m*arange(0,100)+b)
# plt.show()
 2.90940202215 147.414059472 53091441.0482
2.51063478e+20 1.27737150853e+22 4.06758732977e+47
2.19755719833e+40 1.1180825566e+42 3.11637835829e+87
1.92352056874e+60 9.7865702736e+61 2.38761046405e+127
1.68365646235e+80 8.56617940729e+81 1.8292656002e+167
1.47370354614e+100 7.4979719745e+101 1.40149018714e+207
1.28993187772e+120 6.56297061471e+121 1.07375044086e+247
1.12907664062e+140 5.74456445504e+141 8.22652930299e+286
9.88280142863e+159 5.02821400786e+161 inf
8.65041048269e+179 4.40119286792e+181 inf
In [ ]: