Can someone explain the calculus within this gradient descent function?

108 Views Asked by At

I'm new to calculus and I was convinced that what is happening in the gradient descent function is not calculus. It turns out that it is, can someone explain how? Where is the derivative being computed?

import os
import sys 
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sympy import *
from numpy.linalg import inv
import random
#sizes = [2104,1416,1534,852]
#prices = [460,232,315,178]

path = os.getcwd() + '\data\ex1data1.txt'
data = pd.read_csv(path, header=None, names=['Population', 'Profit'])

# 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] 

X = np.matrix(X.values)  
y = np.matrix(y.values) 

print X,y  
theta = np.matrix(np.array([0,0]))

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

def gradientDescent(X, y, theta, alpha, iters):  
    temp_theta = np.matrix(np.zeros(theta.shape))
    num_parameters = int(theta.ravel().shape[1])
    cost = np.zeros(iters)

    for i in range(iters):
        error = (X * theta.T) - y
        for j in range(num_parameters):
            error_term = np.multiply(error, X[:,j])
            temp_theta[0,j] = theta[0,j] - ((alpha / len(X)) * np.sum(error_term))

        theta = temp_theta
        cost[i] = computeCost(X, y, theta)

    return theta, cost

# initialize variables for learning rate and iterations
alpha = 0.01  
iters = 1000

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

print g

x = np.linspace(data.Population.min(), data.Population.max(), 100)  
f = g[0, 0] + (g[0, 1] * x)

fig, ax = plt.subplots(figsize=(12,8))  
ax.plot(x, f, 'r', label='Prediction')  
ax.scatter(data.Population, data.Profit, label='Traning Data')  
ax.legend(loc=2)  
ax.set_xlabel('Population')  
ax.set_ylabel('Profit')  
ax.set_title('Predicted Profit vs. Population Size')
plt.show()