Search code examples
pythonlistscipyprobability

Distance metric with different sized lists (Python)


I am trying to find the jensen shannon distance metric between two distributions. However I am getting a ValueError because the two lists (probability distributions) are not the same size. How can I overcome this?

Here is the current code:

from scipy.spatial import distance
distance.jensenshannon([0.1, 0.1, 0.3, 0.2, 0.30], [0.30, 0.50, 0.20])
>> ValueError: operands could not be broadcast together with shapes (5,)(3,)

Solution

  • I suppose the function is unable to compare between 2 different shapes (5x1) and (3x1). I've tried this with both vectors of the same shape (5x1), and it works

    from scipy.spatial import distance
    distance.jensenshannon([0.1, 0.1, 0.3, 0.2, 0.30], [0.30, 0.50, 0.20, 0.1, 0.2])
    

    output

    0.30985287648299353
    

    And when I compare 2 identical vectors, the distance output is zero

    distance.jensenshannon([0.30, 0.50, 0.20, 0.1, 0.2], [0.30, 0.50, 0.20, 0.1, 0.2])