Bird
0
0

What is wrong with this code snippet?

medium📝 Debug Q7 of 15
SciPy - Clustering and Distance
What is wrong with this code snippet?
from scipy.cluster.hierarchy import dendrogram
Z = [[0, 1, 0.5, 2], [1, 2, 0.7, 2]]
dn = dendrogram(Z)
AZ is not a valid linkage matrix
BMissing import of matplotlib.pyplot
Cdendrogram requires a distance matrix, not linkage
DZ must be a numpy array, not a list
Step-by-Step Solution
Solution:
  1. Step 1: Check linkage matrix format

    Z must be a valid linkage matrix with correct shape and values.
  2. Step 2: Identify issue with Z

    Z is a list but may not represent a valid linkage matrix (e.g., distances or cluster indices incorrect).
  3. Final Answer:

    Z is not a valid linkage matrix -> Option A
  4. Quick Check:

    Linkage matrix must be valid for dendrogram [OK]
Quick Trick: Linkage matrix must be valid and correctly formatted [OK]
Common Mistakes:
  • Passing invalid linkage data
  • Confusing distance matrix with linkage
  • Assuming matplotlib import is required

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes