Reggie Linear Regression Skeleton solution answer mismatch

in the solution this code is being used

possible_ms = [m * 0.1 for m in range(-100, 101)]

thus out-put is comming as
`[-10.0, -9.9, -9.8, -9.700000000000001, -9.600000000000001, -9.5, -9.4, -9.3, -9.200000000000001, -9.1, -9.0, -8.9, -8.8, -8.700000000000001, -8.6, -8.5, -8.4, -8.3, -8.200000000000001, -8.1, -8.0, -7.9, -7.800000000000001, -7.7, -7.6000000000000005, -7.5, -7.4, -7.300000000000001, -7.2, -7.1000000000000005, -7.0, -6.9, -6.800000000000001, -6.7, -6.6000000000000005, -6.5, -6.4, -6.300000000000001, -6.2, -6.1000000000000005, -6.0, -5.9, -5.800000000000001, -5.7, -5.6000000000000005, -5.5, -5.4, -5.300000000000001, -5.2, -5.1000000000000005, -5.0, -4.9, -4.800000000000001, -4.7, -4.6000000000000005, -4.5, -4.4, -4.3, -4.2, -4.1000000000000005, -4.0, -3.9000000000000004, -3.8000000000000003, -3.7, -3.6, -3.5, -3.4000000000000004, -3.3000000000000003, -3.2, -3.1, -3.0, -2.9000000000000004, -2.8000000000000003, -2.7, -2.6, -2.5, -2.4000000000000004, -2.3000000000000003, -2.2, -2.1, -2.0, -1.9000000000000001, -1.8, -1.7000000000000002, -1.6, -1.5, -1.4000000000000001, -1.3, -1.2000000000000002, -1.1, -1.0, -0.9, -0.8, -0.7000000000000001, -0.6000000000000001, -0.5, -0.4, -0.30000000000000004, -0.2, -0.1, 0.0, 0.1, 0.2, 0.30000000000000004, 0.4, 0.5, 0.6000000000000001, 0.7000000000000001, 0.8, 0.9, 1.0, 1.1, 1.2000000000000002, 1.3, 1.4000000000000001, 1.5, 1.6, 1.7000000000000002, 1.8, 1.9000000000000001, 2.0, 2.1, 2.2, 2.3000000000000003, 2.4000000000000004, 2.5, 2.6, 2.7, 2.8000000000000003, 2.9000000000000004, 3.0, 3.1, 3.2, 3.3000000000000003, 3.4000000000000004, 3.5, 3.6, 3.7, 3.8000000000000003, 3.9000000000000004, 4.0, 4.1000000000000005, 4.2, 4.3, 4.4, 4.5, 4.6000000000000005, 4.7, 4.800000000000001, 4.9, 5.0, 5.1000000000000005, 5.2, 5.300000000000001, 5.4, 5.5, 5.6000000000000005, 5.7, 5.800000000000001, 5.9, 6.0, 6.1000000000000005, 6.2, 6.300000000000001, 6.4, 6.5, 6.6000000000000005, 6.7, 6.800000000000001, 6.9, 7.0, 7.1000000000000005, 7.2, 7.300000000000001, 7.4, 7.5, 7.6000000000000005, 7.7, 7.800000000000001, 7.9, 8.0, 8.1, 8.200000000000001, 8.3, 8.4, 8.5, 8.6, 8.700000000000001, 8.8, 8.9, 9.0, 9.1, 9.200000000000001, 9.3, 9.4, 9.5, 9.600000000000001, 9.700000000000001, 9.8, 9.9, 10.0]

when i was doing i used round function to round of the values to 1 float precision.
thus my answers where being .1 more than the solution values.

while doing comparison:


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: 
        error=calculate_all_error(m,b,datapoints)
        if error<smallest_error:     
            best_m=m
            best_b=b
            smallest_error=error
print(best_m,best_b,smallest_error)

can someone tell me which is the right way of doing it.
also why its coming like that in possible_ms = [m * 0.1 for m in range(-100, 101)]

1 Like

You forget that you’re comparing by error, not the values themselves. What’s your error, and what’s the “solution”'s error, and by how much are you off?
Also, you’re using floats, meaning that you’re approximating. If you want an exact result, use a different representation.
You’re not off by 0.1, you’re off by approximately nothing.

hi @text7397673145, i have just done the Project and have same doubt here.

Also I assume the possible_ms list generated should be to 0.1 decimal.

I tested the same list comprehension in Pycharm and the result is similar.

[-10.0, -9.9, -9.8, -9.700000000000001, -9.600000000000001, -9.5, -9.4, -9.3, -9.200000000000001, -9.1, -9.0, -8.9, -8.8, -8.700000000000001, -8.6, -8.5, -8.4, -8.3, -8.200000000000001,…

I google the problem and got this answer , I think it might be helpful for you , link

rounding doesn’t restore lost accuracy
(also not sure why you’d want to)

you might mean that you want to keep accuracy, you can’t fix it afterwards, you fix it by not losing it in the first place

it doesn’t solve the “problem” though, which is having to pick a result from more than one that are equally good.

maybe you should consider the candidates and how good fits they all are. which ones are best?

the codes working fine . the problem is when i used rounding , my solution is exactly .1 more than the provided solution, with the minor difference that they are not rounding off for possible_ms and bs
what i am really confused is why ,do i have to use round function, logicaly i should get the same answer without rounding right. yet values like 1.7000000000000002, 1.8, 1.9000000000000001 are comming.
and this compound to result a +.1 in all my answers

thanks ,i tried using this logic also to round off and didnt get it so i used round().
but still in the end result will be off by .1

No, those differences are not in any sense adding up to 0.1

The problem is that you expect exact math, like school books.
Floats are not that.
You are making a decision based on an extremely small difference, much smaller than 0.1, and that decision is making you pick a different outcome

Don’t just look at the final outcome, look at what you did to reach it.
You are choosing an outcome based on error. You have two or more approximately equal errors, so picking a different one isn’t a particularly meaningful difference.

1 Like

Hey @text7397673145,

There is an error in the solution that was given. The correct values are 1.6 and 0.4, instead of 1.7 and 0.3. This is due as you pointed out to the precision rounding error when implementing the range function and dividing it by the float value.

Hope that helps!

Best regards,
@neuralx

The reason why it comes out like like that is because the computer uses 0 and 1, everything is in binary when it computes numbers or decimal numbers. This is why in computer arithmetic 1/3 is not equal to 0.3333333…, because it is just not possible to represent those quantities exactly using a binary system. So the computer tries to do a best possible approximation.

When implementing the range function above, a similar thing happens, and this is what occasionates the error you got. The way around it is to round the numbers that the list comprehension generates like so [round(n * 1.0 / 10) for n in range(-100, 101).

Best,
@neuralx

1 Like