Bird
Raised Fist0
SciPydata~10 mins

Distance matrix computation in SciPy - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Distance matrix computation
Start with data points
Choose distance metric
Compute pairwise distances
Store results in matrix
Use matrix for analysis
We start with data points, pick a distance type, compute distances between all pairs, and save them in a matrix for further use.
Execution Sample
SciPy
from scipy.spatial import distance_matrix
import numpy as np

points = np.array([[1, 2], [4, 6], [7, 8]])
dist_mat = distance_matrix(points, points)
print(dist_mat)
This code calculates the distance matrix for three 2D points using Euclidean distance.
Execution Table
StepActionPoints involvedDistance computedMatrix update
1Start with points[[1,2],[4,6],[7,8]]-Matrix empty
2Compute distance between point 0 and 0[1,2] & [1,2]0.0dist_mat[0,0] = 0.0
3Compute distance between point 0 and 1[1,2] & [4,6]5.0dist_mat[0,1] = 5.0
4Compute distance between point 0 and 2[1,2] & [7,8]8.48528137423857dist_mat[0,2] = 8.48528137423857
5Compute distance between point 1 and 0[4,6] & [1,2]5.0dist_mat[1,0] = 5.0
6Compute distance between point 1 and 1[4,6] & [4,6]0.0dist_mat[1,1] = 0.0
7Compute distance between point 1 and 2[4,6] & [7,8]3.605551275463989dist_mat[1,2] = 3.605551275463989
8Compute distance between point 2 and 0[7,8] & [1,2]8.48528137423857dist_mat[2,0] = 8.48528137423857
9Compute distance between point 2 and 1[7,8] & [4,6]3.605551275463989dist_mat[2,1] = 3.605551275463989
10Compute distance between point 2 and 2[7,8] & [7,8]0.0dist_mat[2,2] = 0.0
11All pairs computed--Distance matrix complete
💡 All pairwise distances computed and stored in the matrix.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7After Step 10Final
dist_matempty[[0.0, 0, 0], [0, 0, 0], [0, 0, 0]][[0.0, 5.0, 8.485], [0, 0, 0], [0, 0, 0]][[0.0, 5.0, 8.485], [5.0, 0.0, 3.606], [0, 0, 0]][[0.0, 5.0, 8.485], [5.0, 0.0, 3.606], [8.485, 3.606, 0.0]][[0.0, 5.0, 8.48528137], [5.0, 0.0, 3.60555128], [8.48528137, 3.60555128, 0.0]]
Key Moments - 3 Insights
Why is the distance between a point and itself always zero?
Because the distance formula measures how far apart two points are, and a point is zero units away from itself. See execution_table rows 2, 6, and 10 where distances are 0.0.
Why is the distance matrix symmetric?
Distance from point A to B is the same as from B to A, so dist_mat[i,j] equals dist_mat[j,i]. This is shown in rows 3 and 5, 4 and 8, 7 and 9.
What happens if we use different sets of points for rows and columns?
The matrix will have distances between each point in the first set to each point in the second set, not necessarily square or symmetric. Here, both sets are the same, so matrix is square and symmetric.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the distance computed between points [1,2] and [7,8]?
A8.48528137423857
B5.0
C3.605551275463989
D0.0
💡 Hint
Check the 'Distance computed' column at step 4 in execution_table.
At which step does the distance between points [4,6] and [7,8] get computed?
AStep 9
BStep 6
CStep 7
DStep 3
💡 Hint
Look for the row where points involved are [4,6] & [7,8] in execution_table.
If we add a new point [0,0], how will the size of the distance matrix change?
AIt will remain 3x3
BIt will become 4x4
CIt will become 3x4
DIt will become 4x3
💡 Hint
Distance matrix size is number of points by number of points, see variable_tracker for matrix size.
Concept Snapshot
Distance matrix computation:
- Input: list/array of points
- Use scipy.spatial.distance_matrix(pointsA, pointsB)
- Computes pairwise distances (default Euclidean)
- Returns matrix with distances between each pair
- Matrix is square and symmetric if pointsA == pointsB
Full Transcript
Distance matrix computation starts with a set of points. We pick a distance metric, usually Euclidean. Then, for every pair of points, we calculate how far apart they are. These distances fill a matrix where each cell shows the distance between two points. The diagonal is zero because a point is zero distance from itself. The matrix is symmetric because distance from A to B equals distance from B to A. This matrix helps in many data science tasks like clustering or nearest neighbor search.

Practice

(1/5)
1. What does the scipy.spatial.distance_matrix function compute?
easy
A. The average value of a list of numbers
B. The sum of all points in a dataset
C. The distances between all pairs of points in two sets
D. The maximum value in a dataset

