-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinefit_gradient.py
More file actions
62 lines (50 loc) · 1.92 KB
/
Copy pathlinefit_gradient.py
File metadata and controls
62 lines (50 loc) · 1.92 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import numpy as np
import matplotlib.pyplot as plt
import os
gscale = 2 if "ANDROID_STORAGE" in os.environ else 1
plt.rcParams.update({'font.size': 14*gscale})
# Gradient descent function to fit a line ax + by + c = 0
def gradient_descent(x, y, learning_rate=0.01, num_iterations=300):
# Initial parameters a, b, c
a = 1.0
b = 1.0
c = 1.0
for iter in range(num_iterations):
plt.clf()
# Compute the common denominator d
d = (a**2 + b**2)
# Compute the gradients
da = 2 * np.sum((a*x + b*y + c) * (x * d - a * (a*x + b*y + c)) / d**2)
db = 2 * np.sum((a*x + b*y + c) * (y * d - b * (a*x + b*y + c)) / d**2)
dc = 2 * np.sum((a * x + b * y + c) / d)
# Update parameters
a -= learning_rate * da
b -= learning_rate * db
c -= learning_rate * dc
# Print loss
loss = np.sum((a*x + b*y + c)**2/(a**2 + b**2))
bkg=dict(facecolor="white", edgecolor="white", boxstyle="round,pad=0")
plt.text(0.4, 0.1, f"Iter={iter}: loss = {loss:0.3f}", transform=plt.gca().transAxes, fontsize=18*gscale, color="black", bbox=bkg)
# Plotting the data points and the fitted line
plt.scatter(x, y, color='blue', s=100*gscale, label='Data points')
# Calculate fitted line points
x_vals = np.linspace(min(x), max(x), 100)
y_vals = -(a * x_vals + c) / b
plt.plot(x_vals, y_vals, color='red', linewidth=3*gscale, label='Fitted line')
plt.xlim(0, 8)
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('Line Fitting: Gradient Descent')
plt.pause(0.1)
return a, b, c
# Example usage
# Sample data points
x = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([2, 2, 3, 5, 4, 7, 7])
# Init display
plt.figure(figsize=(7*gscale, 7*gscale))
plt.pause(0.1)
# Run gradient descent
a, b, c = gradient_descent(x, y)
plt.show()