I think the code to generate the possible_ms and possible_bs lists should be like：

possible_ms = [round(x * 0.1, 1) for x in range(-100, 101)]

possible_bs = [round(x * 0.1, 1) for x in range(-200, 201)]

If we don’t round the values of these two lists, there will be weird m and b values generated like -9.600000000000001 and -19.700000000000003 (I don’t know why yet).

It results in that the final values of best_m, best_b, and samllest_error are also weird (0.30000000000000004, 1.7000000000000002, 4.999999999999999).

So if we round the m and b values to one decimal, there will be no more weird m and b values. The final values will also be elegant (0.4, 1.6, 5.0).

1 Like

I was just looking for a solution to this problem.

I was trying to use a while loop for this question first:

possible_ms =

m = -10

while m < 11:

possible_ms.append(m)

m += 0.1

I tried to print out the m values to see the results and realized the numbers are off. I searched on Stack Overflow and this issue seems to have to do with how Python deals with float numbers. Anyway, thanks for posting the solution.

1 Like