Gradient Descent For Linear Regression (Python)

# Import what I will need
import math
import numpy as np
import matplotlib.pyplot as plt

# The data that I used
data = [(1, 25), (2, 50), (3, 75), (0, 1), (1, 20), (2, 60), (4, 90), (2, 45), (0, 10), (1, 15), (3, 66), (2, 42), (1, 12)]

#classification of the data
X = []
Y = []
for list in data:
a = list[0]
b = list[1]
a = float(a)
b = float(b)
X.append(a)
Y.append(b)

# exploring the data
mean_X = np.mean(X)
mean_Y = np.mean(Y)
std_X = np.std(X)
std_Y = np.std(Y)
corr_XY = []
a = 0
b = 0
c = 0
i = 0
for x in X:
a = (X[i] – mean_X) * (Y[i] – mean_Y)
b = (X[i] – mean_X) * (X[i] – mean_X)
c = (Y[i] – mean_Y) * (Y[i] – mean_Y)
a += a
b += b
c += c
i += 1
d = math.sqrt(b) * math.sqrt(c)
r = a / d
print(“Correlation between X & Y: r = %s”) % r
print(“Mean X = %s, Mean Y = %s”) % (mean_X, mean_Y)
print(“StD X = %s, StD Y = %s”) % (std_X, std_Y)

# choosing intercept and slope
beta_0 = float(raw_input(“beta 1 = ?:”))
beta_1 = float(raw_input(“beta 2 = ?:”))
Alpha = float(raw_input(“choose the learning rate Alpha for the gradient descent:”))
print(“So we start with the training fonction: Y_hat = %s + %sX,
and with a learning rate = %s”) % (beta_0, beta_1, Alpha)
raw_input(“Here is the representation of the first function:”)

# This function shows the data&the linear regression on a graph
def plot_final(b0, b1):
  plt.axis([0, 4, 0, 100])
  plt.ylabel(‘outcome’)
  plt.xlabel(‘features’)
  v1 = np.arange(0, 4, 0.1)
  v2 = b0 + b1*v1
  plt.plot(X, Y, ‘ro’)
  plt.plot(v1, v2)
  return plt.show()

# Calculating the outcome via our model and listing them in Y_hat
def Y_hat_calc(b0, b1):
  Y_hat = []
  for x in X:
    y_hat_temp = b0 + b1 * x
    Y_hat.append(y_hat_temp)
  return Y_hat

# Compute the current cost/energy/utility given a certain Y_hat
def cost_fct_calc(Yhat):
  m = float(len(X))
  rss_list = []
  i = 0
  rss_tot = 0
  for y in Y:
    b = (Yhat[i] – Y[i]) ** 2
    rss_list.append(b)
    rss_tot += b
    i += 1
  cost_fct = (1 / (2*m)) * rss_tot
  return cost_fct

# calculating the new betas given the new predicted Y_hat
def new_betas_calc(Yhat, b0, b1):
  cost_beta_0 = 0
  i = 0
  for x in X:
    tp = Yhat[i] – Y[i]
    cost_beta_0 += tp
    i += 1
  cost_beta_1 = 0
  i = 0
  for x in X:
    tp = X[i] * (Yhat[i] – Y[i])
    cost_beta_1 += tp
    i += 1
  beta_0_2 = b0 – ((Alpha / 10) * cost_beta_0)
  beta_1_2 = b1 – ((Alpha / 10) * cost_beta_1)
  return beta_0_2, beta_1_2

# Compute the Gradient Descent
def gradient_descent():
  Y_hat = Y_hat_calc(beta_0, beta_1)
  J0 = cost_fct_calc(Y_hat)
  a_betas = new_betas_calc(Y_hat, beta_0, beta_1)
  New_Y_hat = Y_hat_calc(a_betas[0], a_betas[1])
  J1 = cost_fct_calc(New_Y_hat)
  a_betas = new_betas_calc(New_Y_hat, a_betas[0], a_betas[1])
  while (J0 > (J1 + 0.5)):
    Y_hat_recent = Y_hat_calc(a_betas[0], a_betas[1])
    J0 = cost_fct_calc(Y_hat_recent)
    a_betas = new_betas_calc(Y_hat_recent, a_betas[0],
                                                      a_betas[1])
    Y_hat_final = Y_hat_calc(a_betas[0], a_betas[1])
    J1 = cost_fct_calc(Y_hat_final)
  return a_betas

# run the script
Betas_end = gradient_descent()
# show the final parameters
print(Betas_end)
# plot it & run it
Betas_show = plot_final(Betas_end[0], Betas_end[1])
print(Betas_show)
# Show the final parameters in a function Y = B0 + B1X
a = Betas_end[0]
b = Betas_end[1]
print(“Final function is: Y = %s + %s * X “) % (a, b)

# Final results plotted

Screen Shot 2016-03-16 at 10.31.38

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s