# Week 2 practice lab: Linear regression assertion error

**URL:** <https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307>\
**Category:** Supervised ML: Regression and Classification\
**Tags:** week-module-2\
**Created:** [July 22, 2022, 8:39am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307 "2022-07-22T08:39:48Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sanjana\_Bansal](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@Sanjana\_Bansal](https://community.deeplearning.ai/u/Sanjana_Bansal)\
**Post date:** [July 22, 2022, 8:39am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/1 "2022-07-22T08:39:48Z")

</div>

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)  
----\> 8 compute\_gradient\_test(compute\_gradient)

~/work/public\_tests.py in compute\_gradient\_test(target)  
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

---

<div class="post-metadata">

**Author:** ![Elemento](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/elemento/32/854_2.png) [@Elemento](https://community.deeplearning.ai/u/Elemento)\
**Post date:** [July 22, 2022, 9:29am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/2 "2022-07-22T09:29:24Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![zbukhari](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/zbukhari/32/32657_2.png) [@zbukhari](https://community.deeplearning.ai/u/zbukhari)\
**Post date:** [November 5, 2022, 5:28am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/3 "2022-11-05T05:28:45Z")

</div>

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.

 ![HHHHHHH](https://global.discourse-cdn.com/dlai/original/3X/d/a/da774cdccd2b98c639716a76789dc71cbc7fa943.png)

---

<div class="post-metadata">

**Author:** ![Enzo\_Faliveni\_Alzuet](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/enzo_faliveni_alzuet/32/30842_2.png) [@Enzo\_Faliveni\_Alzuet](https://community.deeplearning.ai/u/Enzo_Faliveni_Alzuet)\
**Post date:** [November 5, 2022, 4:28pm UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/4 "2022-11-05T16:28:21Z")

</div>

Hi @ [zbukhari](https://community.deeplearning.ai/u/zbukhari)!, did you initialize the variables `dj_dw = 0` and `dj_db = 0`?

---

<div class="post-metadata">

**Author:** ![Walu\_John](https://avatars.discourse-cdn.com/v4/letter/w/e8c25b/32.png) [@Walu\_John](https://community.deeplearning.ai/u/Walu_John)\
**Post date:** [November 6, 2022, 11:45am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/5 "2022-11-06T11:45:42Z")

</div>

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  
----\> 8 compute\_gradient\_test(compute\_gradient)

~/work/public\_tests.py in compute\_gradient\_test(target)  
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

---

<div class="post-metadata">

**Author:** ![Enzo\_Faliveni\_Alzuet](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/enzo_faliveni_alzuet/32/30842_2.png) [@Enzo\_Faliveni\_Alzuet](https://community.deeplearning.ai/u/Enzo_Faliveni_Alzuet)\
**Post date:** [November 6, 2022, 6:40pm UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/6 "2022-11-06T18:40:57Z")

</div>

Hi @ [Walu\_John](https://community.deeplearning.ai/u/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,

---

<div class="post-metadata">

**Author:** ![zbukhari](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/zbukhari/32/32657_2.png) [@zbukhari](https://community.deeplearning.ai/u/zbukhari)\
**Post date:** [November 7, 2022, 11:22am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/7 "2022-11-07T11:22:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mohammed\_Hareeri](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Mohammed\_Hareeri](https://community.deeplearning.ai/u/Mohammed_Hareeri)\
**Post date:** [November 7, 2022, 11:50am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/8 "2022-11-07T11:50:14Z")

</div>

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

* * *

---

<div class="post-metadata">

**Author:** ![Wan\_Kee\_C](https://avatars.discourse-cdn.com/v4/letter/w/8edcca/32.png) [@Wan\_Kee\_C](https://community.deeplearning.ai/u/Wan_Kee_C)\
**Post date:** [November 7, 2022, 6:29pm UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/9 "2022-11-07T18:29:07Z")

</div>

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  
----\> 9 compute\_gradient\_test(compute\_gradient)

~/work/public\_tests.py in compute\_gradient\_test(target)  
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

```

---

<div class="post-metadata">

**Author:** ![VIVEK\_PATEL1](https://avatars.discourse-cdn.com/v4/letter/v/8797f3/32.png) [@VIVEK\_PATEL1](https://community.deeplearning.ai/u/VIVEK_PATEL1)\
**Post date:** [November 8, 2022, 4:00am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/10 "2022-11-08T04:00:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![VIVEK\_PATEL1](https://avatars.discourse-cdn.com/v4/letter/v/8797f3/32.png) [@VIVEK\_PATEL1](https://community.deeplearning.ai/u/VIVEK_PATEL1)\
**Post date:** [November 8, 2022, 4:03am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/11 "2022-11-08T04:03:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Wan\_Kee\_C](https://avatars.discourse-cdn.com/v4/letter/w/8edcca/32.png) [@Wan\_Kee\_C](https://community.deeplearning.ai/u/Wan_Kee_C)\
**Post date:** [November 8, 2022, 5:19am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/12 "2022-11-08T05:19:49Z")

</div>

Hi @VIVEK_PATEL1  
Thank you for your response.

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.

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [November 8, 2022, 6:05am UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/13 "2022-11-08T06:05:44Z")

</div>

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:

> [@Mohammed\_Hareeri](#):
>
> —\> 26 f\_wb = w \* x(i) + b

Combining this with the error message:

> [@Mohammed\_Hareeri](#):
>
> TypeError: ‘numpy.ndarray’ object is not callable

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

---

<div class="post-metadata">

**Author:** ![Mohammed\_Hareeri](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Mohammed\_Hareeri](https://community.deeplearning.ai/u/Mohammed_Hareeri)\
**Post date:** [November 8, 2022, 7:07pm UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/14 "2022-11-08T19:07:00Z")

</div>

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:

> [@Mohammed\_Hareeri](#):
>
> —\> 26 f\_wb = w \* x(i) + b

Combining this with the error message:

> [@Mohammed\_Hareeri](#):
>
> TypeError: ‘numpy.ndarray’ object is not callable

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]

> [@rmwkwok](#):
>
> 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:
> 
> > [@Mohammed\_Hareeri](#):
> >
> > —\> 26 f\_wb = w \* x(i) + b
> 
> Combining this with the error message:
> 
> > [@Mohammed\_Hareeri](#):
> >
> > TypeError: ‘numpy.ndarray’ object is not callable
> 
> 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

---

<div class="post-metadata">

**Author:** ![Mohammed\_Hareeri](https://avatars.discourse-cdn.com/v4/letter/m/e0b2c6/32.png) [@Mohammed\_Hareeri](https://community.deeplearning.ai/u/Mohammed_Hareeri)\
**Post date:** [November 8, 2022, 7:09pm UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/15 "2022-11-08T19:09:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [November 8, 2022, 11:35pm UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/16 "2022-11-08T23:35:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rmwkwok](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/rmwkwok/32/126977_2.png) [@rmwkwok](https://community.deeplearning.ai/u/rmwkwok)\
**Post date:** [November 8, 2022, 11:35pm UTC](https://community.deeplearning.ai/t/week-2-practice-lab-linear-regression-assertion-error/167307/17 "2022-11-08T23:35:11Z")

</div>


