Help please - New to python, ML and Jupyter - Week 2 Assignment

Hello all,

I am incredibly excited to start my ML journey, however, I am brand new to ML, coding, Python, basically all of it.

I have passed all the tests but I am having trouble with the week 2 practice lab as it appears to require some coding skills.

In this section:

Compute cost with some initial values for paramaters w, b

initial_w = 2
initial_b = 1

cost = compute_cost(x_train, y_train, initial_w, initial_b)
print(type(cost))
print(f’Cost at initial w: {cost:.3f}')

Public tests

from public_tests import *
compute_cost_test(compute_cost)

I get this error:


NameError Traceback (most recent call last)
in
3 initial_b = 1
4
----> 5 cost = compute_cost(x_train, y_train, initial_w, initial_b)
6 print(type(cost))
7 print(f’Cost at initial w: {cost:.3f}')

in compute_cost(x, y, w, b)
29 for i in range(m):
30 # Your code here to get the prediction f_wb for the ith example
—> 31 f_wb = h = a * b + c
32 # Your code here to get the cost associated with the ith example
33 cost = (f_wb - y[i]) ** 2

NameError: name ‘a’ is not defined

I am not sure how to proceed or where to start. Any help would be much appreciated as I am banging my head against a wall here.

TY

Adding two screenshots as that may help

{moderator edit: code removed}

Hello There, I run into similar error messages before, then I realized that I needed to run/execute each line by pressing shift and Enter simultaneously on each line of code from the beginning

then another press “shift + Enter” again when you get to the next line…

you get the idea?

@Tdogg7272,
The error you’re seeing, “name a is not defined” tells you that you’re trying to use a variable called a without having defined what a is by including a line like: a = <some value>

In general, when you are writing code in a function, you can use the parameters passed into the function as variables, or define new variables by assigning something to them. For example, in the function compute_cost(x, y, w, b), x, y, w, and b are passed in, so you can use them as variables. Similarly, because there’s a line defining m:

m = x.shape[0] 

you can use the variable m as a variable in compute_cost on any line after it was defined. And you can use the variable i inside of the for-loop where it’s defined, like this:

for i in range(m):

But, you need to define something first before you can use it.

In your line where you have

f_wb = h = a * b + c

there are a couple of things to think about:

First, it’s unusual to have two = in one line like this. Especially while you’re new to Python, focus on having just one assignment per line.

Second, think about the assignment you’re making, what you’re trying to accomplish with it, and how you can do that with the variables you have available.

So, in this case, you’re trying to define the model’s prediction for an example, f_wb.

  • You can look at the instructions in the section above the function to see what that should be:
    𝑓𝑤𝑏(𝑥(𝑖))=𝑤𝑥(𝑖)+𝑏

  • How can you write that equation using the variables that are available to you in the compute_cost function?

In the assignment, you can click on the “Click for help” link in the cell after the compute_cost code cell for more hints. You can keep drilling in within the help to get more and more help, as needed.

One thing to be aware of - the help may give you a general example, like “h = a * b + c”, but you need to think about how that general example relates to what you want to do and then substitute in the appropriate variables to use instead of h, a, b, and c.

Good luck and happy learning!

Thank you @Tee_Bee I do this for each line/section. That is not the issue but appreciate the help.

Thank you @Wendy This is incredibly helpful and what I was looking for. I am plan to look at this again tomorrow morning. Where can I read more about this logic or approach that is not in the course or does the course cover all of this?

@Tdogg7272, please don’t post your code on the forum. That’s not allowed by the course community standards.

It’s fine to post the error and assert messages.

I’ve edited your posts to remove the images of the code.

@Tdogg7272,
As far as resources for learning more:

This course does assume students have some basic coding experience, but not necessarily any Python experience, so there are some optional labs sprinkled in the course to teach python-specific concepts as they come up.

Since students come to this class with a range of backgrounds, several of the mentors have gathered together a FAQ and other helpful resources in the MLS Resources category.

If you are new to coding, it might be helpful to take a little detour and get some basic Python training - maybe a beginner course. You’ll be the best judge of how much you’ll need and what works well for you. Definitely if you find yourself continually feeling lost with the coding assignments it’s time to try getting a little Python background. Try going to the MLS Resources category and search there for topics containing the word “Python”. You’ll find several helpful topics, including, Starting your Python Journey and Learning Python

Thank you @Wendy I will take a look at those resources. I am still not able to complete the assignment. The calculations throughout the course have all made sense and I was able to complete all the test and assignments prior to this one. This is the first blocker I have faced which is challenging. I want to continue on but don’t feel I should without figuring this out first.

Sounds good, @Tdogg7272. Take a look at the Python resources in particular. I suspect taking a little detour to get some background in Python will be a big help if you’ve have no coding experience before this.

I’m also happy to take a look at what you have now on this assignment to see if we can get you over this hump. Can you share here what error you’re getting now?

Sure. Here is where I am at now. I don’t want to violate the rules so Im just posting the error message. I understand I have an issue with line 7 correct?

1 Like

@Tdogg7272,
Here’s what we can see from this. After the line,

cost = compute_cost(x_train, y_train, initial_w, initial_b)

we have these two print statements:

print(type(cost))
print(f'Cost at initial w: {cost:.3f}')

You can see that after executing the line to calculate the cost by calling compute_cost, the first print statement output <class 'NoneType'> (you can see this just above the error text). The error for the second print statement also talks about NoneType - saying you can’t use that format string with a something of type None.

Both of those print statements are telling you that cost is None. That means compute_cost is returning None when it should be returning a number.

The last line of compute_cost is provided for you:

return total_cost

so, that means somehow, your code is setting total_cost to None, OR it is returning before getting to that last line that is provided for you.

Make sure you don’t have a return in your part of the code between

    ### START CODE HERE ###  
    
    ### END CODE HERE ### 

And make sure you are assigning a valid numeric value to total_cost

I have zero experience in coding, Python etc. I am following the concepts in the video lessons, but I still haven’t learned how to implement them. I am not clear what is expected of me in week 2 lab assignment. Please help.

Hi @Krishnaraj1!

In general, your job is to fill in the code in the sections marked by

    ### START CODE HERE ###  
    
    ### END CODE HERE ### 

The section above the code cell explains what your code should do, and if you have problems figuring out how to implement it, you can click on the “Click for help” link just below the code cell for hints.

If you’re still at a loss after looking at the instructions and hints in the assignment, please create a a new topic explaining specifically what you are stuck on and a mentor can help you. It’s good to create a new topic, rather than adding on to an existing one like this, both because it makes it easier for all mentors to notice someone has a new issue, and because it can make it easier to find for future students who have a similar issue.

Also, as mentioned above, this course is designed for students with basic coding experience, so you may want to consider getting a little background in Python. You can see these posts for some suggestions: [Preformatted text](https://community.deeplearning.ai/t/learning-python/257292) or Starting your Python Journey

See how it goes with this assignment. Happy learning!