Reggie linear regression Python 3

Hello,
I am getting a name error and I don’t know why
http://localhost:8888/notebooks/Reggie’s%2BLinear%2BRegression/Reggie_Linear_Regression_Skeleton.ipynb

def calculate_error(m, b, point):
x_point, y_point = point
y = m*x_point + b
distance = abs(y - y_point)
return distance

We can’t see the file you included

And neither can we see the call arguments, but we may assume they are (float, float, tuple of floats).

Myself would have been content to stick with (x, y) for less verbosity. If we know the math, we know the symbols.

y = mx + b

where,

m = (y2 - y1) / (x2 - x1)

(in framing carpentry this is called rise over run)

and,

b = A_CONSTANT

namely, the y-intercept.

What’s throwing me off is the variable distance. It should resolve to two vectors, change in y, and change in x. Those values can be resolved to a vector length using the distance formula, which is essentially Pythagorean so that,

Delta x squared + Delta y squared all raised to the one-half

equals distance between (x1, y1) and (x2, y2).

I struggled with the same issue and maybe you did the same mistake as I did.

I suddenly remembered reading something about Jupyter Notebook not running all the code automatically, so I looked through the menus and tried Cell > Run All - and the name error disappeared! It seems that the code was not able to access calculate_error that I defined earlier in the notebook, but it became accessible when I told the notebook to run all the code.

(after replying, I realized you asked this a year ago, not this December. Hopefully you figured it out, and maybe some other lost souls could use the answer)

I’m having a lot of problems with this exercise, I need someone experienced to tell me what’s wrong. I think it might be some problem with Jupyter itself, since sometimes when I run the same code I get errors, sometimes I don’t. I managed to get to the point when we calculate best possible m and b. My code looks like this:

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
   bestb = b
   smallest_error = error
            
                  
print(best_m)
print(best_b)
print(smallest_error)

And my console prints are as follows;
0
0
inf

The solution doesn’t help since even when I use code from the solution I get the same result (this is why I suspect there’s something wrong with Jupyter)

When I copy whole code to online python IDE I get the result of -10, 0, 79. How is this even possible?

def get_y(m, b, x):
  y = m*x + b
  return y

print(get_y(1, 0, 7) == 7)
print(get_y(5, 10, 3) == 25)

def calculate_error(m, b, point):
    x_point = point[0]
    y_point = point[1]
    y = get_y(m, b, x_point)
    return abs(y - y_point)
    

def calculate_all_error(m, b, points):
    errors = 0
    for point in points:
        errors += calculate_error(m, b, point)
    return errors
    
possible_ms = (m * 0.1 for m in range(-100, 101))
possible_bs = (b * 0.1 for b in range(-200, 201))

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
   bestb = b
   smallest_error = error
            
                  
print(best_m)
print(best_b)
print(smallest_error)
    

This is THE SAME FRIGGIN CODE! Jupyter gives me 0, 0, inf and other IDE -10, 0 and 79. What is going on here? Am I just too stupid to even dream of programming? This is so demotivating :frowning:

    bestb = b

Could be the source of at least one problem.

Ok, when I changed it to best_b it went back to

TypeError: ‘<’ not supported between instances of ‘float’ and ‘function’

This is messed up and it’s messing with my head. It’s the third day I’m trying to solve this exercise, should I just acknowledge that it’s broken and skip it?

And also my mistake doesn’t explain why I get -10, 0, 79 when I run official solution.

Please post the exercise URL so we can follow up on this problem.

I cannot, we’re doing it outside Codecademy on jupyter, it’s called Reggie Linear Regression.

There must be a project page where the original download is.

https://www.codecademy.com/courses/learn-python-3/informationals/python3-reggies-linear-regression

1 Like

If jupyter isn’t performing some kind of meaningful task for you (it probably isn’t) then yeah just run it as a regular plain text source file. If anyone was to ever give me a jupyter notebook the first thing I’d be doing is converting it to text because the only thing jupyter accomplishes for me is to prevent me from using my code editor.

by jupiter notebook you mean *.ipynb files? Yeah, I tried doing it in pycharm but it was all weird looking and non-executable

jupyter has a conversion command, turns text into comments and code into … code

or just copy it if it’s not much

the ipynb text format can be edited as well, it’s not that bad (it might be if editing one line at a time, but hey that’s where more powerful text editors can help)