A Python wrapper for Ofir Pele and Michael Werman's implementation of the Earth Mover's Distance.
Project description
PyEMD: Fast EMD for Python
This library is maintained for backward compatibility only.
PyEMD v2.0+ is a thin wrapper around POT (Python Optimal Transport). For new projects, use POT directly—it offers the same functionality plus a much broader range of optimal transport features.
PyEMD continues to be maintained for existing projects that depend on its API.
PyEMD computes the Earth Mover’s Distance (Wasserstein distance) between histograms using NumPy.
Usage
>>> from pyemd import emd
>>> import numpy as np
>>> first_histogram = np.array([0.0, 1.0])
>>> second_histogram = np.array([5.0, 3.0])
>>> distance_matrix = np.array([[0.0, 0.5],
... [0.5, 0.0]])
>>> emd(first_histogram, second_histogram, distance_matrix)
3.5
You can also get the associated minimum-cost flow:
>>> from pyemd import emd_with_flow
>>> emd_with_flow(first_histogram, second_histogram, distance_matrix)
(3.5, [[0.0, 0.0], [0.0, 1.0]])
You can also calculate the EMD directly from two arrays of observations:
>>> from pyemd import emd_samples
>>> first_array = [1, 2, 3, 4]
>>> second_array = [2, 3, 4, 5]
>>> emd_samples(first_array, second_array, bins=2)
0.5
API Documentation
emd()
emd(first_histogram,
second_histogram,
distance_matrix,
extra_mass_penalty=-1.0)
Arguments:
first_histogram (array-like): A 1D array of length N.
second_histogram (array-like): A 1D array of length N.
distance_matrix (array-like): A 2D array of size at least N × N. This defines the underlying metric, or ground distance, by giving the pairwise distances between the histogram bins. NOTE: It must represent a metric; there is no warning if it doesn’t.
Keyword Arguments:
extra_mass_penalty (float): The penalty for extra mass. If you want the resulting distance to be a metric, it should be at least half the diameter of the space (maximum possible distance between any two points). If you want partial matching you can set it to zero (but then the resulting distance is not guaranteed to be a metric). The default value is -1.0, which means the maximum value in the distance matrix is used.
Returns: (float) The EMD value.
emd_with_flow()
emd_with_flow(first_histogram,
second_histogram,
distance_matrix,
extra_mass_penalty=-1.0)
Arguments are the same as for emd().
Returns: (tuple(float, list(list(float)))) The EMD value and the associated minimum-cost flow.
emd_samples()
emd_samples(first_array,
second_array,
extra_mass_penalty=-1.0,
distance='euclidean',
normalized=True,
bins='auto',
range=None)
Arguments:
first_array (Iterable): An array of samples used to generate a histogram.
second_array (Iterable): An array of samples used to generate a histogram.
Keyword Arguments:
extra_mass_penalty (float): Same as for emd().
distance (string or function): A string or function implementing a metric on a 1D np.ndarray. Defaults to the Euclidean distance. Currently limited to ‘euclidean’ or your own function, which must take a 1D array and return a square 2D array of pairwise distances.
normalized (boolean): If true (default), treat histograms as fractions of the dataset. If false, treat histograms as counts. In the latter case the EMD will vary greatly by array length.
bins (int or string): The number of bins to include in the generated histogram. If a string, must be one of the bin selection algorithms accepted by np.histogram(). Defaults to 'auto', which gives the maximum of the ‘sturges’ and ‘fd’ estimators.
range (tuple(int, int)): The lower and upper range of the bins, passed to numpy.histogram(). Defaults to the range of the union of first_array and second_array. Note: if the given range is not a superset of the default range, no warning will be given.
Returns: (float) The EMD value between the histograms of first_array and second_array.
Development Setup
This project uses uv for dependency management.
Quick start:
# Install uv (if not already installed) curl -LsSf https://astral.sh/uv/install.sh | sh # Clone and setup git clone https://github.com/wmayner/pyemd.git cd pyemd uv sync --all-extras # Install in editable mode uv pip install -e . # Run tests uv run pytest
Note: For development workflows, see the DEVELOPING.md file in the repository.
Dependencies are locked in uv.lock for reproducibility.
Limitations and Caveats
emd() and emd_with_flow():
The distance_matrix is assumed to represent a metric; there is no check to ensure that this is true.
emd_with_flow():
The flow matrix does not contain the flows to/from the extra mass bin.
Credit
PyEMD v2.0+ uses the POT (Python Optimal Transport) library by Rémi Flamary et al.
PyEMD v1.x was a Python wrapper for the emd_hat C++ implementation by Ofir Pele and Michael Werman.
Citation
For PyEMD v2.0+
If you use PyEMD v2.0 or later, please cite the POT library:
Rémi Flamary et al. POT: Python Optimal Transport. Journal of Machine Learning Research, 22(78):1-8, 2021.
@article{flamary2021pot,
title={POT: Python Optimal Transport},
author={Flamary, R{\'e}mi and Courty, Nicolas and Gramfort, Alexandre and
Alaya, Mokhtar Z. and Boisbunon, Aur{\'e}lie and Chambon, Stanislas and
Chapel, Laetitia and Corenflos, Adrien and Fatras, Kilian and
Fournier, Nemo and Gautheron, L{\'e}o and Gayraud, Nathalie T.H. and
Janati, Hicham and Rakotomamonjy, Alain and Redko, Ievgen and
Rolet, Antoine and Schutz, Antony and Seguy, Vivien and
Sutherland, Danica J. and Tavenard, Romain and Tong, Alexander and
Vayer, Titouan},
journal={Journal of Machine Learning Research},
volume={22},
number={78},
pages={1--8},
year={2021}
}
For PyEMD v1.x or the original C++ implementation
If you use PyEMD v1.x, or wish to cite the original C++ algorithm that PyEMD was built on, please cite the following papers:
Ofir Pele and Michael Werman. Fast and robust earth mover’s distances. Proc. 2009 IEEE 12th Int. Conf. on Computer Vision, Kyoto, Japan, 2009, pp. 460-467.
@INPROCEEDINGS{pele2009,
title={Fast and robust earth mover's distances},
author={Pele, Ofir and Werman, Michael},
booktitle={2009 IEEE 12th International Conference on Computer Vision},
pages={460--467},
year={2009},
month={September},
organization={IEEE}
}
Ofir Pele and Michael Werman. A linear time histogram metric for improved SIFT matching. Computer Vision - ECCV 2008, Marseille, France, 2008, pp. 495-508.
@INPROCEEDINGS{pele2008,
title={A linear time histogram metric for improved sift matching},
author={Pele, Ofir and Werman, Michael},
booktitle={Computer Vision--ECCV 2008},
pages={495--508},
year={2008},
month={October},
publisher={Springer}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyemd-2.0.0.tar.gz.
File metadata
- Download URL: pyemd-2.0.0.tar.gz
- Upload date:
- Size: 64.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15969f94435c2be98e6a36a49307c8366e3dfc1a67012acc306e92c90b503fe5
|
|
| MD5 |
d924215809aecc051d1547e85b2444d7
|
|
| BLAKE2b-256 |
56893e05f132093b4be0de31608b39c4e6b79c8991c4f6eea822a373fdd2bdea
|
File details
Details for the file pyemd-2.0.0-py3-none-any.whl.
File metadata
- Download URL: pyemd-2.0.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6d9a4bd2a590f48ecad07332304402e9c091aedc5dc4c778479fb61da24a890
|
|
| MD5 |
ee63d61a9dfbb86e8f595085b09640d9
|
|
| BLAKE2b-256 |
030a8905c3e6f3517d290ccea0c5943daaaab3428497d38865127e9fb9e69285
|