Flat clustering (fcluster) in SciPy - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using flat clustering with scipy's fcluster, we want to know how the time to assign clusters grows as we add more data points.
We ask: How does the clustering step scale with the number of points?
Analyze the time complexity of this flat clustering code snippet.
from scipy.cluster.hierarchy import linkage, fcluster
# data: array of n points
Z = linkage(data, method='ward')
clusters = fcluster(Z, t=3, criterion='maxclust')
This code creates a hierarchical clustering and then cuts it to form flat clusters.
Look at the main repeated steps:
- Primary operation: The linkage function computes distances and merges clusters repeatedly.
- How many times: It performs about n-1 merges for n points.
- The fcluster step just traverses the linkage once to assign cluster labels.
As the number of points grows, the linkage step does more work combining pairs.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | ~100 |
| 100 | ~10,000 |
| 1000 | ~1,000,000 |
Pattern observation: The work grows roughly with the square of the number of points.
Time Complexity: O(n^3)
This means if you double the number of points, the time to cluster roughly increases by a factor of eight.
[X] Wrong: "fcluster runs in linear time because it just assigns clusters."
[OK] Correct: While fcluster itself is fast, the main cost is in linkage, which grows much faster as data grows.
Understanding how clustering scales helps you explain choices in data analysis and handle larger datasets confidently.
What if we used a different linkage method that approximates distances? How would the time complexity change?
Practice
fcluster function in scipy's hierarchical clustering?Solution
Step 1: Understand hierarchical clustering output
Hierarchical clustering produces a tree (dendrogram) showing nested clusters.Step 2: Role of
fclusterfclustercuts 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 AQuick Check:
Flat clustering = cutting tree with threshold [OK]
- Confusing fcluster with distance calculation
- Thinking fcluster normalizes data
- Assuming fcluster reduces dimensions
fcluster with a distance threshold of 1.5 from a linkage matrix Z?Solution
Step 1: Check
The function signature isfclusterparametersfcluster(Z, t, criterion='distance')wheretis the threshold.Step 2: Identify correct usage
clusters = fcluster(Z, 1.5, criterion='distance') usest=1.5andcriterion='distance', which is correct syntax.Final Answer:
clusters = fcluster(Z, 1.5, criterion='distance') -> Option CQuick Check:
Threshold = 1.5, criterion = 'distance' [OK]
- Using 'method' instead of 'criterion'
- Passing threshold as keyword 'threshold'
- Using wrong criterion like 'maxclust' for distance cut
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')?Solution
Step 1: Understand linkage matrix and threshold
The linkage matrixZshows 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 DQuick Check:
Distance ≤ 1.0 merges points 0 and 1 only [OK]
- Merging clusters above threshold
- Assigning all points to one cluster
- Misreading linkage matrix format
clusters = fcluster(Z, 2, criterion='maxclust') but get an error. What is the likely cause?Solution
Step 1: Check parameter types for 'maxclust'
When usingcriterion='maxclust', the thresholdtmust be an integer specifying the number of clusters.Step 2: Identify common error
IfZis not defined or invalid,fclusterraises 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 AQuick Check:
Undefined Z causes error, not threshold type [OK]
- Passing float instead of int for maxclust threshold
- Misunderstanding criterion parameter
- Ignoring linkage matrix validity
fcluster call correctly achieves this?Solution
Step 1: Understand criteria for exact cluster count
To get exactly 3 clusters, usecriterion='maxclust'witht=3specifying number of clusters.Step 2: Analyze options
fcluster(Z, 3, criterion='maxclust') correctly usesmaxclustwith 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 BQuick Check:
maxclust + t=number of clusters = exact clusters [OK]
- Using distance criterion to get exact cluster count
- Passing float threshold for maxclust
- Confusing inconsistent criterion with maxclust
