Mostafa
Mostafa

Reputation: 1551

Earth Mover Distance between numpy 1-D histograms

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.

Upvotes: 2

Views: 2015

Answers (1)

Japneet Singh
Japneet Singh

Reputation: 11

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

Upvotes: 1

Related Questions