Solution

  1. Step 1: Understand the function purpose

    scipy.spatial.distance_matrix calculates distances between points, not sums or averages.
  2. Step 2: Identify what is computed

    It returns a matrix showing distances between each point in one set to each point in another set.
  3. Final Answer:

    The distances between all pairs of points in two sets -> Option C
  4. Quick Check:

    Distance matrix = pairwise distances [OK]
Hint: Distance matrix = all pair distances between points [OK]
Common Mistakes:
  • Confusing distance matrix with sum or average calculations
  • Thinking it returns a single distance value
  • Assuming it only works for one set of points
2. Which of the following is the correct way to import the distance_matrix function from scipy?
easy
A. import scipy.distance_matrix
B. import distance_matrix from scipy.spatial
C. from scipy import distance_matrix
D. from scipy.spatial import distance_matrix

Solution

  1. Step 1: Recall correct import syntax

    Functions inside modules are imported using from module import function.
  2. Step 2: Match with scipy structure

    distance_matrix is inside scipy.spatial, so correct import is from scipy.spatial import distance_matrix.
  3. Final Answer:

    from scipy.spatial import distance_matrix -> Option D
  4. Quick Check:

    Correct import syntax = from scipy.spatial import distance_matrix [OK]
Hint: Use 'from module import function' for specific imports [OK]
Common Mistakes:
  • Using 'import scipy.distance_matrix' which is invalid
  • Trying 'from scipy import distance_matrix' ignoring submodules
  • Incorrect order like 'import distance_matrix from ...'
3. What is the output of this code?
import numpy as np
from scipy.spatial import distance_matrix
points1 = np.array([[0, 0], [1, 1]])
points2 = np.array([[1, 0], [2, 2]])
dm = distance_matrix(points1, points2)
print(dm)
medium
A. [[1. 2.82842712] [1. 1.41421356]]
B. [[0. 1.41421356] [1. 2.23606798]]
C. [[1.41421356 2.23606798] [0. 1.41421356]]
D. [[1. 1.41421356] [1.41421356 2.82842712]]

Solution

  1. Step 1: Calculate distances from points1 to points2

    Distance between (0,0) and (1,0) is 1.0; between (0,0) and (2,2) is sqrt(4+4)=2.8284.
  2. Step 2: Calculate distances for second point

    Distance between (1,1) and (1,0) is 1.0; between (1,1) and (2,2) is sqrt(1+1)=1.4142.
  3. Final Answer:

    [[1. 2.82842712] [1. 1.41421356]] -> Option A
  4. Quick Check:

    Distance matrix matches calculated values [OK]
Hint: Calculate Euclidean distances pairwise for matrix [OK]
Common Mistakes:
  • Mixing order of points causing wrong matrix
  • Using Manhattan distance instead of Euclidean
  • Confusing rows and columns in output
4. Identify the error in this code snippet:
import numpy as np
from scipy.spatial import distance_matrix
points = np.array([[0, 0], [1, 1]])
dm = distance_matrix(points)
print(dm)
medium
A. distance_matrix cannot handle integer arrays
B. distance_matrix requires two arguments, but only one is given
C. numpy array must be 1D, but points is 2D
D. print statement syntax is incorrect

Solution

  1. Step 1: Check function parameters

    distance_matrix needs two arrays of points to compute distances between them.
  2. Step 2: Identify missing argument

    Only one argument points is passed, so it will raise a TypeError.
  3. Final Answer:

    distance_matrix requires two arguments, but only one is given -> Option B
  4. Quick Check:

    Missing second argument error [OK]
Hint: distance_matrix needs two point sets as input [OK]
Common Mistakes:
  • Passing only one array instead of two
  • Assuming it computes distances within one set automatically
  • Ignoring function signature requirements
5. You have two sets of points:
pointsA = [[0, 0], [3, 4], [6, 8]]
pointsB = [[0, 0], [0, 5]]

You want to find which point in pointsA is closest to any point in pointsB. Which approach using scipy.spatial.distance_matrix is correct?
hard
A. Compute the distance matrix, then find the minimum distance in each row
B. Compute the distance matrix, then sum all distances and pick the smallest sum
C. Compute the distance matrix, then find the maximum distance in each column
D. Compute the distance matrix, then average all distances and pick the largest average

Solution

  1. Step 1: Compute distance matrix between pointsA and pointsB

    This gives distances from each point in pointsA to each point in pointsB.
  2. Step 2: Find minimum distance per point in pointsA

    For each row (point in pointsA), find the smallest distance to any point in pointsB to identify closest point.
  3. Final Answer:

    Compute the distance matrix, then find the minimum distance in each row -> Option A
  4. Quick Check:

    Closest point = min distance per row [OK]
Hint: Minimum distance per row shows closest point [OK]
Common Mistakes:
  • Using sum or average instead of minimum distance
  • Finding maximum distance which is farthest, not closest
  • Confusing rows and columns in the matrix