# Not getting expected cost output

I have written compute_cost function and executed that with following test code.

# 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) <class ‘numpy.float64’> Cost at initial w: 0.099

AssertionError Traceback (most recent call last)
in
9 # Public tests
10 from public_tests import *
—> 11 compute_cost_test(compute_cost)

~/work/public_tests.py in compute_cost_test(target)
17 initial_b = 1.0
18 cost = target(x, y, initial_w, initial_b)
—> 19 assert cost == 2, f"Case 2: Cost must be 2 but got {cost}"
20
21 # print(“Using X with shape (5, 1)”)

AssertionError: Case 2: Cost must be 2 but got 0.5

For the same training data and initial w & b , I am getting cost at initial w ‘a wrong value’ . Please help.

Hi @Gouri_Halde
Welcome to the Community!

You can check the hint below also first you want to do a loop over number of training examples(m), second you want to compute the prediction by multiply the weight w with corsponding training examplex[…] , and add the bias term to the result, after that subtract the result with the corsponding real value y[…], and take the square of it by this equation cost =\sum_{i=0}^{m} (prediction - real\ value\ y)^2 , and after the loop, and before return divide the result with (2*m(number of training example))

Best Regards,
Abdelrahman

I did the same by defining a compute_cost function. But getting the wrong result.

@Gouri_Halde
send me the screen from your code in private message

@Gouri_Halde, you do not need to create your own functions.

yes I added the missing code only. But when test code was run on jupyter , it is giving me error and wrong result. Whereas same code when executed in spyder , I got the exact answer as given Expected Output.

@Gouri_Halde
The indentation of the loop isn’t correct(indentation = use tap space), also after the for loop,you want to divide the cost_sum by 2*m before return it

Got the result. corrected indentation.