Bird
0
0

What is wrong with this code?

medium📝 Debug Q7 of 15
SciPy - Clustering and Distance
What is wrong with this code?
import numpy as np
from scipy.spatial import distance_matrix
A = np.array([[0, 0], [1, 1]])
B = np.array([[1, 0], [2, 2]])
dist = distance_matrix(B, A)
print(dist[0, 1])
AThe indices are reversed; should be dist[1, 0]
BNo error; output is correct
Cdistance_matrix requires inputs to be the same shape
Ddistance_matrix does not support indexing
Step-by-Step Solution
Solution:
  1. Step 1: Understand input order and output shape

    Distance matrix shape is (len(B), len(A)) = (2, 2), so dist[0, 1] is valid.
  2. Step 2: Confirm indexing is valid

    Indexing into the matrix is allowed and returns the distance from B[0] to A[1].
  3. Final Answer:

    No error; output is correct -> Option B
  4. Quick Check:

    distance_matrix output can be indexed normally [OK]
Quick Trick: Distance matrix shape depends on input order; indexing is valid [OK]
Common Mistakes:
  • Assuming inputs must be same shape
  • Thinking indexing is invalid
  • Confusing row and column indices

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes