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.
Calculate the Euclidean distance between the given points.
from scipy.spatial.distance import euclidean p1 = (1, 0) p2 = (10, 2) res = euclidean(p1, p2) print(res) |
9.21954445729 |
Is the distance calculated using four directions of movement, where movement is restricted to up, down, right, or left, but not diagonally.
Calculate the cityblock distance between the given points.
from scipy.spatial.distance import cityblock p1 = (1, 0) p2 = (10, 2) res = cityblock(p1, p2) print(res) |
11 |