Distance metrics help us measure how far apart two points or objects are. They are useful to compare things in many areas like shopping, sports, or health.
0
0
Distance metrics (euclidean, cosine, manhattan) in SciPy
Introduction
To find the closest store to your home based on location coordinates.
To compare how similar two songs are by their features.
To measure the difference between two people's health data.
To group customers with similar buying habits.
To check how different two images are by their pixel values.
Syntax
SciPy
from scipy.spatial import distance # Euclidean distance distance.euclidean(point1, point2) # Cosine distance distance.cosine(point1, point2) # Manhattan distance distance.cityblock(point1, point2)
point1 and point2 are lists or arrays of numbers representing coordinates or features.
Euclidean is straight-line distance, Cosine measures angle difference, Manhattan sums absolute differences.
Examples
Calculates straight-line distance between points (1,2) and (4,6).
SciPy
distance.euclidean([1, 2], [4, 6])
Measures how different two vectors are in direction.
SciPy
distance.cosine([1, 0, 0], [0, 1, 0])
Calculates total absolute difference along each dimension.
SciPy
distance.cityblock([1, 2, 3], [4, 0, 6])
Sample Program
This program calculates three types of distances between two points and prints them with two decimals for clarity.
SciPy
from scipy.spatial import distance pointA = [3, 4] pointB = [7, 1] # Calculate Euclidean distance euclid = distance.euclidean(pointA, pointB) # Calculate Cosine distance cosine = distance.cosine(pointA, pointB) # Calculate Manhattan distance manhattan = distance.cityblock(pointA, pointB) print(f"Euclidean distance: {euclid:.2f}") print(f"Cosine distance: {cosine:.2f}") print(f"Manhattan distance: {manhattan}")
OutputSuccess
Important Notes
Euclidean distance is like measuring with a ruler in straight line.
Cosine distance is good when you care about direction, not size.
Manhattan distance is like walking city blocks, adding up all steps.
Summary
Distance metrics measure how far or different two points are.
Euclidean is straight-line, Cosine is angle-based, Manhattan sums absolute differences.
Use scipy.spatial.distance functions to calculate these easily in Python.