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 hierarchical clustering?
Hierarchical clustering is a method to group similar data points into clusters by building a tree of clusters. It does not require a fixed number of clusters beforehand.
Click to reveal answer
beginner
What does the 'linkage' function in scipy do?
The 'linkage' function in scipy calculates distances between clusters and merges them step-by-step to form a hierarchy, producing a linkage matrix that shows how clusters combine.
Click to reveal answer
intermediate
Name three common linkage methods used in hierarchical clustering.
Three common linkage methods are: 1) Single linkage - distance between closest points, 2) Complete linkage - distance between farthest points, 3) Average linkage - average distance between all points in clusters.
Click to reveal answer
beginner
What is the output of the scipy linkage function?
The output is a linkage matrix, a 2D array where each row shows which clusters were merged, the distance between them, and the number of original points in the new cluster.
Click to reveal answer
intermediate
Why is hierarchical clustering useful compared to k-means?
Hierarchical clustering does not need you to choose the number of clusters in advance and shows the full cluster tree, which helps understand data structure better.
Click to reveal answer
What does the 'linkage' function in scipy.cluster.hierarchy return?
AA linkage matrix showing cluster merges
BA list of cluster labels for each data point
CA distance matrix between all points
DA dendrogram plot
✗ Incorrect
The linkage function returns a linkage matrix that records how clusters are merged step-by-step.
Which linkage method uses the shortest distance between points in clusters?
AComplete linkage
BWard linkage
CSingle linkage
DAverage linkage
✗ Incorrect
Single linkage uses the shortest distance between points in two clusters.
In hierarchical clustering, what does the dendrogram represent?
AA plot showing cluster centers
BA tree showing cluster merges and distances
CA heatmap of data values
DA scatter plot of data points
✗ Incorrect
A dendrogram is a tree diagram that shows how clusters merge at different distances.
Which of these is NOT a valid linkage method in scipy?
ASingle
BComplete
CMedian
DK-means
✗ Incorrect
K-means is a different clustering algorithm, not a linkage method.
What is the main advantage of hierarchical clustering over k-means?
AIt does not require specifying the number of clusters
BIt is faster for large datasets
CIt always produces spherical clusters
DIt uses random initialization
✗ Incorrect
Hierarchical clustering builds a cluster tree without needing the number of clusters upfront.
Explain how the linkage matrix represents cluster merges in hierarchical clustering.
Think about what information you need to know how clusters combine step-by-step.
You got /4 concepts.
Describe the difference between single, complete, and average linkage methods.
Focus on how distance between clusters is measured.
You got /3 concepts.
Practice
(1/5)
1. What does the linkage function in scipy.cluster.hierarchy do in hierarchical clustering?
easy
A. It calculates distances between clusters step-by-step to form a hierarchy.
B. It assigns data points to fixed clusters before clustering.
C. It visualizes the final clusters using a scatter plot.
D. It normalizes the data before clustering.
Solution
Step 1: Understand hierarchical clustering process
Hierarchical clustering builds clusters step-by-step by merging closest groups.
Step 2: Role of linkage function
The linkage function calculates distances between clusters at each step to decide which to merge next.
Final Answer:
It calculates distances between clusters step-by-step to form a hierarchy. -> Option A
2. Which of the following is the correct way to import the linkage function from scipy.cluster.hierarchy?
easy
A. from scipy.cluster import linkage
B. import linkage from scipy.cluster.hierarchy
C. import linkage from scipy.cluster
D. from scipy.cluster.hierarchy import linkage
Solution
Step 1: Identify correct module path
The linkage function is inside the hierarchy submodule of scipy.cluster.
Step 2: Use correct Python import syntax
Python import syntax for functions is from module import function. So, from scipy.cluster.hierarchy import linkage is correct.
Final Answer:
from scipy.cluster.hierarchy import linkage -> Option D
Quick Check:
Correct import = from scipy.cluster.hierarchy import linkage [OK]
Hint: Use 'from scipy.cluster.hierarchy import linkage' [OK]
Common Mistakes:
Using wrong module path
Wrong import syntax like 'import linkage from ...'
Importing from scipy.cluster directly
3. What is the output of this code snippet?
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.shape)
medium
A. (2, 3)
B. (3, 4)
C. (2, 4)
D. (3, 3)
Solution
Step 1: Understand linkage output shape
For n data points, linkage returns a matrix with n-1 rows and 4 columns.
Step 2: Calculate shape for 3 points
Here, n=3, so output shape is (2, 4).
Final Answer:
(2, 4) -> Option C
Quick Check:
Linkage shape = (n-1, 4) = (2, 4) [OK]
Hint: Linkage output shape = (n-1, 4) for n points [OK]
Common Mistakes:
Expecting shape (n, 4) instead of (n-1, 4)
Confusing columns count
Miscounting number of data points
4. Identify the error in this code snippet:
from scipy.cluster.hierarchy import linkage
import numpy as np
X = np.array([[1, 2], [3, 4], [5, 6]])
Z = linkage(X, method='fast')
print(Z)
medium
A. The method 'fast' is not a valid linkage method.
B. The input array X must be 1-dimensional.
C. The linkage function requires a distance matrix, not raw data.
D. The print statement is missing parentheses.
Solution
Step 1: Check valid linkage methods
Valid methods include 'single', 'complete', 'average', 'ward', etc. 'fast' is not valid.
Step 2: Confirm input data and syntax
Input can be raw data array; print statement syntax is correct in Python 3.
Final Answer:
The method 'fast' is not a valid linkage method. -> Option A
Quick Check:
Invalid method name causes error [OK]
Hint: Check method names carefully; 'fast' is invalid [OK]
Common Mistakes:
Assuming 'fast' is a valid method
Thinking input must be 1D array
Confusing linkage input requirements
5. You have a dataset with 5 points and want to perform hierarchical clustering using the 'ward' method. After computing linkage, how many merges will be recorded in the linkage matrix, and why?
hard
A. 3 merges, because only the closest points are merged.
B. 4 merges, because each merge reduces clusters by one until one cluster remains.
C. 6 merges, because the 'ward' method adds an extra merge step.
D. 5 merges, because there are 5 points to merge individually.
Solution
Step 1: Understand merges in hierarchical clustering
For n points, hierarchical clustering performs n-1 merges to combine all points into one cluster.
Step 2: Apply to 5 points with 'ward' method
With 5 points, the linkage matrix records 4 merges regardless of method.
Final Answer:
4 merges, because each merge reduces clusters by one until one cluster remains. -> Option B
Quick Check:
Merges = n-1 = 4 for 5 points [OK]
Hint: Number of merges = number of points minus one [OK]