Bird
0
0

What will be the output of this code snippet?

medium📝 Predict Output Q5 of 15
SciPy - Clustering and Distance
What will be the output of this code snippet?
from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np
X = np.array([[1, 2], [1, 4], [5, 2], [5, 4]])
Z = linkage(X, 'single')
clusters = fcluster(Z, t=3, criterion='distance')
print(clusters)
A[1 1 1 1]
B[1 2 3 4]
C[2 2 1 1]
D[1 1 2 2]
Step-by-Step Solution
Solution:
  1. Step 1: Perform linkage with single method

    Linkage merges closest points first. Points [1,2] and [1,4] cluster together, and [5,2] and [5,4] cluster together.
  2. Step 2: Apply fcluster with threshold 3

    Distance threshold 3 separates these two clusters, so points 0 and 1 get cluster 1, points 2 and 3 get cluster 2.
  3. Final Answer:

    [1 1 2 2] -> Option D
  4. Quick Check:

    Cluster labels = [1 1 2 2] [OK]
Quick Trick: Threshold t splits clusters by max distance [OK]
Common Mistakes:
  • Assuming all points in one cluster
  • Misunderstanding linkage method effect
  • Confusing cluster labels order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes