【问题标题】:DBSCAN in scikit-learn of Python: Trouble understanding result of DBSCANPython scikit-learn 中的 DBSCAN:无法理解 DBSCAN 的结果
【发布时间】:2016-06-25 18:00:03
【问题描述】:

这个例子来自 Data Science for dummies:

digits = load_digits()
X = digits.data
ground_truth = digits.target

pca = PCA(n_components=40)
Cx = pca.fit_transform(scale(X))

DB = DBSCAN(eps=4.35, min_samples=25, random_state=1)
DB.fit(Cx)



for k,cl in enumerate(np.unique(DB.labels_)):
    if cl >= 0:
        example = np.min(np.where(DB.labels_==cl)) # question 1
        plt.subplot(2, 3, k)
            plt.imshow(digits.images[example],cmap='binary', # question 2
            interpolation='none') 
        plt.title('cl '+str(cl))
plt.show()

我的问题是:

  1. np.where(DB.labels_==cl) 我不明白我们在哪个数组上应用 np.where 当我打印 np.where(DB.labels_==cl) 时,它看起来像是应用于 DB.core_sample_indices_。但我不明白为什么。正如我从 np.where 的文档中了解到的那样,应将 np.where(DB.labels_==cl) 应用于 DB.labels_。
  2. np.min(np.where(DB.labels_==cl)) 怎么会给我在 digits.images 中绘制正确图像的索引。 谢谢。

【问题讨论】:

    标签: python scikit-learn dbscan


    【解决方案1】:
    1. 操作DB.labels_ == cl 的输出是一个布尔数组,如果DB.labels_[i] == cl,则(DB.labels_ == cl)[i]True

      因此np.where 应用于数组DB.labels_ == cl。如果在单个数组上使用,它的输出是这个数组的非零元素,元素是True

      np.where(DB.labels_ == cl) 操作返回等于clDB.labels_ 元素的索引。这些是fit 中使用的数据元素,已被DB 标记为集群cl 的一部分。

    2. 在这种情况下,np.min 返回前一个数组中的最小索引。这意味着它将检索您的集合中被归类为集群cl 的第一个元素。通过遍历所有集群,您可以检索一组构成集群的图像示例。

      此索引对应于 data.image 中的索引,因为DB.labels_ 包含您提供给DB.fit 的数据集中每个点的标签。该数据集与data.images的顺序相同。

    【讨论】:

      猜你喜欢
      • 2014-09-11
      • 2019-08-13
      • 2016-01-01
      • 2013-04-29
      • 2015-03-05
      • 2013-04-01
      • 2019-04-15
      • 2013-07-01
      • 2013-08-16
      相关资源
      最近更新 更多