0
0
SciPydata~20 mins

Hierarchical clustering (linkage) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hierarchical Clustering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of linkage matrix with single linkage
What is the output of the linkage matrix when using single linkage on the given data points?
SciPy
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)
A
[[0. 1. 2.82842712 2.]
 [2. 3. 2.82842712 2.]
 [4. 5. 5.65685425 3.]]
B
[[0. 1. 2.82842712 2.]
 [2. 3. 4.24264069 2.]
 [4. 5. 5.65685425 3.]]
C
[[0. 1. 2.82842712 2.]
 [2. 3. 2.82842712 3.]]
D
[[0. 1. 1.41421356 2.]
 [2. 3. 2.82842712 2.]
 [4. 5. 4.24264069 3.]]
Attempts:
2 left
💡 Hint
Remember single linkage merges clusters based on the smallest distance between points.
data_output
intermediate
1:30remaining
Number of clusters from dendrogram cut
Given the linkage matrix below, how many clusters remain if we cut the dendrogram at distance 3.0?
SciPy
import numpy as np
Z = np.array([[0, 1, 1.5, 2], [2, 3, 2.5, 2], [4, 5, 4.0, 4]])
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Clusters merge when linkage distance is less than the cut distance.
🔧 Debug
advanced
1:30remaining
Identify the error in linkage method usage
What error will this code raise when running linkage with an invalid method name?
SciPy
from scipy.cluster.hierarchy import linkage
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
Z = linkage(X, method='invalid_method')
AValueError: Unknown linkage method invalid_method
BTypeError: linkage() missing 1 required positional argument
CAttributeError: 'numpy.ndarray' object has no attribute 'invalid_method'
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint
Check the allowed method names for linkage.
🚀 Application
advanced
2:00remaining
Choosing linkage method for compact clusters
Which linkage method is best to produce compact, spherical clusters in hierarchical clustering?
AWard linkage
BComplete linkage
CAverage linkage
DSingle linkage
Attempts:
2 left
💡 Hint
Ward linkage minimizes variance within clusters.
🧠 Conceptual
expert
2:30remaining
Effect of linkage method on dendrogram shape
How does the choice of linkage method affect the shape of the dendrogram in hierarchical clustering?
AIt only affects the color of the dendrogram branches, not the structure.
BIt changes the number of data points in the dataset.
CIt does not affect the dendrogram; all linkage methods produce identical dendrograms.
DIt changes the order of merges and the height of branches, reflecting different cluster distances.
Attempts:
2 left
💡 Hint
Think about how distances between clusters are computed.