#C1-w2: Practice Lab: Linear Regression: Programming Assignment: Name Error

Do you know how to debug this error?

Every time you open a notebook, you have to run all of the cells starting from the top. That is how the environment is created and the assets are loaded.

1 Like

me to but name error: name ‘x_train’ is not defined?

Did you run all of the cells after opening the notebook?

Because x_train is defined by running the cell in section “3 - Dataset”

Please post a screen capture image that shows the entire error message - but do not include your code.

Im having an error with my Xtrain being defined. I have already defined it in the first exercise but it will still show an error if i dont have the X and y train called. Any help please?

A couple of tips:

  • “x_train” is defined when you run the first cell in section “3 - Dataset”.

  • Every time you open the notebook, you must run all of the cells starting from the top.

  • Inside the compute_gradient() function, it doesn’t use the “x_train” variable. If you look at the function definition, it doesn’t menttion “x_train”.
    image

It uses the variable ‘x’. This is the local variable you should use when you add code to that function.

Hi! Thank you so much for your help. I am still a little confused do you mind explaining a little more please? Thank you so much! I have ran all the cells and it still has the error


I got rid of the X and y train in the second part of code but now it has this error

Python defines blocks of code using indentation.

image

These two lines of code are indented so that they are inside the for-loop. This means they will run on every trip through the loop, so you’ll be doing the “divide by m” too many times.

They should be out-dented (if that’s a word?) one level, so they are only executed once, after the for-loop ends.

A reminder: Please do not post your code on the forum, that’s not allowed by the Code of Conduct. If a mentor needs to see your code, we’ll contact you with instructions.

Ok thank you so much! I’m so sorry i took it out

No problem.