C3_W1_KMeans_Assignment 4 - Image compression with K-means

I don’t understand the mechanic behind those 2 lines of code. I tried to search for it on google but I didn’t find any answer. I guess the line at the first arrow is trying to implement new array X_recovered of the shape (16384, 3), but how is that possible while the shape of centroids is (16,3) and of idx is (16384,).
Please help me!
And can you teach the the way to search for the answer for those kinds of problems on google? :smile:

What googling keywords have you tried for each of those two lines?

I tried ‘inititate an array from an array numpy’. But the only answer I got is some function such as np.zeros, np.arange, …

I see. Obviouisly there is no general rules for how to google. I have tried this “numpy variable square brackets”, and the first two results are pretty relevant.

Note that my keywords do not guess the code’s intention, instead, it is describing centroids[idx, :]. If you can’t read the intention from the lab’s text, then guessing it is dangerous. Below are my suggestions:

After reading some googled results, the next thing to do is to try things out. Add a new code cell, write some managable code, print some variables’ content, and make sure things work as you expect.

With confidence built up on the managable code, print some variables of those two lines in question, and see if things work as expected.

Lastly, combining your understanding of the code and the lab’s text, try to come up with some idea of what the code is doing.

Good luck!

1 Like

I’ve read the two link you sent me but sadly I still cannot understand and find the answer, so I tried some experiments in the lab. It came up with this contradiction. I made array ‘a’ and array ‘b’ with somewhat same distinct as ‘idx’, ‘centroids’ respectively, then I created a new array ‘c’ with the same way as I create the X_recovered array and my guess was ‘c’ has the shape of (10,3). However this bug came up! I’ve tried for hours but still stuck at this :sob:

Hello @therealTin,

Googling is a trial and error thing. Being able to find useful content for yourself is a skill for life. Don’t give up. I can suggest, but you are also on your own. :wink:

Now that you see the error message, what have you done to it, particularly for the last line? Did you read it? Did you google? Did you experiment? The last line looks quite easy to read and understand. Maybe you can try to explain the meaning of “index 4 is out of bounds for axis 0 with size 4” here?

An hour, a day, or even a month is nothing if the skill you gain will be useful for your career life. I am glad to hear that you have been willing to invest time in your skills.


1 Like