python中的余弦量度出现DBSCAN错误 [英] DBSCAN error with cosine metric in python
问题描述
我正尝试使用scikit-learn库中的DBSCAN算法和余弦度量,但因错误而卡住了。
代码行是
I was trying to use DBSCAN algorithm from scikit-learn library with cosine metric but was stuck with the error. The line of code is
db = DBSCAN(eps=1, min_samples=2, metric='cosine').fit(X)
其中 X
是 csr_matrix
。错误如下:
度量'余弦'对算法'auto'无效,
Metric 'cosine' not valid for algorithm 'auto',
尽管文档说可以使用此度量。
我尝试使用选项 algorithm ='kd_tree'
和'ball_tree'
,但是得到了相同的结果。但是,如果使用 euclidean
或说 l1
度量标准,则没有错误。
though the documentation says that it is possible to use this metric.
I tried to use option algorithm='kd_tree'
and 'ball_tree'
but got the same. However, there is no error if I use euclidean
or, say, l1
metric.
矩阵 X
很大,所以我无法使用预先计算的成对距离矩阵。
The matrix X
is large, so I can't use a precomputed matrix of pairwise distances.
我使用 python 2.7.6
和 scikit-learn 0.16.1
。
我的数据集没有完整的零行,因此余弦度量是定义明确的。
I use python 2.7.6
and scikit-learn 0.16.1
.
My dataset doesn't have a full row of zeros, so cosine metric is well-defined.
推荐答案
sklearn中的索引(可能-新版本可能会改变)不能加速余弦。
The indexes in sklearn (probably - this may change with new versions) cannot accelerate cosine.
尝试 algorithm ='brute'
。
有关指标列表, sklearn的版本可以加速,请参阅球树的支持指标:
For a list of metrics that your version of sklearn can accelerate, see the supported metrics of the ball tree:
from sklearn.neighbors.ball_tree import BallTree
print(BallTree.valid_metrics)
这篇关于python中的余弦量度出现DBSCAN错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!