What is the most straightforward way to visualize color-coded clusters along with the cluster centers?
There can be multiple ways, one can be -
- Plot the points with hue=cluster_number
- Plot the Centroid with a different markers
Code for 3 Clusters on 2 Iris Features -
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data  
y = iris.target
X=X[:,:-2]
X = (X - X.mean())/X.std()

def create_cluster(k=3):
    from sklearn.cluster import KMeans
    kmeans = KMeans(n_clusters=k)

    kmeans.fit(X)
    return kmeans

kmeans = create_cluster()
y_pred = kmeans.predict(X)
centroid = kmeans.cluster_centers_

_, ax = plt.subplots(1,1,figsize=(10,6))
color = [&quot;#e74c3c&quot;, &quot;#34495e&quot;, &quot;#2ecc71&quot;]
sns.scatterplot(X[:,0],X[:,1], hue=y_pred, palette=sns.color_palette(color),ax=ax)
sns.pointplot(centroid[:,0], centroid[:,1],markers='^',join=False,ax=ax)

$\hspace{2cm}$Output - Ignore the convergence quality
$\hspace{2cm}$