首页 > 解决方案 > 等群聚类算法

问题描述

我有 300 个收集点,我需要根据 GEO COORDINATE 对其进行聚类。但是我所有的集群的上限应该是 8 下限是 5。我怎么能在 Python 中做到这一点。

请参阅图像以获取示例输出

标签: pythoncluster-computingsklearn-pandas

解决方案


我的问题 回答了你的问题。您需要更改position 数据GEO COORDINATEx,y使用Latitude Longitude.

dfcluster = DataFrame(position, columns=['x', 'y'])
kmeans = KMeans(n_clusters=4).fit(dfcluster)
centroids = kmeans.cluster_centers_
#for plot
# 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)

n=8
dfcluster1=dfcluster.head(n)
n=5
dfcluster2=dfcluster.tail(n)

此外,对于相同的组使用,Size Constrained Clustering 求解器

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

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_

推荐阅读