Curriculum
Course: SCIPY
Login
Text lesson

Distance Matrix

In data science, various distance metrics are used to calculate the distances between two points, such as Euclidean distance, cosine distance, and more. The distance between two vectors can represent not only the straight-line length between them but also factors like the angle between them from the origin or the number of unit steps required. The performance of many machine learning algorithms, such as “K Nearest Neighbors” and “K Means,” heavily depends on the choice of distance metrics. Let’s explore some of these distance metrics.

Euclidean Distance

Calculate the Euclidean distance between the given points.

Example

from scipy.spatial.distance import euclidean

p1 = (10)
p2 = (102)

res = euclidean(p1, p2)

print(res)

Result:

9.21954445729

Cityblock Distance (Manhattan Distance)

Is the distance calculated using four directions of movement, where movement is restricted to up, down, right, or left, but not diagonally.

Example

Calculate the cityblock distance between the given points.

from scipy.spatial.distance import cityblock

p1 = (10)
p2 = (102)

res = cityblock(p1, p2)

print(res)

Result:

11