Linear Regression


In [41]:
# third-party
import matplotlib.pyplot as plot
import numpy
import seaborn

In [42]:
%matplotlib inline

Problem 1

First, an example where the values are perfectly linear.


In [45]:
x_1 = numpy.array([3,6,4,5], dtype=float)
y_1 = numpy.array([0, -3, -1, -2], dtype=float)

In [54]:
expected_w_0 = 3
expected_w_1 = -1
def f(x, w_0, w_1):
    return w_0 + w_1 * x
assert all(f(x_1, expected_w_0, expected_w_1) == y_1)

In [32]:
def weight_1(x, y):
    m = float(len(x))
    return (m * (x * y).sum() - (x.sum() * y.sum()))/(m * (x**2).sum() - x.sum()**2)

In [47]:
w_1 = weight_1(x_1, y_1)
assert w_1 == expected_w_1

In [37]:
def weight_0(x, y, w_1):
    m = float(len(x))
    return y.sum()/m - (w_1/m) * x.sum()

In [48]:
w_0 = weight_0(x_1, y_1, w_1)
assert expected_w_0 == w_0, "Expected: {0} Actual: {1}".format(expected_w_0,
                                                               w_0)

In [61]:
figure = plot.figure()
axe = figure.gca()
exes = numpy.arange(x_1.min(), x_1.max() + 1)
line = axe.plot(exes, f(exes, w_0, w_1), color='firebrick')
line = axe.scatter(x_1, y_1)


<matplotlib.figure.Figure at 0x7f7faf90e510>

Given x, y, calculate the weights for a linear regression.


In [62]:
x_2 = numpy.array([2, 4, 6, 8])
y_2 = numpy.array([2, 5, 5, 8])

w1_2 = weight_1(x_2, y_2)
w0_2 = weight_0(x_2, y_2, w1_2)

In [65]:
print(w0_2)
print(w1_2)


0.5
0.9

In [66]:
figure = plot.figure()
axe = figure.gca()
exes = numpy.arange(0, x_2.max() + 1)
line = axe.plot(exes, f(exes, w0_2, w1_2), color='firebrick')
line = axe.scatter(x_2, y_2)


<matplotlib.figure.Figure at 0x7f7fafb14310>