-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradientStepFunctions.py
40 lines (34 loc) · 1010 Bytes
/
gradientStepFunctions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def get_gradient_at_b(x, y, b, m):
N = len(x)
diff = 0
for i in range(N):
x_val = x[i]
y_val = y[i]
diff += (y_val - ((m * x_val) + b))
b_gradient = -(2/N) * diff
return b_gradient
def get_gradient_at_m(x, y, b, m):
N = len(x)
diff = 0
for i in range(N):
x_val = x[i]
y_val = y[i]
diff += x_val * (y_val - ((m * x_val) + b))
m_gradient = -(2/N) * diff
return m_gradient
# Define your step_gradient function here
def step_gradient(x, y, b_current, m_current):
b_gradient = get_gradient_at_b(x, y, b_current, m_current)
b = b_current - (0.01 * b_gradient)
m_gradient = get_gradient_at_m(x, y, b_current, m_current)
m = m_current - (0.01 * m_gradient)
return (b, m)
months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
revenue = [52, 74, 79, 95, 115, 110, 129, 126, 147, 146, 156, 184]
# current intercept guess:
b = 0
# current slope guess:
m = 0
# Call your function here to update b and m
b, m = step_gradient(months, revenue, b, m)
print(b, m)