Update
Step 3: Update¶
For the updating part, we need to go through each label and find what the average value is. To do so we want to first remember how boolean indexing can work. For example, to find all points which have label equal to 0, we can get the following index then use it to find the mean of those points only.
In [20]:
#Create the boolean index
print(labels == 0)
In [21]:
#Index the data points by it
print(X[labels == 0])
print()
print("Length:")
print(len(X[labels == 0]))
Find the mean value across the columns.
In [22]:
#Find the centroid value
centroid = X[labels == 0].mean(axis=0)
print(centroid)
Not that we know how to grab the centroids, let's write some code to get all the centroids.
In [23]:
#Find the new colors
colors = [X[labels == l].mean(axis=0) for l in list(range(len(colors)))]
colors = np.vstack(colors)
print(colors)
print()
#Plot the new colors
plt.imshow([colors])
plt.show()
In [24]:
#Plot the image with the newly assigned colors
Y = colors[labels]
Y = Y.reshape(img_shape)
plt.imshow(Y)
plt.show()