Friday, 21 June 2019

How to draw a polygon edge on a K-Means cluster with Python?

I am trying to draw a polygon edge on a K-Means cluster shown below (fig_1).

enter image description here this piece of code plot everything except the edge.

df = pd.read_csv('https://raw.githubusercontent.com/MachineIntellect/dataset.ml/master/watermelon/watermelon_4_0.csv')
X = df.iloc[:,1:].to_numpy()
m0 = X[5]
m1 = X[11]
m2 = X[23]
centroids = np.array([m0, m1, m2])
labels = pairwise_distances_argmin(X, centroids)
m0 = X[labels == 0].mean(0)
m1 = X[labels == 1].mean(0)
m2 = X[labels == 2].mean(0)
new_centroids = np.array([m0, m1, m2])
plt.xlim(0.1,0.9)
plt.ylim(0, 0.8)
plt.scatter(X[:,0], X[:,1])
plt.scatter(new_centroids[:,0], new_centroids[:,1], c='r', marker = '+')

this seems to be inspiring

thanks to @ypnos, I have got this (fig_2) with scipy.spatial.ConvexHull

from scipy.spatial import ConvexHull, convex_hull_plot_2d

new_centroids = [X[labels == i].mean(0) for i in range(3)]
new_centroids = np.array(new_centroids)

plt.xlim(0.1,0.9)
plt.ylim(0, 0.8)
plt.scatter(X[:,0], X[:,1], )
plt.scatter(new_centroids[:,0], new_centroids[:,1], c='r', marker = '+')

for i in range(3):    
    points = X[labels == i]
    hull = ConvexHull(points)
    for simplex in hull.simplices:
        plt.plot(points[simplex, 0], points[simplex, 1], 'r-')

enter image description here

the question is that the edges pointed by the arrow in fig_1 are different from the correspondence in fig_2.

thanks to @ImportanceOfBeingErnest's reminder, scipy.spatial.ConvexHull may not be able to produce concave.

is there any other module/package to do this (concave)?

any hint would be appreciated.



from How to draw a polygon edge on a K-Means cluster with Python?

No comments:

Post a Comment