我有一組 1000 多個 x、y 和 z 坐標,我想找出它們的聚類方式。我想設定一個最大距離,該距離將指定點屬于同一個集群,即如果該點與另一個點的歐幾里得距離小于 1,則演算法會將它們聚集在一起。我試圖在 python 上暴力破解,但收效甚微,有沒有人有任何想法或預先建立的演算法可以做類似的事情?
提前致謝
uj5u.com熱心網友回復:
你可以在 scikit-learn 模塊中找到相當多的聚類演算法:https ://scikit-learn.org/stable/modules/clustering.html
根據您對集群的特定定義,似乎sklearn.cluster.AgglomerativeClustering(n_clusters=None, distance_threshold=1)正是您想要的。
import numpy as np
from sklearn.cluster import AgglomerativeClustering
N = 1500
box_size = 10
points = np.random.rand(N, 2) * box_size
# array([[5.93688935, 6.63209391], [2.6182196 , 8.33040083], [4.35061433, 7.21399521], ..., [4.09271753, 2.3614302 ], [5.69176382, 1.78457418], [9.76504841, 1.38935121]])
clustering = AgglomerativeClustering(n_clusters=None, distance_threshold=1).fit(points)
print('Number of clusters:', clustering.n_clusters_)
# Number of clusters: 224
另一種方法是構建一個圖,然后使用例如模塊 networkx獲取圖的連接組件: networkx.algorithms.components.connected_components
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/313546.html
上一篇:給定字串表示確定矩陣維數的演算法
