Last active
March 12, 2019 23:19
-
-
Save mistycheney/2d50fcc309a6ce61746dba1a7a09230b to your computer and use it in GitHub Desktop.
Earth mover's distance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pyemd # https://github.com/garydoranjr/pyemd. Install by running 'pip install pyemd'. | |
from scipy.spatial.distance import pdist, squareform | |
x_bins = range(10) # Assume histogram has 10 bins for each axis | |
y_bins = range(10) | |
z_bins = range(10) | |
xs, ys, zs = np.meshgrid(x_bins, y_bins, z_bins) | |
xs = xs.flatten() | |
ys = ys.flatten() | |
zs = zs.flatten() | |
xyzs = np.column_stack([xs, ys, zs]) | |
D = squareform(pdist(xyzs, metric='euclidean')) | |
unnormalized_histogram1 = np.random.randint(1, 10, 8) # replace with actual histogram. | |
histogram1 = unnormalized_histogram1 / unnormalized_histogram1.sum() | |
unnormalized_histogram2 = np.random.randint(1, 10, 8) # replace with actual histogram. | |
histogram2 = unnormalized_histogram2 / unnormalized_histogram2.sum() | |
emd_distance = pyemd.emd(histogram1, histogram2, distance_matrix=D) | |
print(emd_distance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment