Gradient descent multi-variate linear regression Python

# Import the needed module 
import xlrd
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

# Opening & Reading the excel doc
base = xlrd.open_workbook(‘data.xls’)
first = base.sheet_by_index(0)

# Extracting data from the excel document

Group_X2 = []
Group_X3 = []
Group_Y = []
Group_X1.append(first.col_values(0))
Group_X2.append(first.col_values(1))
Group_X3.append(first.col_values(2))
Group_Y.append(first.col_values(3))
X1 = []
X2 = []
X3 = []
Y = []
pts = []
for i in Group_X1:
  for x in i:
    X1.append(x)
del X1[0]
for i in Group_X2:
  for x in i:
    X2.append(x)
del X2[0]
for i in Group_X3:
  for x in i:
    X3.append(x)
del X3[0]
for i in Group_Y1:
  for y in i:
    Y.append(y)
del Y[0]

# Gathering the data into a dictionary
for i in range(0, 50):
  pts.append(X1[i])
  pts.append(X2[i])
  pts.append(X3[i])
points = {}
i = 0
for a in range(0, 50):
   points[a] = [pts[i], pts[i+1], pts[i+2], Y[a]]
   i += 3

# Compute the current cost/energy/utility given a certain Y_hat
def compute_error_for_line_given_points(b, m1, m2, points):
  totalError = 0
  for i in range(0, len(points)):
    x1 = points[i][0]
    x2 = points[i][2]
    y = points[i][3]
    totalError += (y – (m1 * x1 + b + m2 * x2 + b)) ** 2
  return totalError / float(len(points))

# Compute the descent
def step_gradient(b_current, m1_current, m2_current, points, learningRate):
  b_gradient = 0
  m1_gradient = 0
  m2_gradient = 0
  N = float(len(points))
  for i in range(0, len(X)):
    x1 = points[i][0]
    x2 = points[i][2]
    y = points[i][3]
    b_gradient += -(2/N) * (y – ((m1_current * x1) + b_current))
    m1_gradient += -(2/N) * x1 * (y – ((m1_current * x1 +
                                                            m2_current * x2) + b_current))
    m2_gradient += -(2/N) * x2 * (y – ((m1_current * x1 +
                                                             m2_current * x2) + b_current))
  new_b = b_current – (learningRate * b_gradient)
  new_m1 = m1_current – (learningRate * m1_gradient)
  new_m2 = m2_current – (learningRate * m2_gradient)
  return [new_b, new_m1, new_m2]

# Compute the gradient descent
def gradient_descent_runner(points, starting_b, starting_m1, starting_m2,
                                                         learning_rate, num_iterations):
  b = starting_b
  m1 = starting_m1
  m2 = starting_m2
  for i in range(num_iterations):
    b, m1, m2 = step_gradient(b, m1, m2, points, learning_rate)
  return [b, m1, m2]

# run the gradient descent function
def ranthis():
  learning_rate = 0.0001
  initial_b = 0 # initial y-intercept guess
  initial_m1 = 0 # initial slope guess
  initial_m2 = 0 # initial slope 2 guess
  num_iterations = 1000
  print “Starting gradient descent at b = {0}, m = {1}, m1 = {2}, \
              error = {3}”.format(initial_b, initial_m1, initial_m2,
                compute_error_for_line_given_points(initial_b,
                initial_m1, initial_m2, points))
  print “Running…”
  [b, m1, m2] = gradient_descent_runner(points, initial_b, initial_m1,
                                                                             initial_m2, learning_rate, num_iterations)
  print “After {0} iterations b = {1}, m1 = {2}, \
  m2 = {3} error = {4}”.format(num_iterations, b, m1, m2,
                                                         compute_error_for_line_given_points(b, m1, m2, points))
  return b, m1, m2

# run the prog
betas = ranthis()
b0 = betas[0]
b1 = betas[1]
b2 = betas[2]

# plot the answer
plt3d = plt.figure().gca(projection=’3d’)
plt3d.scatter(X1, X3, Y, c=’r’, marker=’o’)
v1 = np.arange(15, 31, 1)
v2 = np.arange(35, 51, 1)
vy = b0 + b1 * v1 + b2 * v2
plt3d.plot(v1, v2, vy, ‘g-‘)
plt3d.set_xlabel(‘X1’)
plt3d.set_ylabel(‘X2’)
plt3d.set_zlabel(‘Y’)
plt.show()

# And this is what it looks like

Screen Shot 2016-03-16 at 11.15.55

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