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?

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 =

Tasks 11 and 12

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)

@charm @ruby4988369339 @akoller55 @objectslayer41828
(Sorry for tagging you all but need ppl most likely active to see this and didn’t wana msg u all individually)

Hey guys where did you find this project? I got it suggested for code reviews but I don’t remember doing it. I only started like 2 weeks ago now in the AI/ML Engineering foundations path but im like 50% through and I have an empty folder on my computer with the name of this project but no files >.> I have been working with my git repositories a lot to set them up on VS Code/PyCharm/GitHub etc and I think I may have deleted it :sweat_smile: would appreciate direction if you remember where it is or even if u have a jupyter notbook file of the project (before you completed it or if you could erase your answers for me would appreciate it, can do it myself obv but don’t want to accidently see the answers). I don’t want to waste a bunch of productivity time looking back through all my courses to find it so even if u don’t see this for while I will probably still need to find it. Anyways yeah that’s it, thanks!

If you are still waiting to do code reviews yourselves, we can do it when I’m done if you’d like too. I imagine I’m probably a lot farther than when the project was introduced considering I don’t even remember it from max two weeks ago lol So I can probably handle code from wherever u guys are now.

(P.S I made a discord server for Data science/Machine learner students, I found it a bit hard to actually find people to work with actually free and actually where I was in the course, so I made it to make things easier and to share resources etc. There’s only bout 8 ppl so far only been 2 days but if you’re interested in joining pm on discord at Carpen-Them-Diemz#3114 or can ask me here. I plan on actually managing it and putting in my own time to add resources for students and help guide and work with anyone who I actually can lol So should be a good environment for learning when its up and running properly. Already put a playlist of my A.I/Data Science/Python youtube videos for studying, like 170 videos or so. Beginner/intermediate and advanced.)

Literally only just found out you can’t just search people up apparently >.> so here’s a link to the server if u want to join Singularity Surfers (AI/ML/DL-DataScience) . It only works for limited time, so if it doesnt work by time u see it nd u wana join just let me know ill resend.