Course 3 / Week 1: Getting NAN error


In the Course 3 / Wk 1 programming assignment, section 4.2 (image compression), I’m getting a “ValueError: cannot convert float NaN to integer”.

I added a print to my “find_closest_centroids” function, and here’s what I get right before the crash.

My code for the “find_closest_centroids” function seems correct, but maybe it has a bug? Here’s the code:

{mentor edit: code removed}


Please don’t post your code on the forum. That’s not allowed by the Code of Conduct.

If a mentor needs to see your code, we’ll contact you with instructions.

Since “distance” is going to be a list of floats - returned by np.linalg.norm() - initializing it to a vector of zero integers is not correct. Just make it a null array.

You have also deleted a line of code that you need - the one that appends items to the “distance” list.

My apologies. Thanks for your help.

I am getting an error message for the initial code line that reads “no module name utils”…input the code exactly as stated.

This thread is about an error called “Getting NAN”.
Are you having that issue?

I simply went to the week 1 forum. Is there one I can post in for this error?

Typically it’s best if you start a new thread.

But we can cover your question here, since it’s already started.

There are two assignments in Week 1: K-means and Anomaly Detection. Which are you asking about?

It would help if you post a screen capture image that shows your workspace and the error message. That will answer a lot of my questions.


That image doesn’t show your entire workspace. I wanted to know if you were working on Coursera Labs, or if you’re working locally or on some other platform.

I am in the collaborative Numpy space

Sorry, I do not know what that is.

The labs are intended to be used on Coursera Labs. If you want to set up a different environment, that’s on you to accomplish. You would need to install all of the assignment assets. You can get them by using the “Lab Files” tab on the notebook page, then using “Download all files”.