Reggie's Linear Regression: Output isn't matching solution's output. Can someone help me spot my mistake?


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?


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)

Note that there are multiple lines that give you roughly the same total error of 5.
Your code seems fine to me.

Okay, so I’m not sure why you’re getting the wrong answers. I copied your code into an offline IDE and got the right answers:

Running the code on the Jupyter Notebook file gives the right answers, too.

In short, I’m not sure what’s going on. You might try clearing your cache, or running all of your code snippets one after another. That way, you get a clear progression through the problem.