如何根据大小过滤DBSCAN产生的群集?

时间:2019-03-15 16:45:24

标签: python machine-learning scikit-learn unsupervised-learning dbscan

我已经应用DBSCAN对由点云中每个点的X,Y和Z坐标组成的数据集执行聚类。我只想绘制少于100点的聚类。这是我到目前为止的内容:

clustering = DBSCAN(eps=0.1, min_samples=20, metric='euclidean').fit(only_xy)
plt.scatter(only_xy[:, 0], only_xy[:, 1],
        c=clustering.labels_, cmap='rainbow')
clusters = clustering.components_
#Store the labels
labels = clustering.labels_

#Then get the frequency count of the non-negative labels
counts = np.bincount(labels[labels>=0])

print(counts)

Output: 
[1278  564  208   47   36   30  191   54   24   18   40  915   26   20
   24  527   56  677   63   57   61 1544  512   21   45  187   39  132
   48   55  160   46   28   18   55   48   35   92   29   88   53   55
   24   52  114   49   34   34   38   52   38   53   69]

因此,我找到了每个群集中的点数,但是我不确定如何仅选择少于100个点的群集。

2 个答案:

答案 0 :(得分:1)

您可能会发现计数少于100的标签的索引:

ls, cs = np.unique(labels,return_counts=True)
dic = dict(zip(ls,cs))
idx = [i for i,label in enumerate(labels) if dic[label] <100 and label >= 0]

然后,您可以将结果索引应用于DBSCAN结果和标签(或多或少):

plt.scatter(only_xy[idx, 0], only_xy[idx, 1],
        c=clustering.labels_[idx], cmap='rainbow')

答案 1 :(得分:0)

我认为,如果您运行此代码,则可以获得标签以及大小超过100的群集的群集组件:

from collections import Counter
labels_with_morethan100=[label for (label,count) in Counter(clustering.labels_).items() if count>100]
clusters_biggerthan100= clustering.components_[np.isin(clustering.labels_[clustering.labels_>=0], labels_with_morethan100)]