Plotting the boundaries of cluster zone in Python with scikit package

江枫思渺然 提交于 2019-12-09 05:51:03

问题


Here is my simple example of dealing with data clustering in 3 attribute(x,y,value). each sample represent its location(x,y) and its belonging variable.

My code was post here:

x = np.arange(100,200,1)
y = np.arange(100,200,1)
value = np.random.random(100*100)

xx,yy = np.meshgrid(x,y)
xx = xx.reshape(100*100)
yy = yy.reshape(100*100)
j = np.dstack((xx,yy,value))[0,:,:]

fig = plt.figure(figsize =(12,4))
ax1 = plt.subplot(121)
xi,yi = np.meshgrid(x,y)
va    = value.reshape(100,100)
pc = plt.pcolormesh(xi,yi,va,cmap = plt.cm.Spectral)
plt.colorbar(pc)

ax2 = plt.subplot(122)
y_pred = KMeans(n_clusters=12, random_state=random_state).fit_predict(j)
vb = y_pred.reshape(100,100)
plt.pcolormesh(xi,yi,vb,cmap = plt.cm.Accent)

The figure are presented here:

How to identify the boundaries of each cluster zone and outline them to intensify the visualization effect.

PS

Here is an illustration I plot manually. To identify the clustering boundaries and depict them in lines is what I need.

PPS

I found an interesting question here trying to draw the boundaries of cluster area in R

Update

After I tried the subroutine follows:

for i in range(n_cluster):
    plt.contour(vb ==i contours=1,colors=['b']) 

It's done!


回答1:


The cluster zones are actually just a Voronoi diagram of the cluster centers. Scipy has some tools for computing Voronoi cells given a set of points. This page has some examples on how you can do this.



来源:https://stackoverflow.com/questions/37707432/plotting-the-boundaries-of-cluster-zone-in-python-with-scikit-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!