0
0
SciPydata~20 mins

Flat clustering (fcluster) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flat Clustering Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of fcluster with distance threshold
What is the output array of cluster labels when using fcluster with t=1.5 on the given linkage matrix?
SciPy
from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np

X = np.array([[1, 2], [2, 2], [5, 5], [6, 5]])
Z = linkage(X, method='single')
clusters = fcluster(Z, t=1.5, criterion='distance')
print(clusters)
A[2 2 1 1]
B[1 2 3 4]
C[1 1 1 1]
D[1 1 2 2]
Attempts:
2 left
💡 Hint
Clusters are formed by cutting the dendrogram at the given distance threshold.
data_output
intermediate
2:00remaining
Number of clusters formed by fcluster
Using the linkage matrix Z from the code below, how many clusters are formed when using fcluster with t=3 and criterion='distance'?
SciPy
from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np

X = np.array([[1, 1], [2, 1], [4, 4], [5, 5], [10, 10]])
Z = linkage(X, method='complete')
clusters = fcluster(Z, t=3, criterion='distance')
num_clusters = len(set(clusters))
print(num_clusters)
A3
B2
C4
D1
Attempts:
2 left
💡 Hint
Count unique cluster labels after applying fcluster.
🔧 Debug
advanced
2:00remaining
Identify the error in fcluster usage
What error will this code raise when running fcluster with criterion='maxclust' but without specifying t properly?
SciPy
from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np

X = np.array([[0, 0], [1, 1], [5, 5]])
Z = linkage(X, method='ward')
clusters = fcluster(Z, criterion='maxclust')
print(clusters)
ANo error, prints cluster labels
BValueError: Invalid criterion value
CTypeError: fcluster() missing 1 required positional argument: 't'
DNameError: name 'clusters' is not defined
Attempts:
2 left
💡 Hint
Check the required arguments for fcluster function.
🧠 Conceptual
advanced
2:00remaining
Understanding fcluster criterion 'inconsistent'
Which statement best describes how fcluster forms clusters when using criterion='inconsistent'?
AClusters are formed by cutting the dendrogram at a fixed distance threshold.
BClusters are formed by comparing inconsistency coefficients of links to a threshold.
CClusters are formed by specifying the maximum number of clusters desired.
DClusters are formed by grouping points with the same feature values.
Attempts:
2 left
💡 Hint
Inconsistency measures how different a link is compared to links below it.
🚀 Application
expert
3:00remaining
Predict cluster labels for a custom dataset
Given the dataset and linkage below, which option shows the correct cluster labels when using fcluster with t=2 and criterion='distance'?
SciPy
from scipy.cluster.hierarchy import linkage, fcluster
import numpy as np

X = np.array([[0, 0], [0, 1], [1, 0], [5, 5], [6, 5], [5, 6]])
Z = linkage(X, method='average')
clusters = fcluster(Z, t=2, criterion='distance')
print(clusters)
A[1 1 1 2 2 2]
B[1 1 2 3 3 3]
C[1 2 3 4 5 6]
D[1 1 1 1 1 1]
Attempts:
2 left
💡 Hint
Points close together form clusters within the distance threshold.