Skip to content

Instantly share code, notes, and snippets.

@a7r3
Last active April 5, 2019 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a7r3/6d1d8ed44f5d4e264d45c0f7496ea200 to your computer and use it in GitHub Desktop.
Save a7r3/6d1d8ed44f5d4e264d45c0f7496ea200 to your computer and use it in GitHub Desktop.
95,85
85,95
80,70
70,65
60,70
points = []
with open("data.txt", 'r') as f:
lines = f.readlines()
for line in lines:
line = line.rstrip('\n')
points.append([int(x) for x in line.split(',')])
sum_x, mean_x, sum_y, mean_y = 0, 0, 0, 0
for point in points:
sum_x += point[0]
sum_y += point[1]
mean_x = sum_x / len(points)
mean_y = sum_y / len(points)
a = []
b = []
ab = []
for point in points:
current_a = point[0] - mean_x
current_b = point[1] - mean_y
a.append(current_a)
b.append(current_b)
ab.append(current_a * current_b)
b1_num = 0
b1_den = 0
for i in range(len(points)):
b1_num += a[i] * b[i]
b1_den += a[i] ** 2
b1 = b1_num / b1_den
b0 = mean_y - (b1 * mean_x)
print(points)
for i in range(len(points)):
points[i][1] = b1 * points[i][0] + b0
print(b0, b1)
print(points)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment