Bird
Raised Fist0
SciPydata~20 mins

Distance computation (distance.cdist) in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Distance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Euclidean distance matrix
What is the output of the following code that computes Euclidean distances between two sets of points?
SciPy
import numpy as np
from scipy.spatial import distance

A = np.array([[0, 0], [1, 1]])
B = np.array([[1, 0], [2, 2]])
result = distance.cdist(A, B, 'euclidean')
print(result)
A
[[1.41421356 2.23606798]
 [1.         1.41421356]]
B
[[1.         2.23606798]
 [1.41421356 1.        ]]
C
[[1.         2.23606798]
 [1.         1.41421356]]
D
[[1.         2.82842712]
 [1.         1.41421356]]
Attempts:
2 left
💡 Hint
Remember Euclidean distance is the straight line distance between points.
❓ data_output
intermediate
1:30remaining
Number of distances computed with cityblock metric
Given two arrays A and B, how many distances are computed by distance.cdist(A, B, 'cityblock')?
SciPy
import numpy as np
from scipy.spatial import distance

A = np.array([[1, 2], [3, 4], [5, 6]])
B = np.array([[7, 8], [9, 10]])
result = distance.cdist(A, B, 'cityblock')
print(result.shape[0] * result.shape[1])
A3
B5
C2
D6
Attempts:
2 left
💡 Hint
The output shape is (number of rows in A, number of rows in B).
🔧 Debug
advanced
2:00remaining
Identify the error in distance.cdist usage
What error will this code raise and why?
SciPy
import numpy as np
from scipy.spatial import distance

A = np.array([[1, 2, 3], [4, 5, 6]])
B = np.array([[7, 8]])
result = distance.cdist(A, B, 'euclidean')
ANo error, runs successfully
BValueError: A and B must have the same number of columns
CIndexError: index out of bounds
DTypeError: Unsupported metric 'euclidean'
Attempts:
2 left
💡 Hint
Check the shape of A and B carefully.
❓ visualization
advanced
2:30remaining
Visualizing cosine distances between points
Which option correctly plots the cosine distance matrix heatmap between points in A and B?
SciPy
import numpy as np
from scipy.spatial import distance
import matplotlib.pyplot as plt

A = np.array([[1, 0], [0, 1]])
B = np.array([[1, 1], [-1, 0]])
dist_matrix = distance.cdist(A, B, 'cosine')
plt.imshow(dist_matrix, cmap='viridis')
plt.colorbar()
plt.show()
AA heatmap with values close to 0 and 1, showing cosine distances between points.
BA bar chart of distances between points.
CA line plot of distances over index.
DA scatter plot of points A and B.
Attempts:
2 left
💡 Hint
Cosine distance values range between 0 and 1 and are best shown as a heatmap.
🧠 Conceptual
expert
2:00remaining
Understanding distance.cdist with custom metric
Which statement about using a custom metric function with distance.cdist is TRUE?
Adistance.cdist does not support custom metric functions.
BThe custom metric function can accept arrays of any shape and return an array of distances.
CThe custom metric function must accept two 1-D arrays and return a scalar distance.
DThe custom metric function must return a boolean indicating if points are equal.
Attempts:
2 left
💡 Hint
Think about how distance.cdist computes pairwise distances.

Practice

(1/5)
1.

What does the scipy.spatial.distance.cdist function do?

easy
A. Generates random points within a specified range.
B. Calculates the sum of all points in a single array.
C. Computes distances between two sets of points and returns a matrix of distances.
D. Sorts points in ascending order based on their coordinates.

Solution

  1. Step 1: Understand the function purpose

    cdist calculates distances between two groups of points, not sums or sorting.
  2. 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.
  3. Final Answer:

    Computes distances between two sets of points and returns a matrix of distances. -> Option C
  4. Quick Check:

    cdist = distance matrix [OK]
