本文整理汇总了Python中sklearn.metrics.pairwise.PAIRWISE_DISTANCE_FUNCTIONS属性的典型用法代码示例。如果您正苦于以下问题:Python pairwise.PAIRWISE_DISTANCE_FUNCTIONS属性的具体用法?Python pairwise.PAIRWISE_DISTANCE_FUNCTIONS怎么用?Python pairwise.PAIRWISE_DISTANCE_FUNCTIONS使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类sklearn.metrics.pairwise
的用法示例。
在下文中一共展示了pairwise.PAIRWISE_DISTANCE_FUNCTIONS属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_paired_distances
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS [as 别名]
def test_paired_distances(metric, func):
# Test the pairwise_distance helper function.
rng = np.random.RandomState(0)
# Euclidean distance should be equivalent to calling the function.
X = rng.random_sample((5, 4))
# Euclidean distance, with Y != X.
Y = rng.random_sample((5, 4))
S = paired_distances(X, Y, metric=metric)
S2 = func(X, Y)
assert_array_almost_equal(S, S2)
S3 = func(csr_matrix(X), csr_matrix(Y))
assert_array_almost_equal(S, S3)
if metric in PAIRWISE_DISTANCE_FUNCTIONS:
# Check the pairwise_distances implementation
# gives the same value
distances = PAIRWISE_DISTANCE_FUNCTIONS[metric](X, Y)
distances = np.diag(distances)
assert_array_almost_equal(distances, S)
示例2: test_kmedoids_iris
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS [as 别名]
def test_kmedoids_iris():
"""Test kmedoids on the Iris dataset"""
rng = np.random.RandomState(seed)
X_iris = load_iris()["data"]
ref_model = KMeans(n_clusters=3).fit(X_iris)
avg_dist_to_closest_centroid = (
ref_model.transform(X_iris).min(axis=1).mean()
)
for init in ["random", "heuristic", "k-medoids++"]:
distance_metric = "euclidean"
model = KMedoids(
n_clusters=3, metric=distance_metric, init=init, random_state=rng
)
model.fit(X_iris)
# test convergence in reasonable number of steps
assert model.n_iter_ < (len(X_iris) // 10)
distances = PAIRWISE_DISTANCE_FUNCTIONS[distance_metric](X_iris)
avg_dist_to_random_medoid = np.mean(distances.ravel())
avg_dist_to_closest_medoid = model.inertia_ / X_iris.shape[0]
# We want distance-to-closest-medoid to be reduced from average
# distance by more than 50%
assert avg_dist_to_random_medoid > 2 * avg_dist_to_closest_medoid
# When K-Medoids is using Euclidean distance,
# we can compare its performance to
# K-Means. We want the average distance to cluster centers
# to be similar between K-Means and K-Medoids
assert_allclose(
avg_dist_to_closest_medoid, avg_dist_to_closest_centroid, rtol=0.1
)
示例3: test_paired_distances
# 需要导入模块: from sklearn.metrics import pairwise [as 别名]
# 或者: from sklearn.metrics.pairwise import PAIRWISE_DISTANCE_FUNCTIONS [as 别名]
def test_paired_distances():
# Test the pairwise_distance helper function.
rng = np.random.RandomState(0)
# Euclidean distance should be equivalent to calling the function.
X = rng.random_sample((5, 4))
# Euclidean distance, with Y != X.
Y = rng.random_sample((5, 4))
for metric, func in iteritems(PAIRED_DISTANCES):
S = paired_distances(X, Y, metric=metric)
S2 = func(X, Y)
assert_array_almost_equal(S, S2)
S3 = func(csr_matrix(X), csr_matrix(Y))
assert_array_almost_equal(S, S3)
if metric in PAIRWISE_DISTANCE_FUNCTIONS:
# Check the pairwise_distances implementation
# gives the same value
distances = PAIRWISE_DISTANCE_FUNCTIONS[metric](X, Y)
distances = np.diag(distances)
assert_array_almost_equal(distances, S)
# Check the callable implementation
S = paired_distances(X, Y, metric='manhattan')
S2 = paired_distances(X, Y, metric=lambda x, y: np.abs(x - y).sum(axis=0))
assert_array_almost_equal(S, S2)
# Test that a value error is raised when the lengths of X and Y should not
# differ
Y = rng.random_sample((3, 4))
assert_raises(ValueError, paired_distances, X, Y)