C1_W2_Linear regression exercise 1


I’m getting this error when i run the test code to check if my implementation is correct

<class 'numpy.ndarray'>
TypeError                                 Traceback (most recent call last)
<ipython-input-23-335d76763737> in <module>
      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}')
      9 # Public tests

TypeError: unsupported format string passed to numpy.ndarray.__format__

Hello @Mohamed_Said1,

The test code caught an error from your compute_cost function. Here is how we can read it:

  1. The print(type(cost)) command prints the type of the variable cost which is a numpy.ndarray. It means cost contained an array.

  2. The f in the .3f there expects cost to be of the type float

  3. If you read the DocString of the compute_cost function, and look at the Returns, it also expects the type to be float

  4. In short, it expects the function to return a float type value, but your function returned an array. This is the discrepancy that you need to figure out why and resolve it.

    Since the formula for computing the cost will sum over all samples, the computed cost should therefore be a scalar (of type float) instead of an array.

Good luck debugging!


PS: in subsequent assignment exercise, it follows the same mode: you implement an exercise in a graded code cell which does not necessarily complain if something is wrong, but if there is indeed something wrong, it will complain in the test cell. In this case, you need to go back to the graded code cell, find out the bug, and fix it.

  • You might use the error traceback for debugging hint like what I have shown you in above.
  • Underneath each exercise’s graded code cell, there is a Green “Hint” button. You might click that button to show tips that are available for learners. Note also that, after clicking the green “Hint” button, it might or might not further shows some smaller blue “hint” button for further hints, so don’t miss them out.

Thanks @rmwkwok for your reply. Already debugged it, feels good :slight_smile:

Great work, @Mohamed_Said1!


1 Like