Advertisement
julianzhang

Week 3 Lab: Logistic Regression

Aug 28th, 2022
1,522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.63 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.  
  7. %matplotlib inline
  8.  
  9. # load dataset
  10. X_train, y_train = load_data("data/ex2data1.txt")
  11.  
  12. print("First five elements in X_train are:\n", X_train[:5])
  13. print("Type of X_train:",type(X_train))
  14.  
  15. print("First five elements in y_train are:\n", y_train[:5])
  16. print("Type of y_train:",type(y_train))
  17.  
  18. print ('The shape of X_train is: ' + str(X_train.shape))
  19. print ('The shape of y_train is: ' + str(y_train.shape))
  20. print ('We have m = %d training examples' % (len(y_train)))
  21.  
  22. # Plot examples
  23. plot_data(X_train, y_train[:], pos_label="Admitted", neg_label="Not admitted")
  24.  
  25. # Set the y-axis label
  26. plt.ylabel('Exam 2 score')
  27. # Set the x-axis label
  28. plt.xlabel('Exam 1 score')
  29. plt.legend(loc="upper right")
  30. plt.show()
  31.  
  32. # UNQ_C1
  33. # GRADED FUNCTION: sigmoid
  34.  
  35. def sigmoid(z):
  36.     """
  37.    Compute the sigmoid of z
  38.  
  39.    Args:
  40.        z (ndarray): A scalar, numpy array of any size.
  41.  
  42.    Returns:
  43.        g (ndarray): sigmoid(z), with the same shape as z
  44.        
  45.    """
  46.          
  47.     ### START CODE HERE ###
  48.     return 1/(1+(math.e)**(-z))
  49.    
  50.     ### END SOLUTION ###  
  51.    
  52.     return g
  53.  
  54. print ("sigmoid(0) = " + str(sigmoid(0)))
  55.  
  56. print ("sigmoid([ -1, 0, 1, 2]) = " + str(sigmoid(np.array([-1, 0, 1, 2]))))
  57.  
  58. # UNIT TESTS
  59. from public_tests import *
  60. sigmoid_test(sigmoid)
  61.  
  62. # UNQ_C2
  63. # GRADED FUNCTION: compute_cost
  64. def compute_cost(X, y, w, b, lambda_= 1):
  65.     """
  66.    Computes the cost over all examples
  67.    Args:
  68.      X : (ndarray Shape (m,n)) data, m examples by n features
  69.      y : (array_like Shape (m,)) target value
  70.      w : (array_like Shape (n,)) Values of parameters of the model      
  71.      b : scalar Values of bias parameter of the model
  72.      lambda_: unused placeholder
  73.    Returns:
  74.      total_cost: (scalar)         cost
  75.    """
  76.  
  77.     m, n = X.shape
  78.    
  79.     ### START CODE HERE ###
  80.     total_cost=0
  81.    
  82.     for i in range (m):
  83.         z_wb=0
  84.         for j in range (n):
  85.             z_wb+=w[j]*X[i][j]
  86.            
  87.         z_wb+=b
  88.         f_wb=sigmoid(z_wb)
  89.         loss = (-y[i]*np.log(f_wb))-(1-y[i])*np.log(1-f_wb)
  90.         total_cost+=loss
  91.     total_cost/=m
  92.    
  93.     ### END CODE HERE ###
  94.  
  95.     return total_cost
  96.  
  97. m, n = X_train.shape
  98.  
  99. # Compute and display cost with w initialized to zeroes
  100. initial_w = np.zeros(n)
  101. initial_b = 0.
  102. cost = compute_cost(X_train, y_train, initial_w, initial_b)
  103. print('Cost at initial w (zeros): {:.3f}'.format(cost))
  104.  
  105. # Compute and display cost with non-zero w
  106. test_w = np.array([0.2, 0.2])
  107. test_b = -24.
  108. cost = compute_cost(X_train, y_train, test_w, test_b)
  109.  
  110. print('Cost at test w,b: {:.3f}'.format(cost))
  111.  
  112.  
  113. # UNIT TESTS
  114. compute_cost_test(compute_cost)
  115.  
  116. # UNQ_C3
  117. # GRADED FUNCTION: compute_gradient
  118. def compute_gradient(X, y, w, b, lambda_=None):
  119.     """
  120.    Computes the gradient for logistic regression
  121.  
  122.    Args:
  123.      X : (ndarray Shape (m,n)) variable such as house size
  124.      y : (array_like Shape (m,1)) actual value
  125.      w : (array_like Shape (n,1)) values of parameters of the model      
  126.      b : (scalar)                 value of parameter of the model
  127.      lambda_: unused placeholder.
  128.    Returns
  129.      dj_dw: (array_like Shape (n,1)) The gradient of the cost w.r.t. the parameters w.
  130.      dj_db: (scalar)                The gradient of the cost w.r.t. the parameter b.
  131.    """
  132.     m, n = X.shape
  133.     dj_dw = np.zeros(w.shape)
  134.     dj_db = 0.
  135.  
  136.     ### START CODE HERE ###
  137.     for i in range(m):
  138.         z_wb = 0
  139.         for j in range(n):
  140.             z_wb += w[j]*X[i][j]
  141.         z_wb += b
  142.         f_wb = sigmoid(z_wb)
  143.        
  144.         dj_db += f_wb-y[i]
  145.        
  146.         for j in range(n):
  147.             dj_dw[j] += (f_wb-y[i])*X[i][j]
  148.            
  149.     dj_dw = dj_dw/m
  150.     dj_db = dj_db/m
  151.     ### END CODE HERE ###
  152.  
  153.        
  154.     return dj_db, dj_dw
  155.  
  156. # Compute and display gradient with w initialized to zeroes
  157. initial_w = np.zeros(n)
  158. initial_b = 0.
  159.  
  160. dj_db, dj_dw = compute_gradient(X_train, y_train, initial_w, initial_b)
  161. print(f'dj_db at initial w (zeros):{dj_db}' )
  162. print(f'dj_dw at initial w (zeros):{dj_dw.tolist()}' )
  163.  
  164. # Compute and display cost and gradient with non-zero w
  165. test_w = np.array([ 0.2, -0.5])
  166. test_b = -24
  167. dj_db, dj_dw  = compute_gradient(X_train, y_train, test_w, test_b)
  168.  
  169. print('dj_db at test_w:', dj_db)
  170. print('dj_dw at test_w:', dj_dw.tolist())
  171.  
  172. # UNIT TESTS    
  173. compute_gradient_test(compute_gradient)
  174.  
  175. def gradient_descent(X, y, w_in, b_in, cost_function, gradient_function, alpha, num_iters, lambda_):
  176.     """
  177.    Performs batch gradient descent to learn theta. Updates theta by taking
  178.    num_iters gradient steps with learning rate alpha
  179.    
  180.    Args:
  181.      X :    (array_like Shape (m, n)
  182.      y :    (array_like Shape (m,))
  183.      w_in : (array_like Shape (n,))  Initial values of parameters of the model
  184.      b_in : (scalar)                 Initial value of parameter of the model
  185.      cost_function:                  function to compute cost
  186.      alpha : (float)                 Learning rate
  187.      num_iters : (int)               number of iterations to run gradient descent
  188.      lambda_ (scalar, float)         regularization constant
  189.      
  190.    Returns:
  191.      w : (array_like Shape (n,)) Updated values of parameters of the model after
  192.          running gradient descent
  193.      b : (scalar)                Updated value of parameter of the model after
  194.          running gradient descent
  195.    """
  196.    
  197.     # number of training examples
  198.     m = len(X)
  199.    
  200.     # An array to store cost J and w's at each iteration primarily for graphing later
  201.     J_history = []
  202.     w_history = []
  203.    
  204.     for i in range(num_iters):
  205.  
  206.         # Calculate the gradient and update the parameters
  207.         dj_db, dj_dw = gradient_function(X, y, w_in, b_in, lambda_)  
  208.  
  209.         # Update Parameters using w, b, alpha and gradient
  210.         w_in = w_in - alpha * dj_dw              
  211.         b_in = b_in - alpha * dj_db              
  212.        
  213.         # Save cost J at each iteration
  214.         if i<100000:      # prevent resource exhaustion
  215.             cost =  cost_function(X, y, w_in, b_in, lambda_)
  216.             J_history.append(cost)
  217.  
  218.         # Print cost every at intervals 10 times or as many iterations if < 10
  219.         if i% math.ceil(num_iters/10) == 0 or i == (num_iters-1):
  220.             w_history.append(w_in)
  221.             print(f"Iteration {i:4}: Cost {float(J_history[-1]):8.2f}   ")
  222.        
  223.     return w_in, b_in, J_history, w_history #return w and J,w history for graphing
  224.  
  225. np.random.seed(1)
  226. intial_w = 0.01 * (np.random.rand(2).reshape(-1,1) - 0.5)
  227. initial_b = -8
  228.  
  229.  
  230. # Some gradient descent settings
  231. iterations = 10000
  232. alpha = 0.001
  233.  
  234. w,b, J_history,_ = gradient_descent(X_train ,y_train, initial_w, initial_b,
  235.                                    compute_cost, compute_gradient, alpha, iterations, 0)
  236.  
  237. plot_decision_boundary(w, b, X_train, y_train)
  238.  
  239. # UNQ_C4
  240. # GRADED FUNCTION: predict
  241.  
  242. def predict(X, w, b):
  243.     """
  244.    Predict whether the label is 0 or 1 using learned logistic
  245.    regression parameters w
  246.    
  247.    Args:
  248.    X : (ndarray Shape (m, n))
  249.    w : (array_like Shape (n,))      Parameters of the model
  250.    b : (scalar, float)              Parameter of the model
  251.  
  252.    Returns:
  253.    p: (ndarray (m,1))
  254.        The predictions for X using a threshold at 0.5
  255.    """
  256.     # number of training examples
  257.     m, n = X.shape  
  258.     p = np.zeros(m)
  259.    
  260.     ### START CODE HERE ###
  261.     # Loop over each example
  262.     for i in range(m):  
  263.         z_wb = 0
  264.         # Loop over each feature
  265.         for j in range(n):
  266.             # Add the corresponding term to z_wb
  267.             z_wb += w[j]*X[i][j]
  268.        
  269.         # Add bias term
  270.         z_wb += b
  271.        
  272.         # Calculate the prediction for this example
  273.         f_wb = sigmoid(z_wb)
  274.  
  275.         # Apply the threshold
  276.         p[i] = f_wb>=0.5
  277.        
  278.     ### END CODE HERE ###
  279.     return p
  280.  
  281. # Test your predict code
  282. np.random.seed(1)
  283. tmp_w = np.random.randn(2)
  284. tmp_b = 0.3    
  285. tmp_X = np.random.randn(4, 2) - 0.5
  286.  
  287. tmp_p = predict(tmp_X, tmp_w, tmp_b)
  288. print(f'Output of predict: shape {tmp_p.shape}, value {tmp_p}')
  289.  
  290. # UNIT TESTS        
  291. predict_test(predict)
  292.  
  293. #Compute accuracy on our training set
  294. p = predict(X_train, w,b)
  295. print('Train Accuracy: %f'%(np.mean(p == y_train) * 100))
  296.  
  297. #Compute accuracy on our training set
  298. p = predict(X_train, w,b)
  299. print('Train Accuracy: %f'%(np.mean(p == y_train) * 100))
  300.  
  301. # print X_train
  302. print("X_train:", X_train[:5])
  303. print("Type of X_train:",type(X_train))
  304.  
  305. # print y_train
  306. print("y_train:", y_train[:5])
  307. print("Type of y_train:",type(y_train))
  308.  
  309. print ('The shape of X_train is: ' + str(X_train.shape))
  310. print ('The shape of y_train is: ' + str(y_train.shape))
  311. print ('We have m = %d training examples' % (len(y_train)))
  312.  
  313. # Plot examples
  314. plot_data(X_train, y_train[:], pos_label="Accepted", neg_label="Rejected")
  315.  
  316. # Set the y-axis label
  317. plt.ylabel('Microchip Test 2')
  318. # Set the x-axis label
  319. plt.xlabel('Microchip Test 1')
  320. plt.legend(loc="upper right")
  321. plt.show()
  322.  
  323. print("Original shape of data:", X_train.shape)
  324.  
  325. mapped_X =  map_feature(X_train[:, 0], X_train[:, 1])
  326. print("Shape after feature mapping:", mapped_X.shape)
  327.  
  328. print("X_train[0]:", X_train[0])
  329. print("mapped X_train[0]:", mapped_X[0])
  330.  
  331. # UNQ_C5
  332. def compute_cost_reg(X, y, w, b, lambda_ = 1):
  333.     """
  334.    Computes the cost over all examples
  335.    Args:
  336.      X : (array_like Shape (m,n)) data, m examples by n features
  337.      y : (array_like Shape (m,)) target value
  338.      w : (array_like Shape (n,)) Values of parameters of the model      
  339.      b : (array_like Shape (n,)) Values of bias parameter of the model
  340.      lambda_ : (scalar, float)    Controls amount of regularization
  341.    Returns:
  342.      total_cost: (scalar)         cost
  343.    """
  344.  
  345.     m, n = X.shape
  346.    
  347.     # Calls the compute_cost function that you implemented above
  348.     cost_without_reg = compute_cost(X, y, w, b)
  349.    
  350.     # You need to calculate this value
  351.     reg_cost = 0.
  352.    
  353.     ### START CODE HERE ###
  354.     for j in range (n):
  355.         reg_cost+=w[j]**2
  356.        
  357.     #reg_cost/=(2*m)
  358.        
  359.     ### END CODE HERE ###
  360.    
  361.     # Add the regularization cost to get the total cost
  362.     total_cost = cost_without_reg + (lambda_/(2 * m)) * reg_cost
  363.  
  364.     return total_cost
  365.  
  366. X_mapped = map_feature(X_train[:, 0], X_train[:, 1])
  367. np.random.seed(1)
  368. initial_w = np.random.rand(X_mapped.shape[1]) - 0.5
  369. initial_b = 0.5
  370. lambda_ = 0.5
  371. cost = compute_cost_reg(X_mapped, y_train, initial_w, initial_b, lambda_)
  372.  
  373. print("Regularized cost :", cost)
  374.  
  375. # UNIT TEST    
  376. compute_cost_reg_test(compute_cost_reg)
  377.  
  378.  
  379. # UNQ_C6
  380. def compute_gradient_reg(X, y, w, b, lambda_ = 1):
  381.     """
  382.    Computes the gradient for linear regression
  383.  
  384.    Args:
  385.      X : (ndarray Shape (m,n))   variable such as house size
  386.      y : (ndarray Shape (m,))    actual value
  387.      w : (ndarray Shape (n,))    values of parameters of the model      
  388.      b : (scalar)                value of parameter of the model  
  389.      lambda_ : (scalar,float)    regularization constant
  390.    Returns
  391.      dj_db: (scalar)             The gradient of the cost w.r.t. the parameter b.
  392.      dj_dw: (ndarray Shape (n,)) The gradient of the cost w.r.t. the parameters w.
  393.  
  394.    """
  395.     m, n = X.shape
  396.    
  397.     dj_db, dj_dw = compute_gradient(X, y, w, b)
  398.  
  399.     ### START CODE HERE ###
  400.     for j in range (n):
  401.         dj_dw[j]+=(lambda_/m)*w[j]
  402.        
  403.     ### END CODE HERE ###        
  404.        
  405.     return dj_db, dj_dw
  406.  
  407. X_mapped = map_feature(X_train[:, 0], X_train[:, 1])
  408. np.random.seed(1)
  409. initial_w  = np.random.rand(X_mapped.shape[1]) - 0.5
  410. initial_b = 0.5
  411.  
  412. lambda_ = 0.5
  413. dj_db, dj_dw = compute_gradient_reg(X_mapped, y_train, initial_w, initial_b, lambda_)
  414.  
  415. print(f"dj_db: {dj_db}", )
  416. print(f"First few elements of regularized dj_dw:\n {dj_dw[:4].tolist()}", )
  417.  
  418. # UNIT TESTS    
  419. compute_gradient_reg_test(compute_gradient_reg)
  420.  
  421.  
  422. # Initialize fitting parameters
  423. np.random.seed(1)
  424. initial_w = np.random.rand(X_mapped.shape[1])-0.5
  425. initial_b = 1.
  426.  
  427. # Set regularization parameter lambda_ to 1 (you can try varying this)
  428. lambda_ = 0.01;                                          
  429. # Some gradient descent settings
  430. iterations = 10000
  431. alpha = 0.01
  432.  
  433. w,b, J_history,_ = gradient_descent(X_mapped, y_train, initial_w, initial_b,
  434.                                     compute_cost_reg, compute_gradient_reg,
  435.                                     alpha, iterations, lambda_)
  436.  
  437. plot_decision_boundary(w, b, X_mapped, y_train)
  438.  
  439. #Compute accuracy on the training set
  440. p = predict(X_mapped, w, b)
  441.  
  442. print('Train Accuracy: %f'%(np.mean(p == y_train) * 100))
  443.  
  444.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement