link: https://www.codecademy.com/courses/learn-python-3/informationals/python3-reggies-linear-regression
I’m working on the offsite Jupiter notebook linear regression project and part 2 isn’t giving me the proper output. I’m getting 0,3, and 6 instead of 0.3000000000000000, 4 1.7000000000000002, 4.999999999999999
I don’t understand what I’m doing wrong. I’ve looked at the solution’s code and mine is practically identical. So why am I still getting the wrong answer?
code:
def get_line(m, b, x):
return m*x + b
def calculate_error(m, b, point):
x, y = point
diff = get_line(m, b, x) - y
return abs(diff)
def calculate_all_error(m, b, datapoints):
sum =0
for x in datapoints:
sum += calculate_error(m, b, x)
return sum
possible_ms = [m * 0.1 for m in range(-100, 101)]
possible_bs = [b * 0.1 for b in range(-200, 201)]
datapoints = [(1, 2), (2, 0), (3, 4), (4, 4), (5, 3)]
smallest_error = float("inf")
best_m = 0
best_b = 0
for m in possible_ms:
for b in possible_bs:
temp = calculate_all_error(m, b, datapoints)
if temp < smallest_error:
best_m = m
best_b = b
smallest_error = temp
print(best_m, best_b, smallest_error)