In [49]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets.samples_generator import make_regression
# Create Regression Data set
X, Yactual = make_regression( n_samples=80, n_features=1, n_informative=1,random_state=0, noise=30)
plt.scatter(X,Yactual)
plt.xlabel('X')
plt.ylabel('Yactual')
Out[49]:
In [50]:
# Add a column of ones to features for constant term.
features = np.concatenate((np.ones((X.shape[0], 1)), X), axis=1)
In [51]:
def initializeTheta(features):
m, n = features.shape
return(np.zeros(n))
In [52]:
def predict(features,theta):
return(np.dot(features, theta))
In [53]:
def calculate_gradient(features,Yactual,theta):
m = len(Yactual)
error = predict(features,theta) - Yactual
return (np.dot(features.T, error)/m)
In [54]:
def compute_cost(features,Yactual,theta):
m = len(Yactual)
sose = np.square(predict(features, theta) - Yactual).sum()
cost = sose / (2 * m)
return cost
In [55]:
# Write some code here that updates the values of theta a number of times equal to
# num_iterations. Everytime you have computed the cost for a given set of thetas,
# you should append it to cost_history. The function should return both the final
# values of theta and the cost history.
def compute_gradient_descent(features, Yactual, theta, alpha, num_iterations):
cost_history = []
for i in range(num_iterations):
theta -= alpha * calculate_gradient(features, Yactual, theta)
J = compute_cost(features, Yactual, theta)
cost_history.append(J)
return theta, cost_history
In [63]:
# Run gradient descent algorithm for linear regression
num_iterations = 100
alpha = 0.1
theta = initializeTheta(features)
finalTheta,cost_history = compute_gradient_descent(features,Yactual,theta,alpha,num_iterations)
In [64]:
# Plot cost history
plt.plot(cost_history)
plt.xlabel('iteration')
plt.ylabel('cost')
Out[64]:
In [65]:
# Plot results
plt.scatter(X,Yactual)
# Overlay Predictive Model
Ypred = predict(features,finalTheta)
plt.plot(X,Ypred,c='red')
Out[65]:
In [58]: