DBSCAN中的预计算距离矩阵

8

看了一些资料,我发现可以将预先计算好的距离矩阵传递给SKLearn DBSCAN。不幸的是,我不知道如何进行计算。

假设我有一个有100个元素的1D数组,只包含节点的名称。然后我有一个2D矩阵,100x100,其中包含每个元素之间的距离(按相同顺序排列)。

我知道必须调用:

db = DBSCAN(eps=2, min_samples=5, metric="precomputed")

对于节点之间的距离为2和最小5个节点聚类。此外,使用“precomputed”表示使用2D矩阵。但是我该如何传递计算信息呢?

如果使用RAPIDS CUML DBScan函数(GPU加速),则同样的问题也可能出现。

1个回答

9

文档

class sklearn.cluster.DBSCAN(eps=0.5, *, min_samples=5, metric='euclidean', 
metric_params=None, algorithm='auto', leaf_size=30, p=None, n_jobs=None)
[...]
[...]
metricstring, or callable, default=’euclidean’
The metric to use when calculating distance between instances in a feature array. If 
metric is a string or callable, it must be one of the options allowed by 
sklearn.metrics.pairwise_distances for its metric parameter. If metric is 
“precomputed”, X is assumed to be a distance matrix and must be square. X may be a 
Glossary, in which case only “nonzero” elements may be considered neighbors for  
DBSCAN.
[...]

因此,通常称呼它的方式是:

from sklearn.cluster import DBSCAN

clustering = DBSCAN()
DBSCAN.fit(X)

如果您有一个距离矩阵,按以下步骤操作:
from sklearn.cluster import DBSCAN

clustering = DBSCAN(metric='precomputed')
clustering.fit(distance_matrix)

2
那么X将是具有成对距离的方阵,对吧?metric='fixed' 应该改为 metric='precomputed',是吗? - Jaime Nebrera
请您能否查看我下面提供的细节?谢谢。 - Jaime Nebrera
True,X 应该是成对距离矩阵,度量应该是“预计算的”。@warped 的回答是正确的。 - researchcollege111

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接