app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,7 @@ y_actual = [30, 35, 40, 45, 50]
|
|
7 |
alpha = 0.01
|
8 |
max_iters = 1000
|
9 |
|
10 |
-
# Initial values for Theta0 and
|
11 |
Theta0 = 0
|
12 |
Theta1 = 0
|
13 |
Theta2 = 0
|
@@ -22,24 +22,25 @@ while iter_count < max_iters:
|
|
22 |
|
23 |
# Compute the errors
|
24 |
errors = [y_pred[i] - y_actual[i] for i in range(len(x1))]
|
25 |
-
|
26 |
# Update Theta0, Theta1, and Theta2
|
27 |
Theta0 -= alpha * sum(errors) / len(x1)
|
28 |
Theta1 -= alpha * sum([errors[i] * x1[i] for i in range(len(x1))]) / len(x1)
|
29 |
Theta2 -= alpha * sum([errors[i] * x2[i] for i in range(len(x2))]) / len(x2)
|
30 |
-
|
31 |
# Compute the cost function
|
32 |
cost = sum([(y_pred[i] - y_actual[i]) ** 2 for i in range(len(x1))]) / (2 * len(x1))
|
33 |
-
|
34 |
# Print the cost function every 100 iterations
|
35 |
if iter_count % 100 == 0:
|
36 |
-
print("Iteration {}: Cost = {}, Theta0 = {}, Theta1 = {}, Theta2 = {}".format(iter_count, cost, Theta0, Theta1,
|
37 |
-
|
|
|
38 |
# Check for convergence (if the cost is decreasing by less than 0.0001)
|
39 |
if iter_count > 0 and abs(cost - prev_cost) < 0.0001:
|
40 |
print("Converged after {} iterations".format(iter_count))
|
41 |
break
|
42 |
-
|
43 |
# Update the iteration counter and previous cost
|
44 |
iter_count += 1
|
45 |
prev_cost = cost
|
|
|
7 |
alpha = 0.01
|
8 |
max_iters = 1000
|
9 |
|
10 |
+
# Initial values for Theta0, Theta1, and Theta2
|
11 |
Theta0 = 0
|
12 |
Theta1 = 0
|
13 |
Theta2 = 0
|
|
|
22 |
|
23 |
# Compute the errors
|
24 |
errors = [y_pred[i] - y_actual[i] for i in range(len(x1))]
|
25 |
+
|
26 |
# Update Theta0, Theta1, and Theta2
|
27 |
Theta0 -= alpha * sum(errors) / len(x1)
|
28 |
Theta1 -= alpha * sum([errors[i] * x1[i] for i in range(len(x1))]) / len(x1)
|
29 |
Theta2 -= alpha * sum([errors[i] * x2[i] for i in range(len(x2))]) / len(x2)
|
30 |
+
|
31 |
# Compute the cost function
|
32 |
cost = sum([(y_pred[i] - y_actual[i]) ** 2 for i in range(len(x1))]) / (2 * len(x1))
|
33 |
+
|
34 |
# Print the cost function every 100 iterations
|
35 |
if iter_count % 100 == 0:
|
36 |
+
print("Iteration {}: Cost = {}, Theta0 = {}, Theta1 = {}, Theta2 = {}".format(iter_count, cost, Theta0, Theta1,
|
37 |
+
Theta2))
|
38 |
+
|
39 |
# Check for convergence (if the cost is decreasing by less than 0.0001)
|
40 |
if iter_count > 0 and abs(cost - prev_cost) < 0.0001:
|
41 |
print("Converged after {} iterations".format(iter_count))
|
42 |
break
|
43 |
+
|
44 |
# Update the iteration counter and previous cost
|
45 |
iter_count += 1
|
46 |
prev_cost = cost
|