Reggie's Linear Regression solution file error

I’m having a problem with the Reggie’s Linear Regression project
I’m running this Jupyter source file using VS Code.
In the solution file, In the part regarding the calculate_all_error code:

def calculate_all_error(m, b, points):
    total_error = 0
    for point in datapoints:
        point_error = calculate_error(m, b, point)
        total_error += point_error
    return total_error

This is supposed to be the correct solution. That’s what appears in the solution file, in that text box.
right after that, there’s another text box for testing this function:

#every point in this dataset lies upon y=x, so the total error should be zero:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(1, 0, datapoints))

#every point in this dataset is 1 unit away from y = x + 1, so the total error should be 4:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(1, 1, datapoints))

#every point in this dataset is 1 unit away from y = x - 1, so the total error should be 4:
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(1, -1, datapoints))

#the points in this dataset are 1, 5, 9, and 3 units away from y = -x + 1, respectively, so total error should be
# 1 + 5 + 9 + 3 = 18
datapoints = [(1, 1), (3, 3), (5, 5), (-1, -1)]
print(calculate_all_error(-1, 1, datapoints))

Yet, when I run the code in the textbox, I get a NameError.

I guess the NameError appears because the calculate_all_error function is written in a different text box. But this is the solution file. It’s supposed to run correctly and smoothly.
Does this problem also happen when using Jupyter Notebook? Is it happening because I’m using VS Code to run the Jupyter file? Is it OK to work on the Jupyter file through VS Code?

Any help would be appreciated.

Jupyter’s notebooks can take a little getting used to. The fact there’s a NameError suggests the cell in which calculate_error is defined has never been run. You’ll want to run through your notebook and execute each previous cell in turn (you can select and run multiple cells if you like).

Regarding variables and state your notebook will only hold the state of your python program whilst the background kernel is running, if you’ve never run the solution notebook beforehand or if you close the background sessions then you’ll need to run all the previous code to get back to the state you were in before.

There are alternatives to this such as pickling all your objects with various degrees of user friendliness e.g. dill (have a look around for some guidance) but try to not to rely on it. Making your notebook heavily reliant on hidden state means it is very difficult for others to use (or even for yourself is you’re busy with something else for a couple of days). It’s a much better option to make the notebook linear so the current state can be recreated if necessary or at the very least, inferred.

1 Like

Thank you so much, this has helped me alot!
I have only one question, what is a background kernel?

1 Like

You can probably find a better description online but in short the notebook is the pretty frontend that’s more like a text/code editor, it’s one of possible frontends. The kernel is your backend, the program that actually runs your code and deals with all the messages you send from the front end (and passes some back e.g. debugging, stdout and such). You can create numerous different kernels and run several at once e.g. a number of different Python versions, kernels for different programs like R, C++ and quite a few others. This separation allows lots of useful things like running kernels off-site on fancy optimised clusters and so on. Have a wee web search if you’re curious.

1 Like