# Reggies Linear Regression - 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! 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?

1 Like

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.

2 Likes

I just started learning python, I got to say I find this task immensely difficult and I had no idea how to solve it.

1 Like

Thanks–that never would have occurred to me, and I was pulling my hair out re: why I couldn’t get the expected answer.

1 Like

I know this is an old post. But I just worked through this project and came looking for the same answer. No where (that I can find) does it say to use those datapoints in the directions. Can someone fix this or explain?

I got m = 0.4 and b = 1.6

but when I use “m” in the nested loop, I get a different answer which is m = 0.6 and b = 1.4. Does anyone know why this is the case?

1 Like

I am not sure why this happens, but I am having the same issue. The only thing I found is that if you go back and divide possible_bs by 10 instead of 100 it happens. I think possible_bs/ms should both be divided by 100, but the solution divides possible_ms by 100 and bs by 10. Maybe I did something wrong, but clarification by someone else would be nice.

The OP of this thread had a way different value from what I go and what the solution was for m and b. Instead of 0.4 or 0.6 they got 0.3 for m and they got 1.7 instead of 1.4 or 1.6. After searching, that was a result of rounding errors or other mistakes, but the answer considered correct after the mistakes were taken into account was the current m = 0.4, b = 1.6.

For context on how I got my solution I divided by 100 in the list comprehension of possible_ms/bs because this would be the equivalent of a 0.1 increment (first division of 10), while also dividing the range(-100,101) by 10 (second division of 10, totaling 100) to make it the proper range of -10 to 10 with 0.1 increments.

No matter, if there could be further clarification by someone who got the m=0.4, b= 1.6 answer because I have been unable to find anywhere else on the forums that provide how that answer came about.

Thinking about the reason for issues, the 2 values may simply be equal and since it is only less than and not less than or equal to this may e the reason. By you switching the order one may appear before the other and be the contributor to the issue.

I just tried this and it was the exact issue. Only one solution is provided, but multiple are possible. m = 0.4, b = 1.6 is equal to 5, but so is m=0.6, b = 1.4 for the returned value of calculate_all_error().

I tested it with this code for the last few steps by adding temp. By looking at the bottom of the printed list my previous paragraphs can be proven. Adding that more than 1 solution is possible would be nice so there is less confusion if the people code in a different order.

temp =

for m in possible_ms:
for b in possible_bs:
if (calculate_all_error(m,b,datapoints) == smallest_error):
temp.append([m,b,smallest_error])
if calculate_all_error(m,b,datapoints) < smallest_error:
smallest_error = calculate_all_error(m,b,datapoints)
best_m = m
best_b = b
print(“Smallest Error: {smallest_error}”.format(smallest_error = smallest_error))
print(“Best M: {best_m}”.format(best_m = best_m))
print(“Best B: {best_b}”.format(best_b = best_b))
print('y = ’ + str(best_m) + ‘x’ + " + "+ str(best_b))

print(temp)