Hint: Remember: cdist = cross distances between two point sets [OK]
Common Mistakes:
  • Thinking cdist sums points instead of distances
  • Confusing cdist with sorting or random generation
  • Assuming cdist returns a single distance value
2.

Which of the following is the correct way to import and use cdist from scipy.spatial.distance?

?
easy
A. from scipy import cdist result = cdist(A, B, metric='euclidean')
B. import scipy.distance.cdist result = cdist(A, B, metric='euclidean')
C. import cdist from scipy.spatial.distance result = cdist(A, B, metric='euclidean')
D. from scipy.spatial.distance import cdist result = cdist(A, B, metric='euclidean')

Solution

  1. Step 1: Check correct import syntax

    The correct import is from scipy.spatial.distance import cdist. Other forms are invalid or cause errors.
  2. Step 2: Confirm usage syntax

    Calling cdist(A, B, metric='euclidean') is correct to compute Euclidean distances.
  3. Final Answer:

    from scipy.spatial.distance import cdist result = cdist(A, B, metric='euclidean') -> Option D
  4. Quick Check:

    Correct import and call = from scipy.spatial.distance import cdist result = cdist(A, B, metric='euclidean') [OK]
Hint: Use 'from scipy.spatial.distance import cdist' to import [OK]
Common Mistakes:
  • Using incorrect import statements causing ImportError
  • Trying to import cdist directly from scipy
  • Using wrong syntax like 'import cdist from ...'
3.

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)
medium
A. [[1. 2.82842712] [1. 1.41421356]]
B. [[1.41421356 2.23606798] [1. 1.41421356]]
C. [[1. 1.41421356] [1.41421356 2.23606798]]
D. [[0. 1.41421356] [1. 2.23606798]]

Solution

  1. 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.4142
  2. Step 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]]
  3. Final Answer:

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

    Euclidean distances matrix = [[1. 2.82842712] [1. 1.41421356]] [OK]
Hint: Calculate each pair distance carefully, row by row [OK]
Common Mistakes:
  • Mixing row and column order in the output matrix
  • Using wrong distance formula (e.g., Manhattan instead of Euclidean)
  • Rounding errors causing confusion
4.

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)
medium
A. The metric 'euclidean' is not supported by cdist.
B. B should be a 2D array, but it is 1D, causing a shape error.
C. cdist requires both inputs to be lists, not numpy arrays.
D. The import statement for cdist is incorrect.

Solution

  1. 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.
  2. Step 2: Understand error cause

    Passing 1D array B causes a shape mismatch error because cdist tries to interpret rows as points.
  3. Final Answer:

    B should be a 2D array, but it is 1D, causing a shape error. -> Option B
  4. Quick Check:

    Input arrays must be 2D for cdist [OK]
Hint: Always use 2D arrays for both inputs in cdist [OK]
Common Mistakes:
  • Passing 1D arrays instead of 2D arrays
  • Assuming cdist accepts lists only
  • Using unsupported metric names
5.

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?

hard
A. Compute the distance matrix with cdist(customers, stores) and find the index of the minimum distance in each row.
B. Compute the distance matrix with cdist(stores, customers) and find the index of the maximum distance in each column.
C. Use cdist(stores, customers) and sum all distances to find closest stores.
D. Calculate Euclidean distance manually for each pair without using cdist.

Solution

  1. Step 1: Compute distance matrix from customers to stores

    Use cdist(customers, stores) to get distances where each row is a customer and each column is a store.
  2. 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.
  3. Final Answer:

    Compute the distance matrix with cdist(customers, stores) and find the index of the minimum distance in each row. -> Option A
  4. Quick Check:

    Closest store = min distance per customer row [OK]
Hint: cdist(customers, stores), then argmin per row finds closest store [OK]
Common Mistakes:
  • Swapping order of inputs causing wrong distance matrix shape
  • Using max instead of min to find closest
  • Summing distances instead of finding minimum
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard