Advertisement
julianzhang

Week 2 Lab: Linear Regression

Aug 28th, 2022
1,353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.65 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from utils import *
  4. import copy
  5. import math
  6. %matplotlib inline
  7.  
  8. # load the dataset
  9. x_train, y_train = load_data()
  10.  
  11. # print x_train
  12. print("Type of x_train:",type(x_train))
  13. print("First five elements of x_train are:\n", x_train[:5])
  14.  
  15. # print y_train
  16. print("Type of y_train:",type(y_train))
  17. print("First five elements of y_train are:\n", y_train[:5])  
  18.  
  19. print ('The shape of x_train is:', x_train.shape)
  20. print ('The shape of y_train is: ', y_train.shape)
  21. print ('Number of training examples (m):', len(x_train))
  22.  
  23. # Create a scatter plot of the data. To change the markers to red "x",
  24. # we used the 'marker' and 'c' parameters
  25. plt.scatter(x_train, y_train, marker='x', c='r')
  26.  
  27. # Set the title
  28. plt.title("Profits vs. Population per city")
  29. # Set the y-axis label
  30. plt.ylabel('Profit in $10,000')
  31. # Set the x-axis label
  32. plt.xlabel('Population of City in 10,000s')
  33. plt.show()
  34.  
  35. # UNQ_C1
  36. # GRADED FUNCTION: compute_cost
  37.  
  38. def compute_cost(x, y, w, b):
  39.     """
  40.    Computes the cost function for linear regression.
  41.    
  42.    Args:
  43.        x (ndarray): Shape (m,) Input to the model (Population of cities)
  44.        y (ndarray): Shape (m,) Label (Actual profits for the cities)
  45.        w, b (scalar): Parameters of the model
  46.    
  47.    Returns
  48.        total_cost (float): The cost of using w,b as the parameters for linear regression
  49.               to fit the data points in x and y
  50.    """
  51.     # number of training examples
  52.     m = x.shape[0]
  53.    
  54.     # You need to return this variable correctly
  55.     total_cost = 0
  56.    
  57.     ### START CODE HERE ###
  58.    
  59.     cost_sum=0
  60.     for i in range(m):
  61.         pred=w*x[i]+b
  62.         cost=(pred-y[i])**2
  63.         cost_sum+=cost
  64.        
  65.     total_cost=(1/(2*m))*cost_sum
  66.     ### END CODE HERE ###
  67.  
  68.     return total_cost
  69.  
  70. # Compute cost with some initial values for paramaters w, b
  71. initial_w = 2
  72. initial_b = 1
  73.  
  74. cost = compute_cost(x_train, y_train, initial_w, initial_b)
  75. print(type(cost))
  76. print(f'Cost at initial w: {cost:.3f}')
  77.  
  78. # Public tests
  79. from public_tests import *
  80. compute_cost_test(compute_cost)
  81.  
  82. # UNQ_C2
  83. # GRADED FUNCTION: compute_gradient
  84. def compute_gradient(x, y, w, b):
  85.     """
  86.    Computes the gradient for linear regression
  87.    Args:
  88.      x (ndarray): Shape (m,) Input to the model (Population of cities)
  89.      y (ndarray): Shape (m,) Label (Actual profits for the cities)
  90.      w, b (scalar): Parameters of the model  
  91.    Returns
  92.      dj_dw (scalar): The gradient of the cost w.r.t. the parameters w
  93.      dj_db (scalar): The gradient of the cost w.r.t. the parameter b    
  94.     """
  95.    
  96.     # Number of training examples
  97.     m = x.shape[0]
  98.    
  99.     # You need to return the following variables correctly
  100.     dj_dw = 0
  101.     dj_db = 0
  102.    
  103.     ### START CODE HERE ###
  104.    
  105.     for i in range (m):
  106.         # cost
  107.         f_wb = w * x[i] + b
  108.        
  109.         # gradients of w and b
  110.         dj_dw_i = (f_wb - y[i]) * x[i]    
  111.         dj_db_i = f_wb - y[i]
  112.        
  113.         #derivatives of w and b
  114.         dj_dw += dj_dw_i
  115.         dj_db += dj_db_i
  116.        
  117.     dj_dw = dj_dw/m
  118.     dj_db = dj_db/m
  119.    
  120.     ### END CODE HERE ###
  121.        
  122.     return dj_dw, dj_db
  123.  
  124.  
  125.  
  126. # Compute and display gradient with w initialized to zeroes
  127. initial_w = 0
  128. initial_b = 0
  129.  
  130. tmp_dj_dw, tmp_dj_db = compute_gradient(x_train, y_train, initial_w, initial_b)
  131. print('Gradient at initial w, b (zeros):', tmp_dj_dw, tmp_dj_db)
  132.  
  133. compute_gradient_test(compute_gradient)
  134.  
  135. # Compute and display cost and gradient with non-zero w
  136. test_w = 0.2
  137. test_b = 0.2
  138. tmp_dj_dw, tmp_dj_db = compute_gradient(x_train, y_train, test_w, test_b)
  139.  
  140. print('Gradient at test w, b:', tmp_dj_dw, tmp_dj_db)
  141.  
  142. def gradient_descent(x, y, w_in, b_in, cost_function, gradient_function, alpha, num_iters):
  143.     """
  144.    Performs batch gradient descent to learn theta. Updates theta by taking
  145.    num_iters gradient steps with learning rate alpha
  146.    
  147.    Args:
  148.      x :    (ndarray): Shape (m,)
  149.      y :    (ndarray): Shape (m,)
  150.      w_in, b_in : (scalar) Initial values of parameters of the model
  151.      cost_function: function to compute cost
  152.      gradient_function: function to compute the gradient
  153.      alpha : (float) Learning rate
  154.      num_iters : (int) number of iterations to run gradient descent
  155.    Returns
  156.      w : (ndarray): Shape (1,) Updated values of parameters of the model after
  157.          running gradient descent
  158.      b : (scalar)                Updated value of parameter of the model after
  159.          running gradient descent
  160.    """
  161.    
  162.     # number of training examples
  163.     m = len(x)
  164.    
  165.     # An array to store cost J and w's at each iteration — primarily for graphing later
  166.     J_history = []
  167.     w_history = []
  168.     w = copy.deepcopy(w_in)  #avoid modifying global w within function
  169.     b = b_in
  170.    
  171.     for i in range(num_iters):
  172.  
  173.         # Calculate the gradient and update the parameters
  174.         dj_dw, dj_db = gradient_function(x, y, w, b )  
  175.  
  176.         # Update Parameters using w, b, alpha and gradient
  177.         w = w - alpha * dj_dw              
  178.         b = b - alpha * dj_db              
  179.  
  180.         # Save cost J at each iteration
  181.         if i<100000:      # prevent resource exhaustion
  182.             cost =  cost_function(x, y, w, b)
  183.             J_history.append(cost)
  184.  
  185.         # Print cost every at intervals 10 times or as many iterations if < 10
  186.         if i% math.ceil(num_iters/10) == 0:
  187.             w_history.append(w)
  188.             print(f"Iteration {i:4}: Cost {float(J_history[-1]):8.2f}   ")
  189.        
  190.     return w, b, J_history, w_history #return w and J,w history for graphing
  191.  
  192. # initialize fitting parameters. Recall that the shape of w is (n,)
  193. initial_w = 0.
  194. initial_b = 0.
  195.  
  196. # some gradient descent settings
  197. iterations = 1500
  198. alpha = 0.01
  199.  
  200. w,b,_,_ = gradient_descent(x_train ,y_train, initial_w, initial_b,
  201.                      compute_cost, compute_gradient, alpha, iterations)
  202. print("w,b found by gradient descent:", w, b)
  203.  
  204. m = x_train.shape[0]
  205. predicted = np.zeros(m)
  206.  
  207. for i in range(m):
  208.     predicted[i] = w * x_train[i] + b
  209.  
  210. # Plot the linear fit
  211. plt.plot(x_train, predicted, c = "b")
  212.  
  213. # Create a scatter plot of the data.
  214. plt.scatter(x_train, y_train, marker='x', c='r')
  215.  
  216. # Set the title
  217. plt.title("Profits vs. Population per city")
  218. # Set the y-axis label
  219. plt.ylabel('Profit in $10,000')
  220. # Set the x-axis label
  221. plt.xlabel('Population of City in 10,000s')
  222.  
  223. predict1 = 3.5 * w + b
  224. print('For population = 35,000, we predict a profit of $%.2f' % (predict1*10000))
  225.  
  226. predict2 = 7.0 * w + b
  227. print('For population = 70,000, we predict a profit of $%.2f' % (predict2*10000))
  228.  
  229.                                                                
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement