2 Methods of Calulating the Distance between 2 Vectors

These days I’m coding with the K-means clustring. In this algorithm, we often need to calculate the distance between 2 vectors. The expression of disctace between vector x and y is:
$$ d=\sqrt{\sum{(x{i}-y{i})^{2}}} $$

Depend on this expression, we can write our code as:

1
2
3
4
5
import numpy as np
x=np.array([1,2,3,4])
y=np.array([0,1,0,1])
d = np.sqrt(((x-y)**2).sum())
print(d)

This code works well, but we can also use the numpy.linalg.norm to calculate the distance:

1
2
3
4
5
import numpy as np
x=np.array([1,2,3,4])
y=np.array([0,1,0,1])
d = np.linalg.norm(x-y)
print(d)

For those guys who are struggling with the math, maybe the norm is a better choice ;)

Actually numpy.linalg.norm is very powerful. It can calculate different kind of normalisations by giving different value to the ord parameter. Here is the doc of norm