Well, I have been trying for about 2h. It looks simple, but I always get an error from the test code. I have tried the loop implementation and the vectorized one.
The loop implementation:
inside a for:
cost_sum = 0
Please be careful that you don’t post your code on the forum. Thanks.
For the iterative method, check whether you have the correct indentation for all your lines of code. Python defines code blocks using indentation. For example, it’s very easy to misplace a line of code by having it inside a for-loop when it shouldn’t be.
Personally I prefer the vector method. It’s just a few lines of code, involving a dot product, computing a vector of the errors, summing the element-wise squares of the errors, then dividing by (2*m).
You can gain some knowledge if you open the public_tests.py file and look at the data that is used for this compute_cost_test(). You can calculate the expected results by hand, and compare it with your code (by temporarily adding some print() statements to your code).