With the provided python code, the reconstructed image fails to show the compressed image with 16 colors. I would like to know whether this is expected behavior or not. I am running C3_W1_KMeans_Assignment by launching the Coursera lab.
That is not the expected behavior.
I did not modify any of the Python code provided in Sections 4.1 (“Dataset”) and 4.2 (“K-Means on Image Pixels”). In particular, after running run_kMeans in Section 4.2 and printing idx[:5], I obtained the following output. This suggests that run_kMeans may not be functioning correctly for clustering for 16 colors.
Shape of idx: (16384,)
Closest centroid for the first five elements: [0 0 0 0 0]
Did you run the cells in Section 3 that create and initialize the centroids?
Yes, I did. I restarted the kernel and executed all the Python code blocks; however, I am still encountering the same issue. For reference, I am running this JupyterLab environment through Coursera. I wonder whether you could try this lab and advise me whether the outcome is different.
Hello @RSStephen_Kim,
My results for comparison:
Focusing on idx[:5], I have the same number 15 for the first five elements and you also have the same but a different number. Having the same number for the first five element is not the problem, but it is a problem if it is the same number for all elements which might be your case because your compressed image appears to have only one color.
Here is the distribution of my idx and you can run the same code to look at yours:

I understand that you have not modified section 4.2, but still it’s worthy to make sure K = 16 first because if K = 1, it will explain your observation:
Otherwise, perhaps there was something wrong in your implementation. I think a good first step to inspect it closer is to add a print to the find_closest_centroids function, run the K-means, and the distribution will keep changing:

If your code does not behave the same, try to inspect it further with more prints based on your observation. Debugging is part of the assignment and it is fun, so please do spend some time on it, but if you can’t spot the problem, show us all of your inspection prints and tell us how it goes.
Cheers,
Raymond
It appears that my implementation for “find_closest_centroids” is buggy in spite of the fact that the current implementation passes compute_centroids_test. The following is my output for run_kMeans. I wonder why this is the case.
K-Means iteration 0/9
(array([0]), array([16384]))
K-Means iteration 1/9
(array([0]), array([16384]))
K-Means iteration 2/9
(array([0]), array([16384]))
K-Means iteration 3/9
(array([0]), array([16384]))
K-Means iteration 4/9
(array([0]), array([16384]))
K-Means iteration 5/9
(array([0]), array([16384]))
K-Means iteration 6/9
(array([0]), array([16384]))
K-Means iteration 7/9
(array([0]), array([16384]))
K-Means iteration 8/9
(array([0]), array([16384]))
K-Means iteration 9/9
(array([0]), array([16384]))
<Figure size 576x432 with 0 Axes>
The following is my current implementation of find_closest_centroids(X, centroids):
[Code removed by mentor]
Hello @RSStephen_Kim,
It’s a fact that the tests cannot unveil all problems, and that also means it relies on us learners to discover and solve them. Fixing bugs in our code is not only part of the assignment but also an useful practical skill, which is why even though I can read all of your code and tell you where the problem is, my only next step would be to, based on your printout, make suggestions for what you can do.
Your printout shows that there is always only one idx value which is zero, and that is your lead. With the lead, a possible way is to look at the centroid initialization, add some print statements there, and make sure you get as many centroids as you ask, which is 16 different centroids here.
Good luck and I wish you will figure it out yourself quickly!
Cheers,
Raymond
PS: This place does not allow us to share exercise work, but don’t worry because I have removed it for you. ![]()




