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.
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
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.
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.
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.
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.
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.