In [1]:
import os  
import numpy as np  
import pandas as pd  
import matplotlib.pyplot as plt  
%matplotlib inline

Import training data


In [2]:
path = os.getcwd() + '/' + 'ex1data1.txt'  
#data = pd.read_csv(path, header=None, names=['X', 'Y'])

dataraw = {'X':[0.,1.,2.,3.,4.],
          'Y':[1.,3.,7.,13.,21.]}
data = pd.DataFrame(dataraw)
#data.head()
data


Out[2]:
X Y
0 0.0 1.0
1 1.0 3.0
2 2.0 7.0
3 3.0 13.0
4 4.0 21.0

In [3]:
data.describe()


Out[3]:
X Y
count 5.000000 5.000000
mean 2.000000 9.000000
std 1.581139 8.124038
min 0.000000 1.000000
25% 1.000000 3.000000
50% 2.000000 7.000000
75% 3.000000 13.000000
max 4.000000 21.000000

Data Plotting and Visualization


In [4]:
data.plot(kind='scatter', x='X', y='Y', figsize=(12,8))


Out[4]:
<matplotlib.axes._subplots.AxesSubplot at 0x1177d4350>

Implementing 1 Dimensional Linear Regression


In [5]:
def computeCost(X, y, theta):  
    inner = np.power(((X * theta.T) - y), 2)
    return np.sum(inner) / (2 * len(X))

In [6]:
# append a ones column to the front of the data set
data.insert(0, 'Ones', 1)

# set X (training data) and y (target variable)
cols = data.shape[1]  
X = data.iloc[:,0:cols-1]  
y = data.iloc[:,cols-1:cols]

In [7]:
# convert from data frames to numpy matrices
X = np.matrix(X.values)  
y = np.matrix(y.values)  
theta = np.matrix(np.array([0,0]))

In [8]:
X.shape, theta.shape, y.shape


Out[8]:
((5, 2), (1, 2), (5, 1))

In [9]:
computeCost(X, y, theta)


Out[9]:
66.900000000000006

In [10]:
def gradientDescent(X, y, theta, alpha, iters):  
    temp = np.matrix(np.zeros(theta.shape))
    parameters = int(theta.ravel().shape[1])
    cost = np.zeros(iters)
    Wtheta = np.zeros(shape=(iters,2))
    
    for i in range(iters):
        error = (X * theta.T) - y
        for j in range(parameters):
            term = np.multiply(error, X[:,j])
            temp[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(term))

        theta = temp
        cost[i] = computeCost(X, y, theta)
        Wtheta [i,0] = theta[0,0]
        Wtheta [i,1] = theta[0,1]
        #print (theta)     
    return Wtheta, cost

In [11]:
# initialize variables for learning rate and iterations
alpha = 0.01
iters = 1000

# perform gradient descent to "fit" the model parameters
W, cost = gradientDescent(X, y, theta, alpha, iters)

In [12]:
x = np.linspace(data.X.min(), data.X.max(), 10)

In [13]:
fig, ax = plt.subplots(figsize=(12,8)) 
for i in range(iters):
    f = W[i, 0] + (W[i, 1] * x)
    ax.plot(x, f, 'b') 
f = W[iters-1, 0] + (W[iters-1, 1] * x)
ax.plot(x, f,'r') 
ax.scatter(data.X, data.Y, label='Traning Data')  
ax.legend(loc=2)  
ax.set_xlabel('X')  
ax.set_ylabel('Y')  
ax.set_title('Predicted Y vs. X Size')


[ 0.          0.44444444  0.88888889  1.33333333  1.77777778  2.22222222
  2.66666667  3.11111111  3.55555556  4.        ]
[ -0.87654355   1.32643148   3.52940651   5.73238154   7.93535657
  10.1383316   12.34130663  14.54428165  16.74725668  18.95023171]
Out[13]:
<matplotlib.text.Text at 0x11a7e78d0>

In [14]:
fig, ax = plt.subplots(figsize=(12,8))  
ax.plot(np.arange(iters), cost, 'r')  
ax.set_xlabel('Iterations')  
ax.set_ylabel('Cost')  
ax.set_title('Error vs. Training Epoch')


Out[14]:
<matplotlib.text.Text at 0x11c990790>

In [ ]: