I think I've written the right code in the function,but what is this error?

# code removed - mentor edit
# 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)

Output:
UnboundLocalError Traceback (most recent call last) d:\Courses\Machine Learning\Files(practice)\Files\home\jovyan\work\C1_W2_Linear_Regression.ipynb Cell 23 in <cell line: 5>() 2 initial_w = 2 3 initial_b = 1 ----> 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}') d:\Courses\Machine Learning\Files(practice)\Files\home\jovyan\work\C1_W2_Linear_Regression.ipynb Cell 23 in compute_cost**(x, y, w, b)** 26 f_wb=wx[i]+b 27 cost =(f_wb-y[i])**2 —> 28 cost_sum=cost_sum+cost 29 J_wb =(1/2m)*cost_sum 31 ### END CODE HERE ### UnboundLocalError : local variable ‘cost_sum’ referenced before assignment

1 Like

Hi!

The error mean that you don’t declare the variable cost_sum before using it.
As an example:

cost_sum = 0
cost_sum = cost_sum + 1

works, but:

cost_sum = cost_sum + 1

doesn’t because cost_sum wasn’t declared before use.
Also, I’m not sure why these lines:

x_train, y_train = load_data()
h = 0
h = h+ 2 * i

are in the code. Maybe they should be deleted.

Also don’t post code to the public forum, if a mentor needs to see the code they will ask for it in a private direct message.

2 Likes

I have the same error but those adjustments don’t make up for the error.