在地图上的聚类位置,其中每个聚类具有相等数量的点 [英] Clustering positions on a map where each cluster has an equal number of points

查看:201
本文介绍了在地图上的聚类位置,其中每个聚类具有相等数量的点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图上有特定的点,我需要将它们分为相同大小的不同群集,最后一个群集可以是count %n.我阅读了这些答案 1 2 3 ,但这无济于事.我尝试了不同的方法,但是它们都不起作用.在此代码中,我指定了n_clusters=4,因为这是集群中可以对其进行排序的最佳编号,并且可以从排序的点中获取n最佳点,然后我将遍历所有点.例如,我需要将图中所示的32点聚类为4个聚类,并且每个聚类都具有8个聚类

I have specific points on the map and I need them to be grouped to different clusters with the same size and the last cluster can be count %n. I read these answers 1, 2, and 3 but it does not help. I have tried different way but none of them works. In this code, I specific the n_clusters=4 because this is the best number of a cluster that I can sort them and take n best points from the sorted point, and then I will go through all the points. For example, I need the 32 points that shown in the figure to be cluster to 4 clusters and every cluster has 8 points

dfcluster = DataFrame(position, columns=['x', 'y'])
kmeans = KMeans(n_clusters=4).fit(dfcluster)
centroids = kmeans.cluster_centers_

# plt.scatter(dfcluster['x'], dfcluster['y'], c=kmeans.labels_.astype(float), s=50, alpha=0.5)
# plt.scatter(centroids[:, 0], centroids[:, 1], c='red', s=50)
# plt.show()
dfcluster['cluster'] = kmeans.labels_
dfcluster=dfcluster.drop_duplicates(['x', 'y'], keep='last')
dfcluster = dfcluster.sort_values(['cluster', 'x', 'y'], ascending=True)
# d=pd.DataFrame()
# m = pd.DataFrame()
# n=8
# for x in range(4) :
#     m= dfcluster[dfcluster.cluster == x]
#
#
#     if len(m) > int( n /2)-1:
#         m=m.head(int(n/2)-1)
#         # for idx, row in m.iterrows():
#         #     print("code3 group",  "=", row['cluster'])
#         d=d.append(m,ignore_index = True)
#
#     else :
#         d=d.append(m,ignore_index = True)
#
#
# if len(d)>=n:
#     dfcluster = d
# dfcluster.groupby('cluster').nth(n))
dfcluster=dfcluster.head(n)
i=0
if (len(dfcluster )< n):
   change_df()

推荐答案

我发现此模块使用

I found this module that uses the Same Size Constrained K-Means Heuristics: Use Heuristics methods to reach the same size clustering where it gives the same size of the group.

我以pip install size-constrained-clusteringpip install git+https://github.com/jingw2/size_constrained_clustering.git开头,您可以使用minmax flowHeuristics

I start with pip install size-constrained-clustering or pip install git+https://github.com/jingw2/size_constrained_clustering.git and you can use minmax flow or Heuristics

n_samples = 2000
n_clusters = 3
X = np.random.rand(n_samples, 2)

model = equal.SameSizeKMeansMinCostFlow(n_clusters)

#model = equal.SameSizeKMeansHeuristics(n_clusters)
model.fit(X)
centers = model.cluster_centers_
labels = model.labels_

这篇关于在地图上的聚类位置,其中每个聚类具有相等数量的点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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