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 (5×1) and (3×1). I’ve tried this with both vectors of the same shape (5×1), 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])

Leave a Reply