C1_W2_linear regression lab - definition of x in UNQ_C2

Hello everyone,
It’s my first post on this DeepLearning.AI platform.
I am completing the C1_W2 lab. The Code Cell UNQ_C1 is correct but not the UNQ_C2.
Here is the message :

Code Cell UNQ_C2: Function ‘compute_gradient’ is incorrect. Check implementation. I

In the code, the error message is the following :slight_smile: NameError Traceback (most recent call last)
46 for i in range(m):
47 # Your code here to get prediction f_wb for the ith example
—> 48 f_wb = w * x[i] + b
50 # Your code here to get the gradient for w from the ith example

NameError: name ‘x’ is not defined

Would someone be so kind as to look into my code for help please ? I do not manage to define x correctly.


1 Like

Dear @IreneVH,

Welcome to the Community.

Please check your code before line 46.
There is something wrong with the variable ‘x’.
It is not defined.
define variable ‘x’ before using.

1 Like

Thank you for your reply.
However, I still do not see where and what the mistake is precisely. May I send you my code so you could have a closer look ?

1 Like

Dear @IreneVH,

Yes you can personally send me your code.

1 Like

I am in cell 57 and its written

{moderator edit: code removed}

1 Like

I have even tried to add this

x_train, y_train = load_data()
x = x_train
y = y_train

but it still doesn’t work

1 Like

Do not add code like that to your assignment.

The ‘x’ variable is passed to the gradient_descent() function. You do not need to define it inside the function.

And the first few cells in the notebook are where the packages are imported and the data set is created.

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

Also, please do not post your code on the forum. That breaks the Code of Conduct. If a mentor needs to see your code, we’ll contact you with instructions.

Note that when @Girijesh, said “personally send me your code”, the meaning was that you should click on his user ID and use the “Message” button. That sends a private message, so your code isn’t shared on the forum.


Your issue is caused by an indentation problem here:

for i in range(m):
h = h + 2*i

Python creates executable blocks of code based on indentation. Everything after the
def gradient_descent(...):
statement must be indented, all the way to the return statement.


Also, the code h = h + 2*i doesn’t belong there. That’s just part of the Hint example.

1 Like