Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.76 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4.  
  5. def predict(X,theta):
  6.   # takes m by n matrix X as input and returns an mx1 vector containing the predictions h_theta(x^i) for each row x^i, i=1,...,m in X
  7.   ##### replace the next line with your code #####
  8.   pred = list(map(lambda row: np.dot(row, np.transpose(theta)), [X]))
  9.   return pred
  10.  
  11. def computeCost(X, y, theta):
  12.   # function calculates the cost J(theta) and return its value
  13.   ##### replace the next line with your code #####
  14.   cost = (1 / (2.0 * len(X))) * np.sum(np.power(predict(X, theta) - y, 2.0))
  15.   return cost
  16.  
  17. def computeGradient(X,y,theta):
  18.   # function calulate the gradient of J(theta) and returns its value
  19.   ##### replace the next line with your code #####
  20.   grad = 1/len(X) * np.sum((np.subtract(predict(X, theta), y))) * X
  21.   return grad
  22.  
  23. def gradientDescent(X, y, numparams):
  24.   # iteratively update parameter vector theta
  25.   # -- you should not modify this function
  26.  
  27.   # initialize variables for learning rate and iterations
  28.   alpha = 0.02
  29.   iters = 5000
  30.   cost = np.zeros(iters)
  31.   theta= np.zeros(numparams)
  32.  
  33.   for i in range(iters):
  34.     theta = theta - alpha * computeGradient(X,y,theta)
  35.     cost[i] = computeCost(X, y, theta)
  36.  
  37.   return theta, cost
  38.  
  39. def normaliseData(x):
  40.   # rescale data to lie between 0 and 1
  41.   scale = x.max(axis=0)
  42.   return (x/scale, scale)
  43.  
  44. def splitData(X,y):
  45.   # split data into training and test parts
  46.   # ... for now, we use all of the data for training and testing
  47.   Xtrain=X; ytrain=y; Xtest=X; ytest=y
  48.   return (Xtrain,ytrain,Xtest,ytest)
  49.  
  50. def main():
  51.   # load the data
  52.   data=np.loadtxt('stockprices.csv',usecols=(1,2))
  53.   X=data[:,0]
  54.   y=data[:,1]
  55.  
  56.   # plot the data so we can see how it looks
  57.   # (output is in file graph.png)
  58.   fig, ax = plt.subplots(figsize=(12, 8))
  59.   ax.scatter(X, y, label='Data')
  60.   ax.set_xlabel('Amazon')
  61.   ax.set_ylabel('Google')
  62.   ax.set_title('Google stock price vs Amazon')
  63.   fig.savefig('graph.png')
  64.  
  65.   # split the data into training and test parts
  66.   (Xtrain,ytrain,Xtest,ytest)=splitData(X,y)
  67.  
  68.   # add a column of ones to input data
  69.   m=len(y) # m is number of training data points
  70.   Xtrain = np.column_stack((np.ones((m, 1)), Xtrain))
  71.   (m,n)=Xtrain.shape # m is number of data points, n number of features
  72.  
  73.   # rescale training data to lie between 0 and 1
  74.   (Xt,Xscale) = normaliseData(Xtrain)
  75.   (yt,yscale) = normaliseData(ytrain)
  76.  
  77.   # calculate the prediction
  78.   print('testing the prediction function ...')
  79.   theta=(1,2)
  80.   print('when x=[1,1] and theta is [1,2]) cost = ',predict(np.ones(n),theta))
  81.   print('approx expected prediction is 3')
  82.   print('when x=[[1,1],[5,5]] and theta is [1,2]) cost = ',predict(np.array([[1,1],[5,5]]),theta))  
  83.   print('approx expected prediction is [3,15]')
  84.   input('Press Enter to continue...')
  85.  
  86.   # calculate the cost when theta iz zero
  87.   print('testing the cost function ...')
  88.   theta=np.zeros(n)
  89.   print('when theta is zero cost = ',computeCost(Xt,yt,theta))
  90.   print('approx expected cost value is 0.318')
  91.   input('Press Enter to continue...')
  92.  
  93.   # calculate the gradient when theta is zero
  94.   print('testing the gradient function ...')
  95.   print('when theta is zero gradient = ',computeGradient(Xt,yt,theta))
  96.   print('approx expected gradient value is [-0.79,-0.59]')
  97.   input('Press Enter to continue...')
  98.  
  99.   # perform gradient descent to "fit" the model parameters
  100.   print('running gradient descent ...')
  101.   theta, cost = gradientDescent(Xt, yt, n)
  102.   print('after running gradientDescent() theta=',theta)
  103.   print('approx expected value is [0.34, 0.61]')
  104.  
  105.   # plot some predictions
  106.   Xpred = np.linspace(X.min(), X.max(), 100)
  107.   Xpred = np.column_stack((np.ones((100, 1)), Xpred))
  108.   ypred = predict(Xpred/Xscale, theta)*yscale
  109.   fig, ax = plt.subplots(figsize=(12, 8))
  110.   ax.scatter(Xtest, ytest, color='b', label='Test Data')
  111.   ax.plot(Xpred[:,1], ypred, 'r', label='Prediction')
  112.   ax.set_xlabel('Amazon')
  113.   ax.set_ylabel('Google')
  114.   ax.legend(loc=2)
  115.   fig.savefig('pred.png')
  116.  
  117.   # and plot how the cost varies as the gradient descent proceeds
  118.   fig2, ax2 = plt.subplots(figsize=(12, 8))
  119.   ax2.semilogy(cost,'r')
  120.   ax2.set_xlabel('iteration')
  121.   ax2.set_ylabel('cost')
  122.   fig2.savefig('cost.png')
  123.  
  124.   # plot the cost function
  125.   fig3 = plt.figure()
  126.   ax3 = fig3.add_subplot(1, 1, 1, projection='3d')
  127.   n=100
  128.   theta0, theta1 = np.meshgrid(np.linspace(-3, 3, n), np.linspace(-3, 2, n))  
  129.   cost = np.empty((n,n))
  130.   for i in range(n):
  131.     for j in range(n):
  132.       cost[i,j] = computeCost(Xt,yt,(theta0[i,j],theta1[i,j]))
  133.   ax3.plot_surface(theta0,theta1,cost)
  134.   ax3.set_xlabel('theta0')
  135.   ax3.set_ylabel('theta1')
  136.   ax3.set_zlabel('J(theta)')
  137.   fig3.savefig('J.png')
  138.  
  139. if __name__ == '__main__':
  140.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement