Calculate weighted pairwise distance matrix in Python

前端 未结 2 1742
孤城傲影
孤城傲影 2021-02-14 11:37

I am trying to find the fastest way to perform the following pairwise distance calculation in Python. I want to use the distances to rank a list_of_objects by their

2条回答
  •  后悔当初
    2021-02-14 12:40

    scipy.spatial.distance is the module you'll want to have a look at. It has a lot of different norms that can be easily applied.

    I'd recommend using the weighted Monkowski Metrik

    Weighted Minkowski Metrik

    You can do pairwise distance calculation by using the pdist method from this package.

    E.g.

    import numpy as np
    from scipy.spatial.distance import pdist, wminkowski, squareform
    
    object_1 = [0.2, 4.5, 198, 0.003]
    object_2 = [0.3, 2.0, 999, 0.001]
    object_3 = [0.1, 9.2, 321, 0.023]
    list_of_objects = [object_1, object_2, object_3]
    
    # make a 3x4 array from the list of objects
    X = np.array(list_of_objects)
    
    #calculate pairwise distances, using weighted Minkowski norm
    distances = pdist(X,wminkowski,2, [1,1,1,10])
    
    #make a square matrix from result
    distances_as_2d_matrix = squareform(distances)
    
    print distances
    print distances_as_2d_matrix
    

    This will print

    [ 801.00390786  123.0899671   678.0382942 ]
    [[   0.          801.00390786  123.0899671 ]
     [ 801.00390786    0.          678.0382942 ]
     [ 123.0899671   678.0382942     0.        ]]
    

提交回复
热议问题