Skip to main content

A Python wrapper for Ofir Pele and Michael Werman's implementation of the Earth Mover's Distance.

Project description

PyEMD is a Python wrapper for Ofir Pele and Michael Werman’s implementation of the Earth Mover’s Distance that allows it to be used with NumPy.

This wrapper does not expose the full functionality of that library; it can only used be with the np.float data type, and with a symmetric distance matrix that represents a true metric. See the documentation for the original Pele and Werman library for the other options it provides.


To install the latest release:

pip install pyemd

To install the latest development version:

pip install “git+”


Use PyEMD like so:

>>> from pyemd import emd
>>> import numpy as np
>>> first_signature = np.array([0.0, 1.0])
>>> second_signature = np.array([5.0, 3.0])
>>> distance_matrix = np.array([[0.0, 0.5], [0.5, 0.0]])
>>> emd(first_signature, second_signature, distance_matrix)


emd(first_signature, second_signature, distance_matrix)
  • first_signature: A 1-dimensional numpy array of np.float, of size N.
  • second_signature: A 1-dimensional numpy array of np.float, of size N.
  • distance_matrix: A 2-dimensional array of np.float, of size NxN. Must be symmetric and represent a metric.

Limitations and Caveats

  • distance_matrix must be symmetric.
  • distance_matrix is assumed to represent a true metric. This must be enforced by the caller. See the documentation in pyemd/lib/emd_hat.hpp.
  • The signatures and distance matrix must be numpy arrays of np.float. The original C++ template function can accept any numerical C++ type, but this wrapper only instantiates the template with double (Cython converts np.float to double). If there’s demand, I can add support for other types.
  • The original C++ functions have optional parameters extra_mass_penalty and F (for flows); this wrapper does not expose those parameters. See the documentation in pyemd/lib/emd_hat.hpp.


To help develop PyEMD, fork the project on GitHub and install the requirements with pip.

The Makefile defines some tasks to help with development:

  • buildcython: compiles the Cython code into C++ and then builds the C++ into a Python extension
  • runtests: builds everything and then runs the unit tests
  • clean: removes the compiled C++


  • All credit for the actual algorithm and implementation goes to Ofir Pele and Michael Werman. See the relevant paper.
  • Thanks to the Cython devlopers for making this kind of wrapper relatively easy to write.

Please cite these papers if you use this code:

Ofir Pele and Michael Werman, “A linear time histogram metric for improved SIFT matching,” in Computer Vision–ECCV 2008, Marseille, France, 2008, pp. 495-508.

  title={A linear time histogram metric for improved sift matching},
  author={Pele, Ofir and Werman, Michael},
  booktitle={Computer Vision--ECCV 2008},

Ofir Pele and Michael Werman, “Fast and robust earth mover’s distances,” in Proc. 2009 IEEE 12th Int. Conf. on Computer Vision, Kyoto, Japan, 2009, pp. 460-467.

  title={Fast and robust earth mover's distances},
  author={Pele, Ofir and Werman, Michael},
  booktitle={2009 IEEE 12th International Conference on Computer Vision},

Project details

Release history Release notifications

History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


History Node


This version
History Node


History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pyemd-0.0.7-cp33-cp33m-macosx_10_9_x86_64.whl (59.7 kB) Copy SHA256 hash SHA256 Wheel cp33 Apr 9, 2014
pyemd-0.0.7.tar.gz (59.1 kB) Copy SHA256 hash SHA256 Source None Apr 9, 2014

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging CloudAMQP CloudAMQP RabbitMQ AWS AWS Cloud computing Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page