Bird
0
0

You want to compute a distance matrix but only for points within a maximum distance threshold to save memory. Which method best achieves this using scipy?

hard📝 Application Q9 of 15
SciPy - Clustering and Distance
You want to compute a distance matrix but only for points within a maximum distance threshold to save memory. Which method best achieves this using scipy?
ACompute full distance matrix then mask distances above threshold
BCompute distances manually with nested loops and break early
CUse <code>distance_matrix</code> with a max_distance parameter
DUse <code>scipy.spatial.cKDTree</code> to query neighbors within threshold
Step-by-Step Solution
Solution:
  1. Step 1: Understand distance_matrix limitations

    It computes full matrix; no max_distance parameter exists.
  2. Step 2: Use efficient neighbor search

    scipy.spatial.cKDTree supports querying points within a radius efficiently.
  3. Final Answer:

    Use scipy.spatial.cKDTree to query neighbors within threshold -> Option D
  4. Quick Check:

    cKDTree supports radius queries, distance_matrix does not [OK]
Quick Trick: Use cKDTree for radius-based neighbor queries, not distance_matrix [OK]
Common Mistakes:
  • Expecting distance_matrix to support max distance filtering
  • Trying manual loops instead of efficient tree queries
  • Masking after full matrix wastes memory

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes