根据kmeans群集中心绘制边界线 [英] Drawing boundary lines based on kmeans cluster centres

查看:229
本文介绍了根据kmeans群集中心绘制边界线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对scikit学习很陌生,但想尝试一个有趣的项目.

我在英国有经度和纬度,我曾使用scikit学习KMeans类来创建聚类中心.为了可视化此数据,而不是将点作为聚类,我想在每个聚类周围绘制边界.例如,如果一个聚类是伦敦,另一个聚类是牛津,那么我目前在每个城市的中心都有一个点,但是我想知道是否存在一种方法可以使用这些数据来基于我的聚类创建边界线?

到目前为止,这是我创建集群的代码:

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

location1="XXX"
df = pd.read_csv(location1, encoding = "ISO-8859-1")

#Run kmeans clustering
X = df[['long','lat']].values #~2k locations in the UK
y=df['label'].values   #Label is a 0 or 1
kmeans = KMeans(n_clusters=30, random_state=0).fit(X, y)
centers=kmeans.cluster_centers_
plt.scatter(centers[:,0],centers[:,1], marker='s', s=100)

因此,我希望能够将上述示例中的中心转换为划分每个区域的线-这可能吗?

谢谢

Anant

解决方案

我想您是在谈论空间边界,在这种情况下,您应该遵循Bunyk的建议并使用Voronoi图[ http://nbviewer.jupyter.org/gist /pv/8037100 .

I'm quite new to scikit learn, but wanted to try an interesting project.

I have longitude and latitudes for points in the UK, which I used to create cluster centers using scikit learns KMeans class. To visualise this data, rather than having the points as clusters, I wanted to instead draw boundaries around each cluster. For example, if one cluster was London and the other Oxford, I currently have a point at the center of each city, but I was wondering if there's a way to use this data to create a boundary line based on my clusters instead?

Here is my code so far to create the clusters:

import pandas as pd
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans

location1="XXX"
df = pd.read_csv(location1, encoding = "ISO-8859-1")

#Run kmeans clustering
X = df[['long','lat']].values #~2k locations in the UK
y=df['label'].values   #Label is a 0 or 1
kmeans = KMeans(n_clusters=30, random_state=0).fit(X, y)
centers=kmeans.cluster_centers_
plt.scatter(centers[:,0],centers[:,1], marker='s', s=100)

So I would like to be able to convert the centers in the above example to lines that demarcate each of the regions -- is this possible?

Thanks,

Anant

解决方案

I guess you're talking about spatial boundaries, in this case you should follow Bunyk's recommendation and use a Voronoi Diagram [1]. Here is a practical demonstration of what you could achieve: http://nbviewer.jupyter.org/gist/pv/8037100.

这篇关于根据kmeans群集中心绘制边界线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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