Bird
0
0

What is the output of this code snippet?

medium📝 Predict Output Q13 of 15
SciPy - Clustering and Distance
What is the output of this code snippet?
from scipy.cluster.hierarchy import linkage
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6]])
Z = linkage(X, method='single')
print(Z.shape)
A(2, 3)
B(3, 4)
C(2, 4)
D(3, 3)
Step-by-Step Solution
Solution:
  1. Step 1: Understand linkage output shape

    For n data points, linkage returns a matrix with n-1 rows and 4 columns.
  2. Step 2: Calculate shape for 3 points

    Here, n=3, so output shape is (2, 4).
  3. Final Answer:

    (2, 4) -> Option C
  4. Quick Check:

    Linkage shape = (n-1, 4) = (2, 4) [OK]
Quick Trick: Linkage output shape = (n-1, 4) for n points [OK]
Common Mistakes:
  • Expecting shape (n, 4) instead of (n-1, 4)
  • Confusing columns count
  • Miscounting number of data points

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes