繁体   English   中英

在python中获取sklearn中的簇大小

[英]Get the cluster size in sklearn in python

我正在使用sklearn DBSCAN来集群我的数据,如下所示。

#Apply DBSCAN (sims == my data as list of lists)
db1 = DBSCAN(min_samples=1, metric='precomputed').fit(sims)

db1_labels = db1.labels_
db1n_clusters_ = len(set(db1_labels)) - (1 if -1 in db1_labels else 0)
#Returns the number of clusters (E.g., 10 clusters)
print('Estimated number of clusters: %d' % db1n_clusters_)

现在我想从大小(每个群集中的数据点数)中排序前3个群集。 请告诉我如何获取sklearn中的簇大小?

那么你可以在Numpy中使用Bincount Function来获得标签的频率。 例如,我们将使用scikit-learn使用DBSCAN示例

#Store the labels
labels = db.labels_

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

print counts
#Output : [243 244 245]

然后获取前3个值在numpy中使用argsort 在我们的例子中,因为只有3个集群,我将提取前2个值:

top_labels = np.argsort(-counts)[:2]

print top_labels
#Output : [2 1]

#To get their respective frequencies
print counts[top_labels]

另一种选择是使用numpy.unique

db1_labels = db1.labels_
labels, counts = np.unique(db1_labels[db1_labels>=0], return_counts=True)
print labels[np.argsort(-counts)[:3]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM