Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
greyhatguy007
GitHub Repository: greyhatguy007/Machine-Learning-Specialization-Coursera
Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week3/C1W3A1/public_tests.py
3748 views
1
import numpy as np
2
import math
3
4
def sigmoid_test(target):
5
assert np.isclose(target(3.0), 0.9525741268224334), "Failed for scalar input"
6
assert np.allclose(target(np.array([2.5, 0])), [0.92414182, 0.5]), "Failed for 1D array"
7
assert np.allclose(target(np.array([[2.5, -2.5], [0, 1]])),
8
[[0.92414182, 0.07585818], [0.5, 0.73105858]]), "Failed for 2D array"
9
print('\033[92mAll tests passed!')
10
11
def compute_cost_test(target):
12
X = np.array([[0, 0, 0, 0]]).T
13
y = np.array([0, 0, 0, 0])
14
w = np.array([0])
15
b = 1
16
result = target(X, y, w, b)
17
if math.isinf(result):
18
raise ValueError("Did you get the sigmoid of z_wb?")
19
20
np.random.seed(17)
21
X = np.random.randn(5, 2)
22
y = np.array([1, 0, 0, 1, 1])
23
w = np.random.randn(2)
24
b = 0
25
result = target(X, y, w, b)
26
assert np.isclose(result, 2.15510667), f"Wrong output. Expected: {2.15510667} got: {result}"
27
28
X = np.random.randn(4, 3)
29
y = np.array([1, 1, 0, 0])
30
w = np.random.randn(3)
31
b = 0
32
33
result = target(X, y, w, b)
34
assert np.isclose(result, 0.80709376), f"Wrong output. Expected: {0.80709376} got: {result}"
35
36
X = np.random.randn(4, 3)
37
y = np.array([1, 0,1, 0])
38
w = np.random.randn(3)
39
b = 3
40
result = target(X, y, w, b)
41
assert np.isclose(result, 0.4529660647), f"Wrong output. Expected: {0.4529660647} got: {result}. Did you inizialized z_wb = b?"
42
43
print('\033[92mAll tests passed!')
44
45
def compute_gradient_test(target):
46
np.random.seed(1)
47
X = np.random.randn(7, 3)
48
y = np.array([1, 0, 1, 0, 1, 1, 0])
49
test_w = np.array([1, 0.5, -0.35])
50
test_b = 1.7
51
dj_db, dj_dw = target(X, y, test_w, test_b)
52
53
assert np.isclose(dj_db, 0.28936094), f"Wrong value for dj_db. Expected: {0.28936094} got: {dj_db}"
54
assert dj_dw.shape == test_w.shape, f"Wrong shape for dj_dw. Expected: {test_w.shape} got: {dj_dw.shape}"
55
assert np.allclose(dj_dw, [-0.11999166, 0.41498775, -0.71968405]), f"Wrong values for dj_dw. Got: {dj_dw}"
56
57
print('\033[92mAll tests passed!')
58
59
def predict_test(target):
60
np.random.seed(5)
61
b = 0.5
62
w = np.random.randn(3)
63
X = np.random.randn(8, 3)
64
65
result = target(X, w, b)
66
wrong_1 = [1., 1., 0., 0., 1., 0., 0., 1.]
67
expected_1 = [1., 1., 1., 0., 1., 0., 0., 1.]
68
if np.allclose(result, wrong_1):
69
raise ValueError("Did you apply the sigmoid before applying the threshold?")
70
assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"
71
assert np.allclose(result, expected_1), f"Wrong output: Expected : {expected_1} got: {result}"
72
73
b = -1.7
74
w = np.random.randn(4) + 0.6
75
X = np.random.randn(6, 4)
76
77
result = target(X, w, b)
78
expected_2 = [0., 0., 0., 1., 1., 0.]
79
assert result.shape == (len(X),), f"Wrong length. Expected : {(len(X),)} got: {result.shape}"
80
assert np.allclose(result,expected_2), f"Wrong output: Expected : {expected_2} got: {result}"
81
82
print('\033[92mAll tests passed!')
83
84
def compute_cost_reg_test(target):
85
np.random.seed(1)
86
w = np.random.randn(3)
87
b = 0.4
88
X = np.random.randn(6, 3)
89
y = np.array([0, 1, 1, 0, 1, 1])
90
lambda_ = 0.1
91
expected_output = target(X, y, w, b, lambda_)
92
93
assert np.isclose(expected_output, 0.5469746792761936), f"Wrong output. Expected: {0.5469746792761936} got:{expected_output}"
94
95
w = np.random.randn(5)
96
b = -0.6
97
X = np.random.randn(8, 5)
98
y = np.array([1, 0, 1, 0, 0, 1, 0, 1])
99
lambda_ = 0.01
100
output = target(X, y, w, b, lambda_)
101
assert np.isclose(output, 1.2608591964119995), f"Wrong output. Expected: {1.2608591964119995} got:{output}"
102
103
w = np.array([2, 2, 2, 2, 2])
104
b = 0
105
X = np.zeros((8, 5))
106
y = np.array([0.5] * 8)
107
lambda_ = 3
108
output = target(X, y, w, b, lambda_)
109
expected = -np.log(0.5) + 3. / (2. * 8.) * 20.
110
assert np.isclose(output, expected), f"Wrong output. Expected: {expected} got:{output}"
111
112
print('\033[92mAll tests passed!')
113
114
def compute_gradient_reg_test(target):
115
np.random.seed(1)
116
w = np.random.randn(5)
117
b = 0.2
118
X = np.random.randn(7, 5)
119
y = np.array([0, 1, 1, 0, 1, 1, 0])
120
lambda_ = 0.1
121
expected1 = (-0.1506447567869257, np.array([ 0.19530838, -0.00632206, 0.19687367, 0.15741161, 0.02791437]))
122
dj_db, dj_dw = target(X, y, w, b, lambda_)
123
124
assert np.isclose(dj_db, expected1[0]), f"Wrong dj_db. Expected: {expected1[0]} got: {dj_db}"
125
assert np.allclose(dj_dw, expected1[1]), f"Wrong dj_dw. Expected: {expected1[1]} got: {dj_dw}"
126
127
128
w = np.random.randn(7)
129
b = 0
130
X = np.random.randn(7, 7)
131
y = np.array([1, 0, 0, 0, 1, 1, 0])
132
lambda_ = 0
133
expected2 = (0.02660329857573818, np.array([ 0.23567643, -0.06921029, -0.19705212, -0.0002884 , 0.06490588,
134
0.26948175, 0.10777992]))
135
dj_db, dj_dw = target(X, y, w, b, lambda_)
136
assert np.isclose(dj_db, expected2[0]), f"Wrong dj_db. Expected: {expected2[0]} got: {dj_db}"
137
assert np.allclose(dj_dw, expected2[1]), f"Wrong dj_dw. Expected: {expected2[1]} got: {dj_dw}"
138
139
print('\033[92mAll tests passed!')
140
141