使用scikit软件包在Python中绘制群集区域的边界 [英] Plotting the boundaries of cluster zone in Python with scikit package

查看:104
本文介绍了使用scikit软件包在Python中绘制群集区域的边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我处理3个属性(x,y,value)中的数据聚类的简单示例.每个样本代表其位置(x,y)及其所属变量.

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.

我的代码发布在这里:

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)

该图显示在此处:

如何识别每个聚类区域的边界并勾勒出边界以增强可视化效果.

这是我手动绘制的插图.我需要识别聚类边界并用线画出它们.

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

我发现了一个有趣的问题

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

我尝试了以下子例程后:

After I tried the subroutine follows:

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

完成了!

推荐答案

集群区域实际上只是集群中心的Voronoi图. Scipy有一些给定点的用于计算Voronoi细胞的工具. 此页面包含一些示例关于如何执行此操作.

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.

这篇关于使用scikit软件包在Python中绘制群集区域的边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