python - 等群聚类算法
问题描述
我有 300 个收集点,我需要根据 GEO COORDINATE 对其进行聚类。但是我所有的集群的上限应该是 8 下限是 5。我怎么能在 Python 中做到这一点。
解决方案
我的问题 回答了你的问题。您需要更改position
数据GEO COORDINATE
和x,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 flow
orHeuristics
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_
推荐阅读
- delphi - 如何在 fastreport 中合并单元格?
- python - Web 抓取 Python 脚本不会给出错误或结果。是请求问题还是 XPATH 问题?
- html - 类型错误:结果集不可调用 - BeautifulSoup
- angular - 模板解析错误:无法绑定到“showMePartially”,因为它不是“allrecords-app”的已知属性
- azure - 从 Azure 门户中删除 Azure DevOps 项目
- react-native - React-native-navigation:如何启动 Playground 应用程序?
- go - 去使用 fsnotify 监视文件已更改不起作用
- r - 使用带有空间多边形的特定变量进行传单映射
- amazon-web-services - ELB 是否提高了 AWS 架构的可用性?
- python - Python 重新导出另一个同名脚本