Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is flat clustering in the context of hierarchical clustering?
Flat clustering means cutting the hierarchical clustering tree at a certain level to form groups (clusters) without any nested structure.
Click to reveal answer
beginner
What does the fcluster function in scipy.cluster.hierarchy do?
The fcluster function cuts a hierarchical clustering tree to assign cluster labels to data points based on a threshold or number of clusters.
Click to reveal answer
intermediate
Which parameters are important when using fcluster?
Key parameters are: • Z: linkage matrix from hierarchical clustering • t: threshold to cut the tree • criterion: method to decide clusters (e.g., 'distance', 'maxclust')
Click to reveal answer
intermediate
How does the criterion='maxclust' option work in fcluster?
It forms a flat clustering with a maximum number of clusters specified by t. The tree is cut to get at most that many clusters.
Click to reveal answer
beginner
Why is flat clustering useful after hierarchical clustering?
Flat clustering simplifies the hierarchical tree into clear groups, making it easier to analyze and use clusters for tasks like labeling or further analysis.
Click to reveal answer
What does the fcluster function return?
AAn array of cluster labels for each data point
BA linkage matrix
CA dendrogram plot
DThe original data sorted
✗ Incorrect
fcluster returns an array where each element is the cluster label assigned to the corresponding data point.
Which criterion in fcluster cuts the tree by distance threshold?
A'maxclust'
B'distance'
C'inconsistent'
D'monocrit'
✗ Incorrect
The 'distance' criterion cuts the hierarchical tree at a specified distance threshold t.
If you want exactly 3 clusters from hierarchical clustering, which criterion should you use with fcluster?
A'maxclust'
B'distance'
C'inconsistent'
D'monocrit'
✗ Incorrect
The 'maxclust' criterion cuts the tree to form a maximum number of clusters specified by t, so use t=3.
What input does fcluster require to assign clusters?
ARaw data points
BCluster centroids
CLinkage matrix from hierarchical clustering
DDistance matrix only
✗ Incorrect
fcluster needs the linkage matrix Z which encodes the hierarchical clustering structure.
Which of these is NOT a valid criterion for fcluster?
A'inconsistent'
B'maxclust'
C'distance'
D'kmeans'
✗ Incorrect
'kmeans' is not a criterion for fcluster. It is a separate clustering method.
Explain how the fcluster function works and why it is used after hierarchical clustering.
Think about how to get flat groups from a tree structure.
You got /4 concepts.
Describe the difference between using criterion='distance' and criterion='maxclust' in fcluster.
One uses a distance limit, the other uses a cluster count.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of the fcluster function in scipy's hierarchical clustering?
easy
A. To cut a hierarchical cluster tree into flat clusters based on a threshold
B. To compute the distance matrix between data points
C. To perform dimensionality reduction before clustering
D. To normalize data before clustering
Solution
Step 1: Understand hierarchical clustering output
Hierarchical clustering produces a tree (dendrogram) showing nested clusters.
Step 2: Role of fcluster
fcluster cuts this tree at a chosen threshold to form flat, non-overlapping clusters.
Final Answer:
To cut a hierarchical cluster tree into flat clusters based on a threshold -> Option A
Quick Check:
Flat clustering = cutting tree with threshold [OK]
Hint: Remember: fcluster cuts dendrogram into flat groups [OK]
Common Mistakes:
Confusing fcluster with distance calculation
Thinking fcluster normalizes data
Assuming fcluster reduces dimensions
2. Which of the following is the correct syntax to assign flat clusters using fcluster with a distance threshold of 1.5 from a linkage matrix Z?
easy
A. clusters = fcluster(Z, threshold=1.5, criterion='distance')
B. clusters = fcluster(Z, 1.5, method='distance')
C. clusters = fcluster(Z, 1.5, criterion='distance')
D. clusters = fcluster(Z, 1.5, criterion='maxclust')
Solution
Step 1: Check fcluster parameters
The function signature is fcluster(Z, t, criterion='distance') where t is the threshold.
Step 2: Identify correct usage
clusters = fcluster(Z, 1.5, criterion='distance') uses t=1.5 and criterion='distance', which is correct syntax.
Final Answer:
clusters = fcluster(Z, 1.5, criterion='distance') -> Option C
Quick Check:
Threshold = 1.5, criterion = 'distance' [OK]
Hint: Use t for threshold and criterion='distance' in fcluster [OK]
Common Mistakes:
Using 'method' instead of 'criterion'
Passing threshold as keyword 'threshold'
Using wrong criterion like 'maxclust' for distance cut
3. Given the linkage matrix Z = [[0, 1, 0.5, 2], [2, 3, 1.5, 2], [4, 5, 2.5, 4]], what is the output of fcluster(Z, 1.0, criterion='distance')?
medium
A. [1 1 1 1]
B. [1 2 3 4]
C. [1 2 2 3]
D. [1 1 2 3]
Solution
Step 1: Understand linkage matrix and threshold
The linkage matrix Z shows merges with distances: 0.5, 1.5, 2.5. Threshold is 1.0.
Step 2: Assign clusters by cutting at distance 1.0
Clusters merge if distance ≤ 1.0. The first merge (0.5) joins points 0 and 1 into cluster 1. The second merge (1.5 > 1.0) does not merge points 2 and 3, so point 2 gets cluster 2 and point 3 gets cluster 3.
Final Answer:
[1 1 2 3] -> Option D
Quick Check:
Distance ≤ 1.0 merges points 0 and 1 only [OK]
Hint: Cut dendrogram at threshold; merges below threshold cluster together [OK]
Common Mistakes:
Merging clusters above threshold
Assigning all points to one cluster
Misreading linkage matrix format
4. You run the code clusters = fcluster(Z, 2, criterion='maxclust') but get an error. What is the likely cause?
medium
A. The linkage matrix Z is not defined or invalid
B. The criterion 'maxclust' requires an integer number of clusters, but 2 is passed as a float
C. The threshold parameter must be a float when using 'maxclust'
D. The criterion 'maxclust' expects the threshold to be the maximum cluster distance
Solution
Step 1: Check parameter types for 'maxclust'
When using criterion='maxclust', the threshold t must be an integer specifying the number of clusters.
Step 2: Identify common error
If Z is not defined or invalid, fcluster raises an error unrelated to parameter types.
Step 3: Analyze options
The criterion 'maxclust' requires an integer number of clusters, but 2 is passed as a float is incorrect because 2 as an integer or float is accepted; The threshold parameter must be a float when using 'maxclust' is wrong because threshold can be int; The criterion 'maxclust' expects the threshold to be the maximum cluster distance is false because 'maxclust' uses number of clusters, not distance.
Final Answer:
The linkage matrix Z is not defined or invalid -> Option A
Quick Check:
Undefined Z causes error, not threshold type [OK]
Hint: Ensure linkage matrix Z is valid before calling fcluster [OK]
Common Mistakes:
Passing float instead of int for maxclust threshold
Misunderstanding criterion parameter
Ignoring linkage matrix validity
5. You have a dataset with 10 points clustered hierarchically. You want exactly 3 clusters. Which fcluster call correctly achieves this?
hard
A. fcluster(Z, 0.5, criterion='inconsistent')
B. fcluster(Z, 3, criterion='maxclust')
C. fcluster(Z, 0.5, criterion='maxclust')
D. fcluster(Z, 3, criterion='distance')
Solution
Step 1: Understand criteria for exact cluster count
To get exactly 3 clusters, use criterion='maxclust' with t=3 specifying number of clusters.
Step 2: Analyze options
fcluster(Z, 3, criterion='maxclust') correctly uses maxclust with 3 clusters. fcluster(Z, 3, criterion='distance') uses distance criterion which does not guarantee exact cluster count. Options A and C use incorrect thresholds or criteria.
Final Answer:
fcluster(Z, 3, criterion='maxclust') -> Option B
Quick Check:
maxclust + t=number of clusters = exact clusters [OK]
Hint: Use criterion='maxclust' with t = desired cluster count [OK]
Common Mistakes:
Using distance criterion to get exact cluster count