DBSCAN error with cosine metric in python

后端 未结 2 1640
梦毁少年i
梦毁少年i 2021-01-18 08:50

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_s         


        
相关标签:
2条回答
  • 2021-01-18 09:04

    If you want a normalized distance like the cosine distance, you can also normalize your vectors first and then use the euclidean metric. Notice that for two normalized vectors u and v the euclidean distance is equal to sqrt(2-2*cos(u, v)) (see this discussion)

    You can hence do something like:

    Xnorm = np.linalg.norm(X,axis = 1)
    Xnormed = np.divide(X,Xnorm.reshape(Xnorm.shape[0],1))
    db = DBSCAN(eps=0.5, min_samples=2, metric='euclidean').fit(Xnormed) 
    

    The distances will lie in [0,2] so make sure you adjust your parameters accordingly.

    0 讨论(0)
  • 2021-01-18 09:12

    The indexes in sklearn (probably - this may change with new versions) cannot accelerate cosine.

    Try algorithm='brute'.

    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)
    
    0 讨论(0)
提交回复
热议问题