【问题标题】:Get the cluster size in sklearn in python在python中获取sklearn中的集群大小
【发布时间】:2018-02-19 16:34:15
【问题描述】:

我正在使用 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 中获取集群大小?

【问题讨论】:

    标签: python machine-learning scikit-learn cluster-analysis dbscan


    【解决方案1】:

    另一种选择是使用numpy.unique:

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

    【讨论】:

      【解决方案2】:

      你可以Bincount Function in Numpy 来获取标签的频率。例如,我们将使用 scikit-learn 使用example for 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 个值,请使用 argsort in numpy。在我们的示例中,由于只有 3 个集群,我将提取前 2 个值:

      top_labels = np.argsort(-counts)[:2]
      
      print top_labels
      #Output : [2 1]
      
      #To get their respective frequencies
      print counts[top_labels]
      

      【讨论】:

      • 感谢您非常有用的回答。请告诉我如何获取245和244集群的集群标签?
      • '变量 top_label 将按顺序分别包含 245 和 244 簇的标签。另外,如果你觉得我的回答有用,请把它标记为正确的:-)
      猜你喜欢
      • 1970-01-01
      • 2018-02-24
      • 2019-11-12
      • 2019-06-02
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 2020-11-09
      相关资源
      最近更新 更多