本文整理汇总了Python中sklearn.metrics.pairwise_distances方法的典型用法代码示例。如果您正苦于以下问题:Python metrics.pairwise_distances方法的具体用法?Python metrics.pairwise_distances怎么用?Python metrics.pairwise_distances使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.metrics
的用法示例。
在下文中一共展示了metrics.pairwise_distances方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_lof_precomputed
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def test_lof_precomputed(random_state=42):
"""Tests LOF with a distance matrix."""
# Note: smaller samples may result in spurious test success
rng = np.random.RandomState(random_state)
X = rng.random_sample((10, 4))
Y = rng.random_sample((3, 4))
DXX = metrics.pairwise_distances(X, metric='euclidean')
DYX = metrics.pairwise_distances(Y, X, metric='euclidean')
# As a feature matrix (n_samples by n_features)
lof_X = neighbors.LocalOutlierFactor(n_neighbors=3, novelty=True)
lof_X.fit(X)
pred_X_X = lof_X._predict()
pred_X_Y = lof_X.predict(Y)
# As a dense distance matrix (n_samples by n_samples)
lof_D = neighbors.LocalOutlierFactor(n_neighbors=3, algorithm='brute',
metric='precomputed', novelty=True)
lof_D.fit(DXX)
pred_D_X = lof_D._predict()
pred_D_Y = lof_D.predict(DYX)
assert_array_almost_equal(pred_X_X, pred_D_X)
assert_array_almost_equal(pred_X_Y, pred_D_Y)
示例2: test_simple_example
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def test_simple_example():
"""Test on a simple example.
Puts four points in the input space where the opposite labels points are
next to each other. After transform the samples from the same class
should be next to each other.
"""
X = np.array([[0, 0], [0, 1], [2, 0], [2, 1]])
y = np.array([1, 0, 1, 0])
nca = NeighborhoodComponentsAnalysis(n_components=2, init='identity',
random_state=42)
nca.fit(X, y)
X_t = nca.transform(X)
assert_array_equal(pairwise_distances(X_t).argsort()[:, 1],
np.array([2, 3, 0, 1]))
示例3: compute_heterogeneity
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def compute_heterogeneity(data, k, centroids, cluster_assignment):
heterogeneity = 0.0
for i in range(k):
# Select all data points that belong to cluster i. Fill in the blank (RHS only)
member_data_points = data[cluster_assignment == i, :]
if member_data_points.shape[0] > 0: # check if i-th cluster is non-empty
# Compute distances from centroid to data points (RHS only)
distances = pairwise_distances(
member_data_points, [centroids[i]], metric="euclidean"
)
squared_distances = distances ** 2
heterogeneity += np.sum(squared_distances)
return heterogeneity
示例4: get_similarities
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def get_similarities(query_feats, para_features, top=10, combine_feat_scores="mul"):
"""
Get similarities based on multiple independent queries that are then combined using combine_feat_scores
:param query_feats: Multiple vectorized text queries
:param para_features: Multiple vectorized text paragraphs that will be scored against the queries
:param top: Top N facts to keep
:param combine_feat_scores: The way for combining the multiple scores
:return: Ranked fact ids with scores List[tuple(id, weight)]
"""
scores_per_feat = [pairwise_distances(q_feat, para_features, "cosine").ravel() for q_feat in query_feats] # this is distance - low is better!!!
comb_func = comb_funcs[combine_feat_scores]
smoothing_val = 0.000001
max_val = pow((1 + smoothing_val), 2)
dists = scores_per_feat[0] + smoothing_val
if len(scores_per_feat) > 1:
for i in range(1, len(scores_per_feat)):
dists = comb_func(scores_per_feat[i] + smoothing_val, dists)
sorted_ix = np.argsort(dists).tolist() # this is asc (lowers first), in case of ties, uses the earlier paragraph
return [[i, (max_val - dists[i]) / max_val] for i in sorted_ix][:top]
示例5: combine_similarities
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def combine_similarities(scores_per_feat, top=10, combine_feat_scores="mul"):
"""
Get similarities based on multiple independent queries that are then combined using combine_feat_scores
:param query_feats: Multiple vectorized text queries
:param para_features: Multiple vectorized text paragraphs that will be scored against the queries
:param top: Top N facts to keep
:param combine_feat_scores: The way for combining the multiple scores
:return: Ranked fact ids with scores List[tuple(id, weight)]
"""
# scores_per_feat = [pairwise_distances(q_feat, para_features, "cosine").ravel() for q_feat in query_feats] # this is distance - low is better!!!
comb_func = comb_funcs[combine_feat_scores]
smoothing_val = 0.000001
max_val = pow((1 + smoothing_val), 2)
dists = scores_per_feat[0] + smoothing_val
if len(scores_per_feat) > 1:
for i in range(1, len(scores_per_feat)):
dists = comb_func(scores_per_feat[i] + smoothing_val, dists)
sorted_ix = np.argsort(dists).tolist() # this is asc (lowers first) ,in case of ties, uses the earlier paragraph
max_val = max(np.max(dists), 1)
return [[i, (max_val - dists[i]) / max_val] for i in sorted_ix][:top]
示例6: spatial_check
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def spatial_check(metric):
dist_matrix = pairwise_distances(spatial_data, metric=metric)
# scipy is bad sometimes
if metric == "braycurtis":
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 0.0
if metric in ("cosine", "correlation"):
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 1.0
# And because distance between all zero vectors should be zero
dist_matrix[10, 11] = 0.0
dist_matrix[11, 10] = 0.0
dist_function = dist.named_distances[metric]
test_matrix = np.array(
[
[
dist_function(spatial_data[i], spatial_data[j])
for j in range(spatial_data.shape[0])
]
for i in range(spatial_data.shape[0])
]
)
assert_array_almost_equal(
test_matrix,
dist_matrix,
err_msg="Distances don't match " "for metric {}".format(metric),
)
示例7: binary_check
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def binary_check(metric):
dist_matrix = pairwise_distances(binary_data, metric=metric)
if metric in ("jaccard", "dice", "sokalsneath", "yule"):
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 0.0
if metric in ("kulsinski", "russellrao"):
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 0.0
# And because distance between all zero vectors should be zero
dist_matrix[10, 11] = 0.0
dist_matrix[11, 10] = 0.0
dist_function = dist.named_distances[metric]
test_matrix = np.array(
[
[
dist_function(binary_data[i], binary_data[j])
for j in range(binary_data.shape[0])
]
for i in range(binary_data.shape[0])
]
)
assert_array_almost_equal(
test_matrix,
dist_matrix,
err_msg="Distances don't match " "for metric {}".format(metric),
)
示例8: test_seuclidean
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def test_seuclidean():
v = np.abs(np.random.randn(spatial_data.shape[1]))
dist_matrix = pairwise_distances(spatial_data, metric="seuclidean", V=v)
test_matrix = np.array(
[
[
dist.standardised_euclidean(spatial_data[i], spatial_data[j], v)
for j in range(spatial_data.shape[0])
]
for i in range(spatial_data.shape[0])
]
)
assert_array_almost_equal(
test_matrix,
dist_matrix,
err_msg="Distances don't match " "for metric seuclidean",
)
示例9: test_mahalanobis
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def test_mahalanobis():
v = np.cov(np.transpose(spatial_data))
dist_matrix = pairwise_distances(spatial_data, metric="mahalanobis", VI=v)
test_matrix = np.array(
[
[
dist.mahalanobis(spatial_data[i], spatial_data[j], v)
for j in range(spatial_data.shape[0])
]
for i in range(spatial_data.shape[0])
]
)
assert_array_almost_equal(
test_matrix,
dist_matrix,
err_msg="Distances don't match " "for metric mahalanobis",
)
示例10: sparse_spatial_check
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def sparse_spatial_check(metric, sparse_spatial_data):
# Check that metric is supported for this test, otherwise, fail!
assert (
metric in spdist.sparse_named_distances
), f"{metric} not supported for sparse data"
dist_matrix = pairwise_distances(sparse_spatial_data.todense(), metric=metric)
if metric in ("braycurtis", "dice", "sokalsneath", "yule"):
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 0.0
if metric in ("cosine", "correlation", "kulsinski", "russellrao"):
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 1.0
# And because distance between all zero vectors should be zero
dist_matrix[10, 11] = 0.0
dist_matrix[11, 10] = 0.0
run_test_sparse_metric(metric, sparse_spatial_data, dist_matrix)
示例11: sparse_binary_check
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def sparse_binary_check(metric, sparse_binary_data):
# Check that metric is supported for this test, otherwise, fail!
assert (
metric in spdist.sparse_named_distances
), f"{metric} not supported for sparse data"
dist_matrix = pairwise_distances(sparse_binary_data.todense(), metric=metric)
if metric in ("jaccard", "dice", "sokalsneath", "yule"):
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 0.0
if metric in ("kulsinski", "russellrao"):
dist_matrix[np.where(~np.isfinite(dist_matrix))] = 1.0
# And because distance between all zero vectors should be zero
dist_matrix[10, 11] = 0.0
dist_matrix[11, 10] = 0.0
run_test_sparse_metric(metric, sparse_binary_data, dist_matrix)
# --------------------
# Spatial Metric Tests
# --------------------
示例12: test_weighted_minkowski
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def test_weighted_minkowski(spatial_data):
v = np.abs(np.random.randn(spatial_data.shape[1]))
dist_matrix = pairwise_distances(spatial_data, metric="wminkowski", w=v, p=3)
test_matrix = np.array(
[
[
dist.weighted_minkowski(spatial_data[i], spatial_data[j], v, p=3)
for j in range(spatial_data.shape[0])
]
for i in range(spatial_data.shape[0])
]
)
assert_array_almost_equal(
test_matrix,
dist_matrix,
err_msg="Distances don't match " "for metric weighted_minkowski",
)
示例13: test_mahalanobis
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def test_mahalanobis(spatial_data):
v = np.cov(np.transpose(spatial_data))
dist_matrix = pairwise_distances(spatial_data, metric="mahalanobis", VI=v)
test_matrix = np.array(
[
[
dist.mahalanobis(spatial_data[i], spatial_data[j], v)
for j in range(spatial_data.shape[0])
]
for i in range(spatial_data.shape[0])
]
)
assert_array_almost_equal(
test_matrix,
dist_matrix,
err_msg="Distances don't match " "for metric mahalanobis",
)
示例14: gaussian
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def gaussian(x, workers=None):
"""Default medial gaussian kernel similarity calculation"""
l1 = pairwise_distances(X=x, metric="l1", n_jobs=workers)
n = l1.shape[0]
med = np.median(
np.lib.stride_tricks.as_strided(
l1, (n - 1, n + 1), (l1.itemsize * (n + 1), l1.itemsize)
)[:, 1:]
)
# prevents division by zero when used on label vectors
med = med if med else 1
gamma = 1.0 / (2 * (med ** 2))
return rbf_kernel(x, gamma=gamma)
# p-value computation
示例15: _compute_isc
# 需要导入模块: from sklearn import metrics [as 别名]
# 或者: from sklearn.metrics import pairwise_distances [as 别名]
def _compute_isc(data, metric='median'):
''' Helper function to compute intersubject correlation from observations by subjects array.
Args:
data: (pd.DataFrame, np.array) observations by subjects where isc is computed across subjects
metric: (str) type of association metric ['spearman','pearson','kendall']
Returns:
isc: (float) intersubject correlation coefficient
'''
from nltools.data import Adjacency
similarity = Adjacency(1 - pairwise_distances(data.T, metric='correlation'), matrix_type='similarity')
if metric =='mean':
isc = np.tanh(similarity.r_to_z().mean())
elif metric =='median':
isc = similarity.median()
return isc