We use distance matrix computation to find how far apart items are from each other. This helps us understand relationships or groupings in data.
0
0
Distance matrix computation in SciPy
Introduction
Comparing distances between cities on a map.
Finding similar customers based on their shopping habits.
Grouping animals by their features in biology.
Measuring how close different documents are in text analysis.
Syntax
SciPy
from scipy.spatial import distance_matrix distance_matrix(X, Y=None, p=2, threshold=1000.0)
X and Y are arrays of points (rows are points, columns are features).
p sets the distance type: 2 means Euclidean (straight line).
Examples
Compute distances between points in
X itself.SciPy
from scipy.spatial import distance_matrix import numpy as np X = np.array([[0, 0], [1, 1]]) D = distance_matrix(X, X) print(D)
Compute distances from points in
X to points in Y.SciPy
from scipy.spatial import distance_matrix import numpy as np X = np.array([[0, 0], [1, 1]]) Y = np.array([[2, 2]]) D = distance_matrix(X, Y) print(D)
Sample Program
This program calculates the distances between each point in X and each point in Y. The result is a matrix where each row corresponds to a point in X and each column to a point in Y.
SciPy
from scipy.spatial import distance_matrix import numpy as np # Define two sets of points X = np.array([[0, 0], [3, 4], [6, 8]]) Y = np.array([[0, 0], [1, 1]]) # Compute the distance matrix D = distance_matrix(X, Y) print(D)
OutputSuccess
Important Notes
The distance matrix is always a 2D array with shape (len(X), len(Y)).
Euclidean distance (p=2) is the most common, but you can use other distances by changing p.
If Y is not given, distances are computed between points in X itself.
Summary
Distance matrix shows how far points are from each other.
Use scipy.spatial.distance_matrix to compute it easily.
It helps in clustering, similarity, and spatial analysis tasks.