使用 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
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP