POT (Python Optimal Transport) is a comprehensive Python library offering various solvers for optimal transport problems. It provides efficient implementations for classic optimal transport, Wasserstein distances, Sinkhorn algorithm, Gromov-Wasserstein, and more, including recent extensions like unbalanced OT and GMM-OT. Currently at version 0.9.6.post1, the library sees frequent minor releases, often introducing new features, solvers, and bug fixes.
pip install potVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to compute the Earth Mover's Distance (EMD) between two 1D samples using POT's core `ot.emd` function. It covers generating samples, defining uniform marginal distributions, computing a normalized cost matrix, and finally, calculating the optimal transport plan and its total cost.
Review the official documentation and examples for GW solvers if migrating from versions older than 0.9.0. Verify results, especially when dealing with non-symmetric matrices.
For GPU or specific backend usage, import and configure `ot.backend` (e.g., `import ot.backend as ob; ob.set_backend('torch', 'cuda')`) or ensure all input tensors are compatible with the desired backend.Always consult the specific function's documentation for expected input shapes. Use `.reshape()` or `.T` carefully to ensure arrays conform to the required dimensions.
Normalize marginal distributions `a` and `b` such that `np.sum(a) == 1` and `np.sum(b) == 1` before passing them to POT functions, unless the specific function documentation explicitly states otherwise for unbalanced OT.
For N > 1000 samples, favor `ot.sinkhorn` or other regularized/approximate solvers over `ot.emd`. Explore techniques like sub-sampling, multi-scale, or barycentric mapping for further scalability.
Use `import ot` instead of `import pot` in your Python code. Make sure the library is installed with `pip install POT` or `conda install -c conda-forge pot`.
First install Cython and NumPy: `pip install numpy cython`, then install POT: `pip install POT`. Upgrading POT to a newer version might also resolve this as pre-compiled wheels are often available.
Ensure the `metric` parameter provided to the 1D EMD function is one of the accepted strings: 'sqeuclidean', 'minkowski', 'cityblock', or 'euclidean'.
Normalize your histograms `a` and `b` so that `np.sum(a)` and `np.sum(b)` are both approximately 1.0, and ensure all elements in `a` and `b` are non-negative. For example: `a = a / np.sum(a)`.