Week 2 practice lab: Linear regression,

im trying to do the Week 2 practice lab: Linear regression

the code is showing many errors, and the code doesn’t allow me to edit the code:

please, can anybody help me?

and in te end appear:
Try Again
To Pass: 80% or higher

@Mariaclaraa,
These assert errors are coming from the test code that is testing if the code you wrote is working as expected.

In your first screenshot, compute_cost_test() is testing your compute_cost implementation. It is calling your compute_cost, passing in values that should return the value 2, but is returning 0.

In the second screenshot, compute_gradient_test() is testing your compute_gradient function. It is passing in values that should mean the shape of dj_db is -2, but your function is returning a different shape.

You can go to the File menu and choose Open… to find the public_tests.py to take a look at the test code to see what values the tests are passing in. You can also try adding some print statements in your functions temporarily to see what values are being passed in and to help you narrow down what is causing your code to return unexpected results

1 Like

C1_W2_Linear_Regression: Code throwing the error, could anyone help me??

o que aparece no

what appears in public_tests.py, is this:

import numpy as np

def compute_cost_test(target):
# print(“Using X with shape (4, 1)”)
# Case 1
x = np.array([2, 4, 6, 8]).T
y = np.array([7, 11, 15, 19]).T
initial_w = 2
initial_b = 3.0
cost = target(x, y, initial_w, initial_b)
assert cost == 0, f"Case 1: Cost must be 0 for a perfect prediction but got {cost}"

# Case 2
x = np.array([2, 4, 6, 8]).T
y = np.array([7, 11, 15, 19]).T
initial_w = 2.0
initial_b = 1.0
cost = target(x, y, initial_w, initial_b)
assert cost == 2, f"Case 2: Cost must be 2 but got {cost}"

# print("Using X with shape (5, 1)")
# Case 3
x = np.array([1.5, 2.5, 3.5, 4.5, 1.5]).T
y = np.array([4, 7, 10, 13, 5]).T
initial_w = 1
initial_b = 0.0
cost = target(x, y, initial_w, initial_b)
assert np.isclose(cost, 15.325), f"Case 3: Cost must be 15.325 for a perfect prediction but got {cost}"

# Case 4
initial_b = 1.0
cost = target(x, y, initial_w, initial_b)
assert np.isclose(cost, 10.725), f"Case 4: Cost must be 10.725 but got {cost}"

# Case 5
y = y - 2
initial_b = 1.0
cost = target(x, y, initial_w, initial_b)
assert  np.isclose(cost, 4.525), f"Case 5: Cost must be 4.525 but got {cost}"

print("\033[92mAll tests passed!")

print(“Using X with shape (4, 1)”)
# Case 1
x = np.array([2, 4, 6, 8]).T
y = np.array([4.5, 8.5, 12.5, 16.5]).T
initial_w = 2.
initial_b = 0.5
dj_dw, dj_db = target(x, y, initial_w, initial_b)
#assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
assert dj_db == 0.0, f"Case 1: dj_db is wrong: {dj_db} != 0.0"
assert np.allclose(dj_dw, 0), f"Case 1: dj_dw is wrong: {dj_dw} != [[0.0]]"

# Case 2
x = np.array([2, 4, 6, 8]).T
y = np.array([4, 7, 10, 13]).T + 2
initial_w = 1.5
initial_b = 1
dj_dw, dj_db = target(x, y, initial_w, initial_b)
#assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
assert dj_db == -2, f"Case 2: dj_db is wrong: {dj_db} != -2"
assert np.allclose(dj_dw, -10.0), f"Case 1: dj_dw is wrong: {dj_dw} != -10.0"

print("\033[92mAll tests passed!")

@Mariaclaraa,
So now you see the test code, your next step is to debug your code to figure out why it’s not producing the expected result.

For example, the error you got from compute_cost_test was from Case 2 of compute_cost_test(). Now that you see what parameters compute_cost_test() passed to your compute_cost function, you need to see why your compute_cost() returns 0 for those input values when it should return 2.

You can start by just looking at your code to see if it’s something obvious - like maybe it always returns 0 because you forgot to set the return value. If nothing stands out, you can temporarily add some print statements in your compute_cost() code to help you narrow down what’s going wrong. Put in the print statements and run the compute_cost_test() again to get insights as to what’s wrong.

I dont know how to debug this code, can you help me??

I tried to debub but i couldn’t

this appears:

Code Cell UNQ_C1: Function ‘compute_cost’ is incorrect. Check implementation.
Code Cell UNQ_C2: Function ‘compute_gradient’ is incorrect. Check implementation.
If you see many functions being marked as incorrect, try to trace back your steps & identify if there is an incorrect function that is being used in other steps.
This dependency may be the cause of the errors.

@Mariaclaraa, is there something specific you’re stuck on with the debugging, or are you unsure how to go about debugging this in general? There will be coding assignments in most of the weeks of these courses, so having a basic idea of how to debug is going to be critical. If you are new to programming, it’d probably be wise to pause this course while you take a quick intro to Python. Otherwise, the programming assignments are going to be frustrating.

If you are just stuck on something in particular, here are a few tips you can try to get started on your debugging:

1). Print statements in your code:

For example, the last part of the compute_cost function looks like this (with your code added between the ### START …### and ### END … # comments):

You can start your debugging by adding a print statement to print total_cost right after your last line of code. Re-run the cell to update the code definition, then re-run the following cell that calls compute_cost_test(). This will run your print statement whenever compute_cost_test() calls your compute_cost() function, so you’ll be able to see what value your function calculated.

If the problem is simple, for example, if you forgot to add a line

total_cost = <your code here>

then you might see the problem right away. If not, work backwards and add more print statements earlier in your function and repeat the process until you have narrowed down where the problem is.

2). Check the hints

Most of the coding cells in the assignments have a “Click for hints” link. You can click on this to get hints on how to code this part. If you need even more hints, you can drill in further by clicking on the “Click for more hints”.
Of course, it’s best if you can figure it out without the hints, but they are there to help give you that “ah ha” moment to see what you might have missed.

3). Be careful to indent properly

Python is picky about indentation. This is one of the most common things to overlook for people who are new to Python. It can lead to some weird-seeming errors.

4). If something still seems confusing after all of the above, go back and re-read the instructions for the section you’re working on and make sure you are implementing what it describes. Sometimes there are little nuances that you may have overlooked the first time.

I hope this helps!

1 Like

still I’m not able to debug it…Could you help?

probably I will drop out of this course,

Looks like I have passed the refund window, Would it be possible to switch over to other course??