在SciPy中,可以使用scipy.spatial.distance
模块来计算两个样本集之间的距离和相似性。
from scipy.spatial import distance
# 样本集合1
X = [[0, 1], [1, 1]]
# 样本集合2
Y = [[2, 0], [3, 1]]
# 计算欧氏距离
euclidean_distance = distance.cdist(X, Y, 'euclidean')
print("Euclidean Distance:")
print(euclidean_distance)
# 计算曼哈顿距离
manhattan_distance = distance.cdist(X, Y, 'cityblock')
print("\nManhattan Distance:")
print(manhattan_distance)
# 计算闵可夫斯基距离
minkowski_distance = distance.cdist(X, Y, 'minkowski', p=2)
print("\nMinkowski Distance:")
print(minkowski_distance)
from scipy.spatial import distance
# 样本集合1
X = [[0, 1], [1, 1]]
# 样本集合2
Y = [[2, 0], [3, 1]]
# 计算余弦相似度
cosine_similarity = 1 - distance.cdist(X, Y, 'cosine')
print("Cosine Similarity:")
print(cosine_similarity)
# 计算皮尔逊相关系数
pearson_similarity = 1 - distance.cdist(X, Y, 'correlation')
print("\nPearson Correlation:")
print(pearson_similarity)
以上代码分别展示了如何计算欧氏距禮、曼哈顿距离、闵可夫斯基距离、余弦相似度和皮尔逊相关系数。可以根据具体需求选择合适的距离或相似性度量方法。