本文整理汇总了Python中scipy.stats.wasserstein_distance方法的典型用法代码示例。如果您正苦于以下问题:Python stats.wasserstein_distance方法的具体用法?Python stats.wasserstein_distance怎么用?Python stats.wasserstein_distance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats
的用法示例。
在下文中一共展示了stats.wasserstein_distance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: aggtest
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def aggtest(self, f, colname, numbins=0, binsize="auto", debug=False, plot=True, bound=True, exact=False):
"""
Verification of SQL aggregation mechanisms
Returns statistical distance measures between repeated analysis
responses on neighboring datasets
"""
d1, d2, d1_metadata, d2_metadata = self.generate_neighbors()
fD1, fD2 = self.apply_aggregation_neighbors(f, (d1, colname), (d2, colname))
d1size, d2size = fD1.size, fD2.size
ks_res = self.ks_test(fD1, fD2)
d1hist, d2hist, bin_edges = \
self.generate_histogram_neighbors(fD1, fD2, numbins, binsize, exact=exact)
dp_res, d1histupperbound, d2histupperbound, d1lower, d2lower = self.dp_test(d1hist, d2hist, bin_edges, d1size, d2size, debug, exact=exact)
ws_res = 0.0
if(exact):
return False, 0.0, 0.0
else:
ws_res = self.wasserstein_distance(d1hist, d2hist)
if(plot):
self.plot_histogram_neighbors(fD1, fD2, d1histupperbound, d2histupperbound, d1hist, d2hist, d1lower, d2lower, bin_edges, bound, exact)
return dp_res, ks_res, ws_res
示例2: test_emd_1d_emd2_1d
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_emd_1d_emd2_1d():
# test emd1d gives similar results as emd
n = 20
m = 30
rng = np.random.RandomState(0)
u = rng.randn(n, 1)
v = rng.randn(m, 1)
M = ot.dist(u, v, metric='sqeuclidean')
G, log = ot.emd([], [], M, log=True)
wass = log["cost"]
G_1d, log = ot.emd_1d(u, v, [], [], metric='sqeuclidean', log=True)
wass1d = log["cost"]
wass1d_emd2 = ot.emd2_1d(u, v, [], [], metric='sqeuclidean', log=False)
wass1d_euc = ot.emd2_1d(u, v, [], [], metric='euclidean', log=False)
# check loss is similar
np.testing.assert_allclose(wass, wass1d)
np.testing.assert_allclose(wass, wass1d_emd2)
# check loss is similar to scipy's implementation for Euclidean metric
wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,)))
np.testing.assert_allclose(wass_sp, wass1d_euc)
# check constraints
np.testing.assert_allclose(np.ones((n,)) / n, G.sum(1))
np.testing.assert_allclose(np.ones((m,)) / m, G.sum(0))
# check G is similar
np.testing.assert_allclose(G, G_1d)
# check AssertionError is raised if called on non 1d arrays
u = np.random.randn(n, 2)
v = np.random.randn(m, 2)
with pytest.raises(AssertionError):
ot.emd_1d(u, v, [], [])
示例3: test_emd_1d_emd2_1d_with_weights
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_emd_1d_emd2_1d_with_weights():
# test emd1d gives similar results as emd
n = 20
m = 30
rng = np.random.RandomState(0)
u = rng.randn(n, 1)
v = rng.randn(m, 1)
w_u = rng.uniform(0., 1., n)
w_u = w_u / w_u.sum()
w_v = rng.uniform(0., 1., m)
w_v = w_v / w_v.sum()
M = ot.dist(u, v, metric='sqeuclidean')
G, log = ot.emd(w_u, w_v, M, log=True)
wass = log["cost"]
G_1d, log = ot.emd_1d(u, v, w_u, w_v, metric='sqeuclidean', log=True)
wass1d = log["cost"]
wass1d_emd2 = ot.emd2_1d(u, v, w_u, w_v, metric='sqeuclidean', log=False)
wass1d_euc = ot.emd2_1d(u, v, w_u, w_v, metric='euclidean', log=False)
# check loss is similar
np.testing.assert_allclose(wass, wass1d)
np.testing.assert_allclose(wass, wass1d_emd2)
# check loss is similar to scipy's implementation for Euclidean metric
wass_sp = wasserstein_distance(u.reshape((-1,)), v.reshape((-1,)), w_u, w_v)
np.testing.assert_allclose(wass_sp, wass1d_euc)
# check constraints
np.testing.assert_allclose(w_u, G.sum(1))
np.testing.assert_allclose(w_v, G.sum(0))
示例4: test_distinct_value_and_weight_lengths
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_distinct_value_and_weight_lengths(self):
# When the number of weights does not match the number of values,
# a ValueError should be raised.
assert_raises(ValueError, stats.wasserstein_distance,
[1], [2], [4], [3, 1])
assert_raises(ValueError, stats.wasserstein_distance, [1], [2], [1, 0])
示例5: test_zero_weight
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_zero_weight(self):
# When a distribution is given zero weight, a ValueError should be
# raised.
assert_raises(ValueError, stats.wasserstein_distance,
[0, 1], [2], [0, 0])
assert_raises(ValueError, stats.wasserstein_distance,
[0, 1], [2], [3, 1], [0])
示例6: test_negative_weights
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_negative_weights(self):
# A ValueError should be raised if there are any negative weights.
assert_raises(ValueError, stats.wasserstein_distance,
[0, 1], [2, 2], [1, 1], [3, -1])
示例7: test_empty_distribution
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_empty_distribution(self):
# A ValueError should be raised when trying to measure the distance
# between something and nothing.
assert_raises(ValueError, stats.wasserstein_distance, [], [2, 2])
assert_raises(ValueError, stats.wasserstein_distance, [1], [])
示例8: test_inf_weight
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_inf_weight(self):
# An inf weight is not valid.
assert_raises(ValueError, stats.wasserstein_distance,
[1, 2, 1], [1, 1], [1, np.inf, 1], [1, 1])
示例9: test_same_distribution
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_same_distribution(self):
# Any distribution moved to itself should have a Wasserstein distance of
# zero.
assert_equal(stats.wasserstein_distance([1, 2, 3], [2, 1, 3]), 0)
assert_equal(
stats.wasserstein_distance([1, 1, 1, 4], [4, 1],
[1, 1, 1, 1], [1, 3]),
0)
示例10: test_shift
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_shift(self):
# If the whole distribution is shifted by x, then the Wasserstein
# distance should be x.
assert_almost_equal(stats.wasserstein_distance([0], [1]), 1)
assert_almost_equal(stats.wasserstein_distance([-5], [5]), 10)
assert_almost_equal(
stats.wasserstein_distance([1, 2, 3, 4, 5], [11, 12, 13, 14, 15]),
10)
assert_almost_equal(
stats.wasserstein_distance([4.5, 6.7, 2.1], [4.6, 7, 9.2],
[3, 1, 1], [1, 3, 1]),
2.5)
示例11: test_combine_weights
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_combine_weights(self):
# Assigning a weight w to a value is equivalent to including that value
# w times in the value array with weight of 1.
assert_almost_equal(
stats.wasserstein_distance(
[0, 0, 1, 1, 1, 1, 5], [0, 3, 3, 3, 3, 4, 4],
[1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1]),
stats.wasserstein_distance([5, 0, 1], [0, 4, 3],
[1, 2, 4], [1, 2, 4]))
示例12: test_collapse
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def test_collapse(self):
# Collapsing a distribution to a point distribution at zero is
# equivalent to taking the average of the absolute values of the values.
u = np.arange(-10, 30, 0.3)
v = np.zeros_like(u)
assert_almost_equal(
stats.wasserstein_distance(u, v),
np.mean(np.abs(u)))
u_weights = np.arange(len(u))
v_weights = u_weights[::-1]
assert_almost_equal(
stats.wasserstein_distance(u, v, u_weights, v_weights),
np.average(np.abs(u), weights=u_weights))
示例13: peste_distance
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def peste_distance(self) -> np.ndarray:
"""Calculates the euclidean distance between pixels of two different arrays
on a vector of observations, and normalizes the result applying the relativize function.
In a more general scenario, any function that quantifies the notion of "how different two
observations are" could work, even if it is not a proper distance.
"""
# Get random companion
peste_obs = self.get_peste_obs()
# Euclidean distance between states (pixels / RAM)
# obs = self.observations.astype(np.float32).reshape((self.n_walkers, -1))
dist = self.wasserstein_distance(np.array(self.observations), peste_obs)
return relativize_vector(dist)
示例14: wasserstein_distance
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def wasserstein_distance(x, y):
def entropy_dist(x, y):
def hernandez_crossentropy(x, y):
return 1 + np.log(np.prod(2 - x ** y, axis=2))
first = hernandez_crossentropy(x, y).mean(axis=1)
sec = hernandez_crossentropy(y, x).mean(axis=1)
return np.maximum(first, sec)
def _wasserstein_distance(x, y):
from scipy import stats
def stacked_distance(x, y):
distances = []
for i in range(x.shape[0]):
dist_val = stats.wasserstein_distance(x[i], y[i])
distances.append(dist_val)
return np.array(distances)
distances = []
for i in range(x.shape[0]):
dist_val = stacked_distance(x[i], y[i]).mean()
distances.append(dist_val)
return np.array(distances)
return _wasserstein_distance(x, y)
示例15: evaluate_distance
# 需要导入模块: from scipy import stats [as 别名]
# 或者: from scipy.stats import wasserstein_distance [as 别名]
def evaluate_distance(self) -> np.ndarray:
"""Calculates the euclidean distance between pixels of two different arrays
on a vector of observations, and normalizes the result applying the relativize function.
In a more general scenario, any function that quantifies the notion of "how different two
observations are" could work, even if it is not a proper distance.
"""
# Get random companion
idx = np.random.permutation(np.arange(self.n_walkers, dtype=int))
# Euclidean distance between states (pixels / RAM)
obs = self.observations.astype(np.float32)
dist = self.wasserstein_distance(obs[idx], obs) # ** 2
return relativize_vector(dist)