欧氏距离(Euclidean Distance)
欧氏距离是最容易直观理解的距离度量方法,我们小学、初中和高中接触到的两个点在空间中的距离一般都是指欧氏距离。
Python 实现计算欧氏距离
import numpy as np x=np.random.random(10) y=np.random.random(10) #方法一:根据公式求解 d1=np.sqrt(np.sum(np.square(x-y))) #方法二:根据scipy库求解 from scipy.spatial.distance import pdist X=np.vstack([x,y]) d2=pdist(X)
1
2
3
4
5
6
7
8
9
10
11
12
|
import
numpy
as
np
x
=
np
.
random
.
random
(
10
)
y
=
np
.
random
.
random
(
10
)
#方法一:根据公式求解
d1
=
np
.
sqrt
(
np
.
sum
(
np
.
square
(
x
-
y
)
)
)
#方法二:根据scipy库求解
from
scipy
.
spatial
.
distance
import
pdist
X
=
np
.
vstack
(
[
x
,
y
]
)
d2
=
pdist
(
X
)
|