溫馨提示×

SciPy中怎么計(jì)算兩個樣本集之間的距離和相似性

小億
105
2024-05-24 16:34:15
欄目: 編程語言

在SciPy中,可以使用scipy.spatial.distance模塊來計(jì)算兩個樣本集之間的距離和相似性。

  1. 計(jì)算距離:
from scipy.spatial import distance

# 樣本集合1
X = [[0, 1], [1, 1]]
# 樣本集合2
Y = [[2, 0], [3, 1]]

# 計(jì)算歐氏距離
euclidean_distance = distance.cdist(X, Y, 'euclidean')
print("Euclidean Distance:")
print(euclidean_distance)

# 計(jì)算曼哈頓距離
manhattan_distance = distance.cdist(X, Y, 'cityblock')
print("\nManhattan Distance:")
print(manhattan_distance)

# 計(jì)算閔可夫斯基距離
minkowski_distance = distance.cdist(X, Y, 'minkowski', p=2)
print("\nMinkowski Distance:")
print(minkowski_distance)
  1. 計(jì)算相似性:
from scipy.spatial import distance

# 樣本集合1
X = [[0, 1], [1, 1]]
# 樣本集合2
Y = [[2, 0], [3, 1]]

# 計(jì)算余弦相似度
cosine_similarity = 1 - distance.cdist(X, Y, 'cosine')
print("Cosine Similarity:")
print(cosine_similarity)

# 計(jì)算皮爾遜相關(guān)系數(shù)
pearson_similarity = 1 - distance.cdist(X, Y, 'correlation')
print("\nPearson Correlation:")
print(pearson_similarity)

以上代碼分別展示了如何計(jì)算歐氏距禮、曼哈頓距離、閔可夫斯基距離、余弦相似度和皮爾遜相關(guān)系數(shù)??梢愿鶕?jù)具體需求選擇合適的距離或相似性度量方法。

0