Most efficient way to construct similarity matrix

前端 未结 5 1401
孤城傲影
孤城傲影 2020-12-31 13:53

I\'m using the following links to create a \"Euclidean Similarity Matrix\" (that I convert to a DataFrame). https://stats.stackexchange.com/questions/53068/euclidean-distan

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 14:37

    I think you can just use pdist and squareform to broadcast directly on your DataFrame:

    from scipy.spatial.distance import pdist,squareform
    
    In [6]: squareform(pdist(DF_var, metric='euclidean'))
    
    Out[6]:
    array([[ 0.        ,  0.6164414 ,  1.4525839 ,  0.78740079],
           [ 0.6164414 ,  0.        ,  1.1       ,  0.24494897],
           [ 1.4525839 ,  1.1       ,  0.        ,  0.87749644],
           [ 0.78740079,  0.24494897,  0.87749644,  0.        ]])
    

提交回复
热议问题