Home > Enterprise >  How to plot the cluster centers?
How to plot the cluster centers?

Time:12-20

By using this k-means clustering from scratch, How can i plot the initial random cluster centers for k=3 on the scatter plot in the photo? scatter plot with kmeans data and centers

Here is an attempt to show the given target names together with the kmeans approximation. Note that the order of the kmeans values is random. The larger background circles show the target names, the smaller circles, nicely grouped towards their centroid, come from kmeans.

from sklearn.datasets import load_iris

iris_data = load_iris()
x = iris_data.data[:, :2]

color_givens = ['magenta', 'gold', 'cyan']
for val, (name, color) in enumerate(zip(iris_data.target_names, color_givens)):
    plt.scatter(x[iris_data.target == val, 0], x[iris_data.target == val, 1],
                color=color, s=150, alpha=0.6, label=f'given {name}')

k = 3
points, centroids = kmeans(x, k)
colors_kmeans = plt.cm.Set1.colors
for val, color in zip(range(k), colors_kmeans):
    plt.scatter(centroids[val, 0], centroids[val, 1], facecolor='none', edgecolor=color, lw=3,
                s=150, label=f'centroid {val}')
for val, color in zip(range(k), colors_kmeans):
    plt.scatter(x[points == val, 0], x[points == val, 1], color=color, label=f'set {val}')
plt.xlabel(iris_data.feature_names[0])
plt.ylabel(iris_data.feature_names[1])
plt.legend(ncol=3)
plt.show()

showing given data vs kmeans approximation

  • Related