0
import pandas as pd

import matplotlib.pyplot as plt

from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn import metrics
import numpy as np
%matplotlib inline
from scipy.spatial import ConvexHull
X =np.random.rand(10,10)

X_Name = ['a','b','c','d','e','f','g','h','i','j']
k = 3 
kmeans = KMeans(n_clusters=k)
clusters = kmeans.fit(X)
labels = clusters.labels_
centroids = clusters.cluster_centers_
pca = PCA(n_components=2, svd_solver='full')
X_ = pca.fit(X).transform(X)
colors = ['navy', 'turquoise', 'darkorange']
target_names = ['cluster1','cluster2','cluster3']
lw = 2
for color, i, target_name in zip(colors, [0, 1, 2], target_names):
    plt.scatter(X_[labels == i, 0], X_[labels == i, 1], color=color, 
    label=target_name)
for i, txt in enumerate(X_Name):
    plt.annotate(txt, (X_[i,0],X_[i,1]))
plt.legend(loc='best', shadow=False, scatterpoints=1)

I am getting image like this

But i want a image in this way

This code generate scatter plot for different clusters generated by K-means. But i want to generate boundaries around clusters so that it can be easily distinguished.

Community
  • 1
  • 1
  • 1
    I don't see you computing k-means in your code. Only the PCA. If you want to plot the cluster areas, see http://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_digits.html – CodeZero Feb 05 '18 at 10:55
  • You could construct and display a Voronoi diagram using the centroids of your clusters. See the scipy doc for that : https://docs.scipy.org/doc/scipy-0.17.0/reference/generated/scipy.spatial.Voronoi.html – Ben Feb 05 '18 at 10:56
  • @DizietAsahi I am bit confused how can I get the boundaries co-ordinates if I have some 50 clusters generated from K-Means –  Feb 05 '18 at 11:49
  • @datasailor : Sorry about it, I have updated the code –  Feb 05 '18 at 11:50

0 Answers0