Reggies Linear Regression - Help

Please Help -
Python3
Reggies Linear Regression Project:

My code is identical to the solution but yet in part 2, I get two different solutions. I find this really troubling! Can anyone shed any light??

Mine:

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)
0.26 1.74 4.999999999999999

Solution:

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)
0.30000000000000004 1.7000000000000002 4.999999999999999

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

1 Like

Hello … I have used different methodology because i am unable to understand use of print(“inf”) in solution. I have used min function to find min value in list and used two loops instead of one.

you can see in below mentioned screenshot.

The topic seems to be dormant for quite a time, but still have a question - where does the datapoints = [(1, 2), (2, 0), (3, 4), (4, 4), (5, 3)] line come from? Didn’t see it any where, but the solution? Does this mean that you should just imagine it from the start?

I think it’s supposed to be in that code line already but is missing. I only realized it when I came here trying to figure out why the answer I got was different than the one the markdown claimed I should get (it was using the last test set of datapoints and not this “correct” set.)

Sorry, you’re absolutely right, it’s right there and I just missed it! Thank you!

I’m very confused about notebook. Are we supposed to create our code within the notebook? If so, how do we run it? I see there are buttons next to the code but they don’t seem to do anything.

Try either ctrl/(shift) + enter on Win or cmd+enter on a Mac and it should work, Good luck!

That works, thank you!!

I also ended up adding in the code line listing the data points from the solution notebook into my solution.

datapoints = [(1, 2), (2, 0), (3, 4), (4, 4), (5, 3)]

I’ll add in another possible solution you may end up with if you have defined your possible_ms and possible_bs differently.

I used:

possible_ms = [i/10 for i in range(-100, 101, 1)]
possible_bs = [i/10 for i in range(-200, 201, 1)]

rather than the proposed solution method:

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

As a result, it gives me the best m as 0.4 and the best b as 1.6.
I think that there is a slightly different floating point number from dividing by 10 vs multiplying by 0.1 that is leading to n error of 4.999999 for these numbers rather and a 5.000000 error for m = 0.3 and b = 1.7.