wmayner / pyemd

Fast EMD for Python: a wrapper for Pele and Werman's C++ implementation of the Earth Mover's Distance metric

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for multi-dimensional points

pclucas14 opened this issue · comments

Hi,

Do you plan on adding support for multi-dimensional points ? I am working with (x,y,z) coordinates and it would be of huge help if this support would be added.

Thanks!

It shouldn't really matter whether the bins of your histograms are indexed by three or any other number of coordinates, as long as you construct the distance matrix appropriately.

Just np.ravel the signatures so that they're 1-dimensional, and build the distance matrix so that the i,jth entry is the ground distance between point i = (x_1, y_1, z_1) and point j = (x_2, y_2, z_2). You'll need a mapping between integer indices and 3D points.

Hi again,

thanks for the fast reply. After looking more closely to the example, I'm unsure as to how I can translate my problem to the histogram setting. Basically I have 2 sets of size N, with a NxN matrix of pairwise distances. In the example, the distance matrix seems to show cost to move from 1 histogram to another, whereas mine are actual distances. Given this information, do you think I can leverage your code ?

Thank you

If you take a look at the API section of the README, the distance matrix is exactly that: an N by N matrix that gives the pairwise distance between the histogram bins (the “ground distance”). The EMD between the histograms is the output of the emd function, not one of its arguments.

The only way I can think of adapting this to the histogram setting is by by setting first_historgram to be a vector of size 2Nx1, where the first N are 0's and the last N are 1's (and vice versa for second_histogram).
Then I would need to convert my NxN matrix into a 2Nx2N matrix where the top left and bottom right quadrant are filled with zeros. Note that the 2 sets contain different elements, and I don't have distances between points in the same set.

Unfortunately, that's too much overhead since I have N =~ 2500. You seem to say there is an easy to plugging this in your API. I'd like to apologise, as I cannot cleary see it.

Thanks for the help

If you don't want to allow moving mass within a histogram, then yes, you need to pad them with zeros as you described—however, the top-left and bottom-right blocks of the distance matrix should be filled with a value that's higher than any other, not with zero, as that would mean moving mass within a histogram is free (and allowed).

If your histogram just consists of 1s, then your problem is an instance of minimum-cost bipartite matching: you need to find the minimum-cost way of pairing up points in either set. The EMD is more general; you might find better performance with another optimization library that solves that problem specifically. Furthermore, since you're dealing with a 3-dimensional space equipped with some metric, then there are additional constraints on costs between the points; you might be able to exploit that structure to get a faster solution than in the general cost case if you write your own solver.

In any case, none of this requires additional implementation to support multi-dimensional points, so I'm closing this issue.