Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/C1W3A1/public_tests.py
3748 views
import numpy as np1import math23def sigmoid_test(target):4assert np.isclose(target(3.0), 0.9525741268224334), "Failed for scalar input"5assert np.allclose(target(np.array([2.5, 0])), [0.92414182, 0.5]), "Failed for 1D array"6assert np.allclose(target(np.array([[2.5, -2.5], [0, 1]])),7[[0.92414182, 0.07585818], [0.5, 0.73105858]]), "Failed for 2D array"8print('\033[92mAll tests passed!')910def compute_cost_test(target):11X = np.array([[0, 0, 0, 0]]).T12y = np.array([0, 0, 0, 0])13w = np.array([0])14b = 115result = target(X, y, w, b)16if math.isinf(result):17raise ValueError("Did you get the sigmoid of z_wb?")1819np.random.seed(17)20X = np.random.randn(5, 2)21y = np.array([1, 0, 0, 1, 1])22w = np.random.randn(2)23b = 024result = target(X, y, w, b)25assert np.isclose(result, 2.15510667), f"Wrong output. Expected: {2.15510667} got: {result}"2627X = np.random.randn(4, 3)28y = np.array([1, 1, 0, 0])29w = np.random.randn(3)30b = 03132result = target(X, y, w, b)33assert np.isclose(result, 0.80709376), f"Wrong output. Expected: {0.80709376} got: {result}"3435X = np.random.randn(4, 3)36y = np.array([1, 0,1, 0])37w = np.random.randn(3)38b = 339result = target(X, y, w, b)40assert np.isclose(result, 0.4529660647), f"Wrong output. Expected: {0.4529660647} got: {result}. Did you inizialized z_wb = b?"4142print('\033[92mAll tests passed!')4344def compute_gradient_test(target):45np.random.seed(1)46X = np.random.randn(7, 3)47y = np.array([1, 0, 1, 0, 1, 1, 0])48test_w = np.array([1, 0.5, -0.35])49test_b = 1.750dj_db, dj_dw = target(X, y, test_w, test_b)5152assert np.isclose(dj_db, 0.28936094), f"Wrong value for dj_db. Expected: {0.28936094} got: {dj_db}"53assert dj_dw.shape == test_w.shape, f"Wrong shape for dj_dw. Expected: {test_w.shape} got: {dj_dw.shape}"54assert np.allclose(dj_dw, [-0.11999166, 0.41498775, -0.71968405]), f"Wrong values for dj_dw. Got: {dj_dw}"5556print('\033[92mAll tests passed!')5758def predict_test(target):59np.random.seed(5)60b = 0.561w = np.random.randn(3)62X = np.random.randn(8, 3)6364result = target(X, w, b)65wrong_1 = [1., 1., 0., 0., 1., 0., 0., 1.]66expected_1 = [1., 1., 1., 0., 1., 0., 0., 1.]67if np.allclose(result, wrong_1):68raise ValueError("Did you apply the sigmoid before applying the threshold?")69assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"70assert np.allclose(result, expected_1), f"Wrong output: Expected : {expected_1} got: {result}"7172b = -1.773w = np.random.randn(4) + 0.674X = np.random.randn(6, 4)7576result = target(X, w, b)77expected_2 = [0., 0., 0., 1., 1., 0.]78assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"79assert np.allclose(result,expected_2), f"Wrong output: Expected : {expected_2} got: {result}"8081print('\033[92mAll tests passed!')8283def compute_cost_reg_test(target):84np.random.seed(1)85w = np.random.randn(3)86b = 0.487X = np.random.randn(6, 3)88y = np.array([0, 1, 1, 0, 1, 1])89lambda_ = 0.190expected_output = target(X, y, w, b, lambda_)9192assert np.isclose(expected_output, 0.5469746792761936), f"Wrong output. Expected: {0.5469746792761936} got:{expected_output}"9394w = np.random.randn(5)95b = -0.696X = np.random.randn(8, 5)97y = np.array([1, 0, 1, 0, 0, 1, 0, 1])98lambda_ = 0.0199output = target(X, y, w, b, lambda_)100assert np.isclose(output, 1.2608591964119995), f"Wrong output. Expected: {1.2608591964119995} got:{output}"101102w = np.array([2, 2, 2, 2, 2])103b = 0104X = np.zeros((8, 5))105y = np.array([0.5] * 8)106lambda_ = 3107output = target(X, y, w, b, lambda_)108expected = -np.log(0.5) + 3. / (2. * 8.) * 20.109assert np.isclose(output, expected), f"Wrong output. Expected: {expected} got:{output}"110111print('\033[92mAll tests passed!')112113def compute_gradient_reg_test(target):114np.random.seed(1)115w = np.random.randn(5)116b = 0.2117X = np.random.randn(7, 5)118y = np.array([0, 1, 1, 0, 1, 1, 0])119lambda_ = 0.1120expected1 = (-0.1506447567869257, np.array([ 0.19530838, -0.00632206, 0.19687367, 0.15741161, 0.02791437]))121dj_db, dj_dw = target(X, y, w, b, lambda_)122123assert np.isclose(dj_db, expected1[0]), f"Wrong dj_db. Expected: {expected1[0]} got: {dj_db}"124assert np.allclose(dj_dw, expected1[1]), f"Wrong dj_dw. Expected: {expected1[1]} got: {dj_dw}"125126127w = np.random.randn(7)128b = 0129X = np.random.randn(7, 7)130y = np.array([1, 0, 0, 0, 1, 1, 0])131lambda_ = 0132expected2 = (0.02660329857573818, np.array([ 0.23567643, -0.06921029, -0.19705212, -0.0002884 , 0.06490588,1330.26948175, 0.10777992]))134dj_db, dj_dw = target(X, y, w, b, lambda_)135assert np.isclose(dj_db, expected2[0]), f"Wrong dj_db. Expected: {expected2[0]} got: {dj_db}"136assert np.allclose(dj_dw, expected2[1]), f"Wrong dj_dw. Expected: {expected2[1]} got: {dj_dw}"137138print('\033[92mAll tests passed!')139140141