Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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,)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading