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/week2/C1W2A1/public_tests.py
3748 views
1
import numpy as np
2
3
def compute_cost_test(target):
4
# print("Using X with shape (4, 1)")
5
# Case 1
6
x = np.array([2, 4, 6, 8]).T
7
y = np.array([7, 11, 15, 19]).T
8
initial_w = 2
9
initial_b = 3.0
10
cost = target(x, y, initial_w, initial_b)
11
assert cost == 0, f"Case 1: Cost must be 0 for a perfect prediction but got {cost}"
12
13
# Case 2
14
x = np.array([2, 4, 6, 8]).T
15
y = np.array([7, 11, 15, 19]).T
16
initial_w = 2.0
17
initial_b = 1.0
18
cost = target(x, y, initial_w, initial_b)
19
assert cost == 2, f"Case 2: Cost must be 2 but got {cost}"
20
21
# print("Using X with shape (5, 1)")
22
# Case 3
23
x = np.array([1.5, 2.5, 3.5, 4.5, 1.5]).T
24
y = np.array([4, 7, 10, 13, 5]).T
25
initial_w = 1
26
initial_b = 0.0
27
cost = target(x, y, initial_w, initial_b)
28
assert np.isclose(cost, 15.325), f"Case 3: Cost must be 15.325 for a perfect prediction but got {cost}"
29
30
# Case 4
31
initial_b = 1.0
32
cost = target(x, y, initial_w, initial_b)
33
assert np.isclose(cost, 10.725), f"Case 4: Cost must be 10.725 but got {cost}"
34
35
# Case 5
36
y = y - 2
37
initial_b = 1.0
38
cost = target(x, y, initial_w, initial_b)
39
assert np.isclose(cost, 4.525), f"Case 5: Cost must be 4.525 but got {cost}"
40
41
print("\033[92mAll tests passed!")
42
43
def compute_gradient_test(target):
44
print("Using X with shape (4, 1)")
45
# Case 1
46
x = np.array([2, 4, 6, 8]).T
47
y = np.array([4.5, 8.5, 12.5, 16.5]).T
48
initial_w = 2.
49
initial_b = 0.5
50
dj_dw, dj_db = target(x, y, initial_w, initial_b)
51
#assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
52
assert dj_db == 0.0, f"Case 1: dj_db is wrong: {dj_db} != 0.0"
53
assert np.allclose(dj_dw, 0), f"Case 1: dj_dw is wrong: {dj_dw} != [[0.0]]"
54
55
# Case 2
56
x = np.array([2, 4, 6, 8]).T
57
y = np.array([4, 7, 10, 13]).T + 2
58
initial_w = 1.5
59
initial_b = 1
60
dj_dw, dj_db = target(x, y, initial_w, initial_b)
61
#assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
62
assert dj_db == -2, f"Case 1: dj_db is wrong: {dj_db} != -2"
63
assert np.allclose(dj_dw, -10.0), f"Case 1: dj_dw is wrong: {dj_dw} != -10.0"
64
65
print("\033[92mAll tests passed!")
66
67
68
69