Format error in Course 3/Week 2/Exercise 1

Hello all!

I’m working on the collaborative filtering assignment and I ran into this error when testing my cost function without regularization (Exercise 1):

When I check my value of J with print(J) it outputs [13.67072581] so the function seems to be computing the right value. The data type of J is float64, which I think is okay?

Has anyone else ran into this issue? Any advice would be greatly appreciated!

Hello @CBCasper,

Printing out J is a very good way. Thank you!

It’s a practice to study the error message. By the arrow, it happened in the print line when converting the numeric J into a string of this formatting 0.2f. According to the message, it sees J as a numpy.ndarray, and it is erroneous because it can’t convert an array to a string that satisfies the formatting string. That formatting string 0.2f expects a number, not an array. That’s the problem.

So, as asked by the DocString:


J is supposed to be a float, but yours is not. This is where you can start looking into. You are given many ndarray as inputs, and through your code, it finally becomes another ndarray. You need to figure out at which step you can expect it to be a scalar but it remains as an array.


1 Like

Hi Raymond,

Thanks for the helpful hints! I used ndarray.item to have my function to return a scalar and not an ndarray and that fixed the issue.

Great work fixing the problem, @CBCasper :clap: :clap: