C3_W1_KMeans_Assignment X not defined

Hello everyone,

I get the following error after submitting the completed assignment (KMean, W1) during grading:

Cell #4. Can’t compile the student’s code. Error: NameError("name ‘X’ is not define

And it seems like I “failed” the assignment.

I completed the graded functions “def find_closest_centroids” but the next cell wants to print the variable X which is only loaded in the following cell. While doing the lab I could make it work by loading the cell first,

Load an example dataset that we will be using

X = load_data()

and then rerun the previous ones. I would get the “all test passed” messages when running the assignment. But at grading time the auto-grading software results in the error above, which is the same I would get if I ran the code in the written sequence.
How do you solve this issue? Did anyone had this same problem? It seems to be that there is an issue with the way the lab is coded, not with the student part of the code.
Please, help. The coursera support people recommended to post my problem here as they can’t solve issue related to assignment content.
Thank you very much

Thank you very much.

Hi @Tiziana_Cherubini

Try refreshing the kernel and rerun all the code before submitting the assignment for grading.

On the top of the menu bar of your opened notebook, select:

Kernel → restart and clear all output
Cell → run all

No one else has reported any issue with that.

1 Like


Thank you for your response. I did what you suggest, but I am still getting the same error (I am trying to attach a screen shot below… hopefully it will show).
And I do not see how that would have solved the issue. :grin:
The coding cell #3 (which is not editable, as it is part of the lab) wants to print X, which has not been loaded yet.
print(“First five elements of X are:\n”, X[:5])
print(‘The shape of X is:’, X.shape)

The command X = load_data(), is two cells down from the print one.
What am I missing here?

I honestly do not know how to move forward.
Please, help.


Hi @Tiziana_Cherubini ,

It looks like the cell for X = load_data() has moved below the print statements. So if you move the cell back to before the print statements, it should be fine.

:scream: :woozy_face:
I did not even know I could move non-student cells myself!! I must have dragged it without realize it! I guess I am not that familiar with Jupiter.
It works now. Thank you so much.