Week 2 practice lab: Linear regression assertion error

I keep on encountering an assertion error and I don’t know how to fix it

Here is my code below:

{Moderator’s Edit: Solution Code Removed}

and this is the error I keep on getting:

Gradient at initial w, b (zeros): -0.04747762052631062 -0.007323908939747608 Using X with shape (4, 1)

AssertionError Traceback (most recent call last)
in
6 tmp_dj_dw, tmp_dj_db = compute_gradient(x_train, y_train, initial_w, initial_b)
7 print(‘Gradient at initial w, b (zeros):’, tmp_dj_dw, tmp_dj_db)

60 dj_dw, dj_db = target(x, y, initial_w, initial_b)
61 #assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
—> 62 assert dj_db == -2, f"Case 1: dj_db is wrong: {dj_db} != -2"
63 assert np.allclose(dj_dw, -10.0), f"Case 1: dj_dw is wrong: {dj_dw} != -10.0"
64

AssertionError: Case 1: dj_db is wrong: -0.6640625 != -2

Hey @Sanjana_Bansal,
Welcome to the community. You are dividing the cumulative sum at every step by `m`, whereas, you are only supposed to divide the sum only once, after the `for` loop has ended, i.e., after the total sum has been calculated. In other words, the following lines of code, should be outside the `for` loop:

``````dj_dw = dj_dw / m
dj_db = dj_db / m
``````

Let me know if this helps.

P.S. - It’s against the community guidelines to post code publicly. If a mentor needs to take a look at your code, he/she will ask you to DM your code.

Cheers,
Elemento

hi ,
I’m getting same error I also tried the solution given by sanjana but still it’s showing assertion eror can YOU PLEASE HELP.

Hi @ zbukhari!, did you initialize the variables `dj_dw = 0` and `dj_db = 0`?

I get the same ‘assertion’ error in this Lab2 Lab. --could it be a bug?

– Gradient at initial w, b (zeros): -0.04747762052631062 -0.007323908939747608 Using X with shape (4, 1)

AssertionError Traceback (most recent call last)
in
6 print(‘Gradient at initial w, b (zeros):’, tmp_dj_dw, tmp_dj_db)
7

60 dj_dw, dj_db = target(x, y, initial_w, initial_b)
61 #assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
—> 62 assert dj_db == -2, f"Case 1: dj_db is wrong: {dj_db} != -2"
63 assert np.allclose(dj_dw, -10.0), f"Case 1: dj_dw is wrong: {dj_dw} != -10.0"
64

AssertionError: Case 1: dj_db is wrong: -0.6640625 != -2

nbL dj_dw and dj_db initialized to zero

Hi @ Walu_John I’d be happy to help you with this error, could you send me (privately) your code so I can take a closer look at it?

Cheers,

hey @Enzo_Faliveni_Alzuet I solved the problem it’s working fine now.
Thanks

1 Like

Not able to understand the error.

TypeError 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)
24 cost_sum = 0
25 for i in range (m):
—> 26 f_wb = w * x(i) + b
27 cost = (f_wb - y[i]) ** 2
28 cost_sum = cost_sum + cost

TypeError: ‘numpy.ndarray’ object is not callable

1 Like

Hi, I have the same assertion error…

– Gradient at initial w, b (zeros): -0.03458597056701031 -0.006361340206185567 Using X with shape (4, 1)

AssertionError Traceback (most recent call last)
in
7 print(‘Gradient at initial w, b (zeros):’, tmp_dj_dw, tmp_dj_db)
8

60 dj_dw, dj_db = target(x, y, initial_w, initial_b)
61 #assert dj_dw.shape == initial_w.shape, f"Wrong shape for dj_dw. {dj_dw} != {initial_w.shape}"
—> 62 assert dj_db == -2, f"Case 1: dj_db is wrong: {dj_db} != -2"
63 assert np.allclose(dj_dw, -10.0), f"Case 1: dj_dw is wrong: {dj_dw} != -10.0"
64

AssertionError: Case 1: dj_db is wrong: -0.5 != -2

I have:

``````dj_dw = 0
dj_db = 0

dj_dw = dj_dw / m
dj_db = dj_db / m
``````

Hi @Mohammed_Hareeri In code make sure you initialize below codes
m = x.shape[0]
dj_dw = 0
dj_db = 0
correctly.
If you did and still got the error message personally I will see what i can do further.

Hi @Wan_Kee_C In code make sure you initialize the below codes
m = x.shape[0]
correctly.
If you did and still got the error message personally I will see what I can do further.

Hi @VIVEK_PATEL1

I have the following in my code:

``````# Number of training examples
m = x.shape[0]

# You need to return the following variables correctly
dj_dw = 0
dj_db = 0
``````

Assignment is done and syntax is alright.

1 Like

Hello @Mohammed_Hareeri,

Let’s read the error message. As indicated by an arrow in the Error Traceback, the error-triggering line is this one:

Combining this with the error message:

and a piece of background knowledge that when you call a function, you use this syntax `function_name(some_input_arguments)` which means you use a pair of parentheses after the function name. The only parentheses used in the error-triggering line is `x(i)` so here is the problem: you don’t index an array with parentheses, but square brackets.

Next time, try to narrow down the problem by reading the Error Traceback. This is an important skill since many people say debugging a solution can actually take more of your time than coding the solution.

Cheers,
Raymond

Thank you so much. I’m still not an expert in python and trying to learn how to program. Appreciating your assistance. I will try to resolve it[quote=“rmwkwok, post:13, topic:167307, full:true”]
Hello @Mohammed_Hareeri,

Let’s read the error message. As indicated by an arrow in the Error Traceback, the error-triggering line is this one:

Combining this with the error message:

and a piece of background knowledge that when you call a function, you use this syntax `function_name(some_input_arguments)` which means you use a pair of parentheses after the function name. The only parentheses used in the error-triggering line is `x(i)` so here is the problem: you don’t index an array with parentheses, but square brackets.

Next time, try to narrow down the problem by reading the Error Traceback. This is an important skill since many people say debugging a solution can actually take more of your time than coding the solution.

Cheers,
Raymond
[/quote]

Thank you Raymond! Appreciating your support and assistance. I’m not an expert in python and and I’m still learning. Will try to resolve the issue as you explained.

Best Regards,
Mohammed

You are welcome @Mohammed_Hareeri. Another suggestion for you is to google the error message and actually I can find helpful webpages for your problem. Both suggestions are good for beginners, only more challenging at the early stage but you will also grow your Python skills faster.

Good luck!
Raymond