本文整理匯總了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)