C3 W1 Lab 1 meaning of code

image

Hi, I am confused about what’s the meaning of this line of code?
Precisely, how it linked every pixel in idx with the corresponding centroids values?
In addition, the result shape of X_recovered is still (128, 128, 3). Does it mean python interpretor will still allocate the same memory for it as the original image since they have the same shape and no data type is specified?

Hello Sebastian,

idx is an array of 128 * 128 = 16384 numbers where each number is the I.D. of the centroid. Since centroid is the RGB color, centroids[idx, :] maps I.D. by I.D. from an I.D. to a color. For example,

centroids[idx[0], :] will give you the RGB color of the corresponding centroid for the zeroth pixel.

centroids[idx[1], :] will give you the RGB color of the corresponding centroid for the first pixel.

centroids[idx[:2], :] will give you the RGB colors of the corresponding centroids for the zeroth and the first pixel.

centroids[idx :] will give you the RGB colors of the corresponding centroids for each and every pixel.

That’s why the outcome of centroids[idx :] is basically that transformed image that also has 128 *128 pixels but represented by the centroids’ colors.

Same amount of memory I would say, but not the same memory.

Raymond

1 Like