Distance computation (distance.cdist) in SciPy - Time & Space Complexity
When we calculate distances between two groups of points, it takes time depending on how many points there are.
We want to know how the time needed grows as the number of points grows.
Analyze the time complexity of the following code snippet.
from scipy.spatial import distance
import numpy as np
pointsA = np.random.rand(100, 3) # 100 points in 3D
pointsB = np.random.rand(200, 3) # 200 points in 3D
result = distance.cdist(pointsA, pointsB, 'euclidean')
This code computes the Euclidean distance between every point in pointsA and every point in pointsB.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calculating distance between each pair of points from two sets.
- How many times: For each of the 100 points in pointsA, distances to all 200 points in pointsB are computed, so 100 x 200 = 20,000 times.
As the number of points grows, the total distance calculations grow by multiplying the sizes of both sets.
| Input Size (pointsA x pointsB) | Approx. Operations |
|---|---|
| 10 x 10 | 100 |
| 100 x 100 | 10,000 |
| 1000 x 1000 | 1,000,000 |
Pattern observation: Doubling the number of points in both sets roughly squares the number of distance calculations.
Time Complexity: O(m × n)
This means the time grows proportionally to the product of the number of points in each set.
[X] Wrong: "The time depends only on the total number of points combined, like m + n."
[OK] Correct: Each point in one set is compared to every point in the other set, so the total work multiplies, not adds.
Understanding how distance calculations scale helps you explain performance in tasks like clustering or nearest neighbor search, showing you grasp practical data science challenges.
"What if we only compute distances between points in the same set (like pointsA to pointsA)? How would the time complexity change?"
Practice
What does the scipy.spatial.distance.cdist function do?
Solution
Step 1: Understand the function purpose
cdistcalculates distances between two groups of points, not sums or sorting.Step 2: Identify the output type
The function returns a matrix showing distances from each point in the first group to each point in the second.Final Answer:
Computes distances between two sets of points and returns a matrix of distances. -> Option CQuick Check:
cdist = distance matrix [OK]
- Thinking cdist sums points instead of distances
- Confusing cdist with sorting or random generation
- Assuming cdist returns a single distance value
Which of the following is the correct way to import and use cdist from scipy.spatial.distance?
?Solution
Step 1: Check correct import syntax
The correct import isfrom scipy.spatial.distance import cdist. Other forms are invalid or cause errors.Step 2: Confirm usage syntax
Callingcdist(A, B, metric='euclidean')is correct to compute Euclidean distances.Final Answer:
from scipy.spatial.distance import cdist result = cdist(A, B, metric='euclidean') -> Option DQuick Check:
Correct import and call = from scipy.spatial.distance import cdist result = cdist(A, B, metric='euclidean') [OK]
- Using incorrect import statements causing ImportError
- Trying to import cdist directly from scipy
- Using wrong syntax like 'import cdist from ...'
Given the code below, what is the output?
import numpy as np
from scipy.spatial.distance import cdist
A = np.array([[0, 0], [1, 1]])
B = np.array([[1, 0], [2, 2]])
result = cdist(A, B, metric='euclidean')
print(result)Solution
Step 1: Calculate distances from points in A to points in B
Distances: - From [0,0] to [1,0] = sqrt((1-0)^2 + (0-0)^2) = 1.0 - From [0,0] to [2,2] = sqrt((2-0)^2 + (2-0)^2) ≈ 2.8284 - From [1,1] to [1,0] = sqrt((1-1)^2 + (0-1)^2) = 1.0 - From [1,1] to [2,2] = sqrt((2-1)^2 + (2-1)^2) ≈ 1.4142Step 2: Form the distance matrix
Matrix rows correspond to points in A, columns to points in B: [[1.0, 2.8284], [1.0, 1.4142]]Final Answer:
[[1. 2.82842712] [1. 1.41421356]] -> Option AQuick Check:
Euclidean distances matrix = [[1. 2.82842712] [1. 1.41421356]] [OK]
- Mixing row and column order in the output matrix
- Using wrong distance formula (e.g., Manhattan instead of Euclidean)
- Rounding errors causing confusion
What is wrong with the following code snippet?
import numpy as np
from scipy.spatial.distance import cdist
A = np.array([[0, 0], [1, 1]])
B = np.array([1, 0])
result = cdist(A, B, metric='euclidean')
print(result)Solution
Step 1: Check input array shapes
cdist expects both inputs to be 2D arrays representing sets of points. Here, B is 1D, which is invalid.Step 2: Understand error cause
Passing 1D array B causes a shape mismatch error because cdist tries to interpret rows as points.Final Answer:
B should be a 2D array, but it is 1D, causing a shape error. -> Option BQuick Check:
Input arrays must be 2D for cdist [OK]
- Passing 1D arrays instead of 2D arrays
- Assuming cdist accepts lists only
- Using unsupported metric names
You have two sets of points representing locations of stores and customers:
stores = np.array([[0, 0], [5, 5], [10, 10]])
customers = np.array([[1, 1], [6, 6], [8, 8], [12, 12]])Using cdist, how can you find the closest store for each customer?
Solution
Step 1: Compute distance matrix from customers to stores
Usecdist(customers, stores)to get distances where each row is a customer and each column is a store.Step 2: Find closest store for each customer
For each customer (row), find the index of the minimum distance value. This index corresponds to the closest store.Final Answer:
Compute the distance matrix with cdist(customers, stores) and find the index of the minimum distance in each row. -> Option AQuick Check:
Closest store = min distance per customer row [OK]
- Swapping order of inputs causing wrong distance matrix shape
- Using max instead of min to find closest
- Summing distances instead of finding minimum
Ready to prove it?
Pass the test and this pattern moves unknown
