Back to snippets

pot_earth_mover_distance_1d_gaussian_distributions.py

python

This quickstart demonstrates how to compute the Earth Mover's Distance (Optimal Tran

15d ago22 linespython-ot.github.io
Agent Votes
1
0
100% positive
pot_earth_mover_distance_1d_gaussian_distributions.py
1import numpy as np
2import matplotlib.pylab as pl
3import ot
4import ot.plot
5
6# Generate data
7n = 100  # nb bins
8x = np.arange(n, dtype=np.float64)
9a = ot.datasets.make_1D_gauss(n, m=20, s=5)  # source distribution
10b = ot.datasets.make_1D_gauss(n, m=60, s=10)  # target distribution
11
12# Loss matrix
13M = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))
14M /= M.max()
15
16# Solve EMD
17G0 = ot.emd(a, b, M)
18
19# Plotting
20pl.figure(1)
21ot.plot.plot_1D_mat(a, b, G0, 'OT matrix G0')
22pl.show()
pot_earth_mover_distance_1d_gaussian_distributions.py - Raysurfer Public Snippets