2

I try to calculate the Earth Mover Distance between two 1-dimensional numpy histograms, like:

(array([ 0.53586639,  0.71448852,  1.22534781,  1.68262046,  1.20391316]), array([ 0.        ,  0.18648936,  0.37297871,  0.55946807,  0.74595742,
     0.93244678]), <a list of 5 Patch objects>)

and

(array([ 0.05986936,  0.41133267,  1.0449142 ,  2.43569242,  2.50891394]), array([ 0.17373296,  0.32851441,  0.48329586,  0.63807731,  0.79285876,
     0.9476402 ]), <a list of 5 Patch objects>)

I want to do it for 1-dimensional arrays, not for images. I want a simple solution.

Mostafa
  • 1,501
  • 3
  • 21
  • 37
  • 1
    Possible duplicate of [How to compute "EMD" for 2 numpy arrays i.e "histogram" using opencv?](https://stackoverflow.com/questions/15706339/how-to-compute-emd-for-2-numpy-arrays-i-e-histogram-using-opencv) – Gabriel Devillers Aug 17 '17 at 16:39
  • 2
    not exactly: my question is more simple. I just want to calculate the EMD, while in the other question, s/he asks for a specific method: 'How can I transfer numpy array to CVhistogram and from Cvhistogram to the function parameter signature?' – Mostafa Aug 17 '17 at 16:55
  • Possible duplicate of [Python code for Earth mover's Distance](https://stackoverflow.com/questions/5101004/python-code-for-earth-movers-distance) – Eskapp Aug 17 '17 at 20:54

1 Answers1

1

A simple python code:

import numpy as np

def wasserstein_distance(A,B):
    n = len(A)
    dist = np.zeros(n)
    for x in range(n-1):
        dist[x+1] = A[x]-B[x]+dist[x]
    return np.sum(abs(dist))