使用 SciPy 計算歐幾里得距離


歐幾里得距離是兩個實值向量的距離。我們大多使用它來計算兩行具有數字值(浮點值或整數值)的資料之間的距離。以下是計算歐幾里得距離的公式 −

$$\mathrm{d(r,s) =\sqrt{\sum_{i=1}^{n}(s_i-r_i)^2} }$$

此處,

r 和 s 是歐幾里得 n 維空間中的兩個點。

si 和 ri 是歐幾里得向量。

n 表示 n 維空間。

讓我們看看如何使用 SciPy 庫計算兩個點之間的歐幾里得距離 −

示例

# Importing the SciPy library
from scipy.spatial import distance
# Defining the points
A = (1, 2, 3, 4, 5, 6)
B = (7, 8, 9, 10, 11, 12)
A, B
# Computing the Euclidean distance
euclidean_distance = distance.euclidean(A, B)
print('Euclidean Distance b/w', A, 'and', B, 'is: ', euclidean_distance)

輸出

((1, 2, 3, 4, 5, 6), (7, 8, 9, 10, 11, 12))

Euclidean Distance b/w (1, 2, 3, 4, 5, 6) and (7, 8, 9, 10, 11, 12) is: 1
4.696938456699069

Updated on: 14-Dec-2021

760 檢視

開啟您的 事業

完成課程獲得認證

開始
廣告
© . All rights reserved.