如何解决等分组聚类算法
我有300个采集点,我需要根据GEO COORDINATE将其聚类。但是我所有集群的上限应该是8,下限是5。如何在Python中做到这一点。
解决方法
My question回答了您的问题。您需要更改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',ascending=True)
n=8
dfcluster1=dfcluster.head(n)
n=5
dfcluster2=dfcluster.tail(n)
另外,对于Size Constrained Clustering solver
以pip install size-constrained-clustering
或pip install git+https://github.com/jingw2/size_constrained_clustering.git
开头,您可以使用minmax flow
或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_
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。