Bird
0
0

What is the issue with this code snippet?

medium📝 Debug Q6 of 15
SciPy - Clustering and Distance
What is the issue with this code snippet?
import numpy as np
from scipy.spatial import distance_matrix
A = [1, 2, 3]
B = [4, 5, 6]
dist = distance_matrix(A, B)
ALists are not valid inputs; inputs must be 2D arrays representing points
BThe function distance_matrix does not exist in scipy.spatial
CThe code is missing an import for numpy
Ddistance_matrix requires the points to be of the same length
Step-by-Step Solution
Solution:
  1. Step 1: Check input types

    The inputs A and B are Python lists of 1D values, not arrays of points.
  2. Step 2: Understand expected input format

    distance_matrix expects 2D arrays where each row is a point (e.g., shape (n_points, n_dimensions)).
  3. Step 3: Correct usage

    Convert lists to 2D numpy arrays, e.g., np.array([[1], [2], [3]]) to represent points in 1D space.
  4. Final Answer:

    Lists are not valid inputs; inputs must be 2D arrays representing points -> Option A
  5. Quick Check:

    Passing 1D lists causes errors [OK]
Quick Trick: Inputs must be 2D arrays, not 1D lists [OK]
Common Mistakes:
  • Passing 1D lists instead of 2D arrays
  • Assuming distance_matrix accepts flat lists
  • Ignoring input shape requirements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes