本文整理汇总了Python中sklearn.metrics.pairwise.euclidean_distances方法的典型用法代码示例。如果您正苦于以下问题:Python pairwise.euclidean_distances方法的具体用法?Python pairwise.euclidean_distances怎么用?Python pairwise.euclidean_distances使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics.pairwise
的用法示例。
在下文中一共展示了pairwise.euclidean_distances方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: time_dist
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def time_dist(datasets_dimred, time):
time_dist = euclidean_distances(time, time)
time_dists, scores = [], []
for i in range(time_dist.shape[0]):
for j in range(time_dist.shape[1]):
if i >= j:
continue
score = np.mean(euclidean_distances(
datasets_dimred[i], datasets_dimred[j]
))
time_dists.append(time_dist[i, j])
scores.append(score)
print('Spearman rho = {}'.format(spearmanr(time_dists, scores)))
print('Pearson rho = {}'.format(pearsonr(time_dists, scores)))
示例2: test_kmedoids_pp
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def test_kmedoids_pp():
"""Initial clusters should be well-separated for k-medoids++"""
rng = np.random.RandomState(seed)
kmedoids = KMedoids()
X = [
[10, 0],
[11, 0],
[0, 10],
[0, 11],
[10, 10],
[11, 10],
[12, 10],
[10, 11],
]
D = euclidean_distances(X)
centers = kmedoids._kpp_init(D, n_clusters=3, random_state_=rng)
assert len(centers) == 3
inter_medoid_distances = D[centers][:, centers]
assert np.all((inter_medoid_distances > 5) | (inter_medoid_distances == 0))
示例3: test_precomputed
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def test_precomputed():
"""Test the 'precomputed' distance metric."""
rng = np.random.RandomState(seed)
X_1 = [[1.0, 0.0], [1.1, 0.0], [0.0, 1.0], [0.0, 1.1]]
D_1 = euclidean_distances(X_1)
X_2 = [[1.1, 0.0], [0.0, 0.9]]
D_2 = euclidean_distances(X_2, X_1)
kmedoids = KMedoids(metric="precomputed", n_clusters=2, random_state=rng)
kmedoids.fit(D_1)
assert_allclose(kmedoids.inertia_, 0.2)
assert_array_equal(kmedoids.medoid_indices_, [2, 0])
assert_array_equal(kmedoids.labels_, [1, 1, 0, 0])
assert kmedoids.cluster_centers_ is None
med_1, med_2 = tuple(kmedoids.medoid_indices_)
predictions = kmedoids.predict(D_2)
assert_array_equal(predictions, [med_1 // 2, med_2 // 2])
transformed = kmedoids.transform(D_2)
assert_array_equal(transformed, D_2[:, kmedoids.medoid_indices_])
示例4: test_euclidean_distances
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def test_euclidean_distances(dtype, x_array_constr, y_array_constr):
# check that euclidean distances gives same result as scipy cdist
# when X and Y != X are provided
rng = np.random.RandomState(0)
X = rng.random_sample((100, 10)).astype(dtype, copy=False)
X[X < 0.8] = 0
Y = rng.random_sample((10, 10)).astype(dtype, copy=False)
Y[Y < 0.8] = 0
expected = cdist(X, Y)
X = x_array_constr(X)
Y = y_array_constr(Y)
distances = euclidean_distances(X, Y)
# the default rtol=1e-7 is too close to the float32 precision
# and fails due too rounding errors.
assert_allclose(distances, expected, rtol=1e-6)
assert distances.dtype == dtype
示例5: test_euclidean_distances_sym
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def test_euclidean_distances_sym(dtype, x_array_constr):
# check that euclidean distances gives same result as scipy pdist
# when only X is provided
rng = np.random.RandomState(0)
X = rng.random_sample((100, 10)).astype(dtype, copy=False)
X[X < 0.8] = 0
expected = squareform(pdist(X))
X = x_array_constr(X)
distances = euclidean_distances(X)
# the default rtol=1e-7 is too close to the float32 precision
# and fails due too rounding errors.
assert_allclose(distances, expected, rtol=1e-6)
assert distances.dtype == dtype
示例6: predict
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def predict(self, x):
"""
Applying multiple estimators for prediction.
Args:
x (numpy.ndarray): NxD array
Returns:
numpy.ndarray: predicted labels, Nx1 array
"""
confidences = []
for e in self.estimators:
confidence = np.ravel(e.decision_function(x))
confidences.append(confidence)
y = np.array(confidences).T
pred = euclidean_distances(y, self.codebook).argmin(axis=1)
return self.classes[pred]
示例7: _calc_objective_vector
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def _calc_objective_vector(x, labels):
clusters = {}
for i, label in enumerate(labels):
if label not in clusters:
clusters[label] = []
clusters[label].append(i)
result = np.zeros([1, x.shape[1]])
for i in range(x.shape[1]):
feature = 0
samples = x[:, i].T.reshape([x.shape[0], 1])
for label, cluster in clusters.items():
size = len(cluster)
cluster_samples = samples[cluster]
distances = euclidean_distances(cluster_samples)
feature += np.sum(distances) / size
result[0, i] = np.sum(euclidean_distances(samples)) / x.shape[0] - feature
return result
示例8: get_scaled_vectors
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def get_scaled_vectors(vectors, user_seed=None, n_components=12, normalize=True, progress=progress):
if user_seed:
seed = np.random.RandomState(seed=user_seed)
else:
seed = np.random.RandomState()
# FIXME: Make this optional:
from sklearn.metrics.pairwise import euclidean_distances as d
vectors = get_normalized_vectors(np.array(vectors)) if normalize else np.array(vectors)
# compute similarities based on d
progress.update('Computing similarity matrix')
similarities = d(vectors)
progress.update('Scaling using %d components' % n_components)
mds = manifold.MDS(n_components=n_components, max_iter=300, eps=1e-10, random_state=seed,
dissimilarity="precomputed", n_jobs=1)
progress.update('Fitting')
scaled_vectors = mds.fit(similarities).embedding_
return scaled_vectors
示例9: test_rbfize
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def test_rbfize():
X = np.random.normal(size=(20, 4))
dists = euclidean_distances(X)
median = np.median(dists[np.triu_indices_from(dists, k=1)])
rbf = RBFize(gamma=.25)
res = rbf.fit_transform(dists)
assert not hasattr(res, 'median_')
assert np.allclose(res, np.exp(-.25 * dists ** 2))
rbf = RBFize(gamma=.25, squared=True)
res = rbf.fit_transform(dists)
assert np.allclose(res, np.exp(-.25 * dists))
rbf = RBFize(gamma=4, scale_by_median=True)
res = rbf.fit_transform(dists)
assert np.allclose(rbf.median_, median)
assert np.allclose(res, np.exp((-4 * median**2) * dists ** 2))
rbf = RBFize(gamma=4, scale_by_median=True, squared=True)
res = rbf.fit_transform(dists)
assert np.allclose(rbf.median_, median)
assert np.allclose(res, np.exp((-4 * median) * dists))
示例10: _get_similarity_values
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def _get_similarity_values(self, q1_csc, q2_csc):
cosine_sim = []
manhattan_dis = []
eucledian_dis = []
jaccard_dis = []
minkowsk_dis = []
for i,j in zip(q1_csc, q2_csc):
sim = cs(i, j)
cosine_sim.append(sim[0][0])
sim = md(i, j)
manhattan_dis.append(sim[0][0])
sim = ed(i, j)
eucledian_dis.append(sim[0][0])
i_ = i.toarray()
j_ = j.toarray()
try:
sim = jsc(i_, j_)
jaccard_dis.append(sim)
except:
jaccard_dis.append(0)
sim = minkowski_dis.pairwise(i_, j_)
minkowsk_dis.append(sim[0][0])
return cosine_sim, manhattan_dis, eucledian_dis, jaccard_dis, minkowsk_dis
示例11: euclidean_distance_xy
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def euclidean_distance_xy(x, y, to_similar=False):
"""
欧式距离(L2范数)计算两个序列distance, g_euclidean_safe控制是否使用euclidean_distances计算
还是使用la.norm,效率差别如下:
euclidean_distances: 10000 loops, best of 3: 128 µs per loop
la.norm : 10000 loops, best of 3: 89.6 µs per loop
在需要批量且很耗时的情况下切好模式,否则不要切换
:param x: 可迭代序列
:param y: 可迭代序列
:param to_similar: 是否进行后置输出转换similar值
:return: float数值
"""
if g_euclidean_safe:
euclidean = lambda a, b: euclidean_distances(a, b)
else:
euclidean = lambda a, b: la.norm(a - b)
distance = _distance_xy(euclidean, x, y)
if to_similar:
# 实际上l1和l2转换similar的值不直观,只能对比使用
distance = 1.0 / (1.0 + distance)
return distance
示例12: test_init
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def test_init():
default = Spanning_Forest()
assert default.metric == skm.manhattan_distances
assert default.center == np.mean
assert default.reduction == np.sum
change = Spanning_Forest(dissimilarity=skm.euclidean_distances,
center=np.median, reduction=np.max)
assert change.metric == skm.euclidean_distances
assert change.center == np.median
assert change.reduction == np.max
sym = Spanning_Forest(affinity=skm.cosine_similarity)
assert isinstance(sym.metric, types.LambdaType)
test_distance = -np.log(skm.cosine_similarity(data[:2,]))
comparator = sym.metric(data[:2,])
np.testing.assert_allclose(test_distance, comparator)
示例13: _make_kernel
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def _make_kernel(X, Y, ktype, constant=0.1, degree=2.0, sigma=1.0):
# Linear kernel
if ktype == "linear":
return (X @ Y.T)
# Polynomial kernel
elif ktype == "poly":
return (X @ Y.T + constant) ** degree
# Gaussian kernel
elif ktype == "gaussian":
distmat = euclidean_distances(X, Y, squared=True)
return np.exp(-distmat / (2 * sigma ** 2))
# Linear diagonal kernel
elif ktype == "linear-diag":
return (X @ Y.T).diagonal()
# Polynomial diagonal kernel
elif ktype == "poly-diag":
return ((X @ Y.T + constant) ** degree).diagonal()
# Gaussian diagonal kernel
elif ktype == "gaussian-diag":
return np.exp(-np.sum(np.power((X-Y), 2), axis=1)/(2*sigma**2))
示例14: time_align_correlate
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def time_align_correlate(alignments, time):
time_dist = euclidean_distances(time, time)
assert(time_dist.shape == alignments.shape)
time_dists, scores = [], []
for i in range(time_dist.shape[0]):
for j in range(time_dist.shape[1]):
if i >= j:
continue
time_dists.append(time_dist[i, j])
scores.append(alignments[i, j])
print('Spearman rho = {}'.format(spearmanr(time_dists, scores)))
print('Pearson rho = {}'.format(pearsonr(time_dists, scores)))
示例15: L2retrieval
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import euclidean_distances [as 别名]
def L2retrieval(clips_embed, captions_embed, return_ranks = False):
captions_num = captions_embed.shape[0]
#index_list = []
ranks = np.zeros(captions_num)
top1 = np.zeros(captions_num)
import time
t1 = time.time()
d = euclidean_distances(captions_embed, clips_embed)
inds = np.argsort(d)
num = np.arange(captions_num).reshape(captions_num, 1)
ranks = np.where(inds == num)[1]
top1 = inds[:, 0]
t2 = time.time()
print((t2 - t1))
r1 = 100.0 * len(np.where(ranks < 1)[0]) / len(ranks)
r5 = 100.0 * len(np.where(ranks < 5)[0]) / len(ranks)
r10 = 100.0 * len(np.where(ranks < 10)[0]) / len(ranks)
r50 = 100.0 * len(np.where(ranks < 50)[0]) / len(ranks)
# r100 = 100.0 * len(np.where(ranks < 100)[0]) / len(ranks)
#plus 1 because the index starts from 0
medr = np.floor(np.median(ranks)) + 1
meanr = ranks.mean() + 1
if return_ranks:
return (r1, r5, r10, r50, medr, meanr), (ranks, top1)
else:
return (r1, r5, r10, r50, medr, meanr)