Path: blob/master/1_Supervised_Machine_Learning/Week 2. Regression with multiple input variables/public_tests.py
2826 views
import numpy as np123def compute_cost_test(target):4# print("Using X with shape (4, 1)")5# Case 16x = np.array([2, 4, 6, 8]).T7y = np.array([7, 11, 15, 19]).T8initial_w = 29initial_b = 3.010cost = target(x, y, initial_w, initial_b)11assert cost == 0, f"Case 1: Cost must be 0 for a perfect prediction but got {cost}"1213# Case 214x = np.array([2, 4, 6, 8]).T15y = np.array([7, 11, 15, 19]).T16initial_w = 2.017initial_b = 1.018cost = target(x, y, initial_w, initial_b)19assert cost == 2, f"Case 2: Cost must be 2 but got {cost}"2021# print("Using X with shape (5, 1)")22# Case 323x = np.array([1.5, 2.5, 3.5, 4.5, 1.5]).T24y = np.array([4, 7, 10, 13, 5]).T25initial_w = 126initial_b = 0.027cost = target(x, y, initial_w, initial_b)28assert np.isclose(cost, 15.325), f"Case 3: Cost must be 15.325 for a perfect prediction but got {cost}"2930# Case 431initial_b = 1.032cost = target(x, y, initial_w, initial_b)33assert np.isclose(cost, 10.725), f"Case 4: Cost must be 10.725 but got {cost}"3435# Case 536y = y - 237initial_b = 1.038cost = target(x, y, initial_w, initial_b)39assert np.isclose(cost, 4.525), f"Case 5: Cost must be 4.525 but got {cost}"4041print("\033[92mAll tests passed!")424344def compute_gradient_test(target):45print("Using X with shape (4, 1)")46# Case 147x = np.array([2, 4, 6, 8]).T48y = np.array([4.5, 8.5, 12.5, 16.5]).T49initial_w = 2.50initial_b = 0.551dj_dw, dj_db = target(x, y, initial_w, initial_b)52# assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"53assert dj_db == 0.0, f"Case 1: dj_db is wrong: {dj_db} != 0.0"54assert np.allclose(dj_dw, 0), f"Case 1: dj_dw is wrong: {dj_dw} != [[0.0]]"5556# Case 257x = np.array([2, 4, 6, 8]).T58y = np.array([4, 7, 10, 13]).T + 259initial_w = 1.560initial_b = 161dj_dw, dj_db = target(x, y, initial_w, initial_b)62# assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"63assert dj_db == -2, f"Case 2: dj_db is wrong: {dj_db} != -2"64assert np.allclose(dj_dw, -10.0), f"Case 1: dj_dw is wrong: {dj_dw} != -10.0"6566print("\033[92mAll tests passed!")67686970