Bird
Raised Fist0
SciPydata~20 mins

Dendrogram visualization in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Dendrogram Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dendrogram leaf order
What is the order of leaf labels in the dendrogram produced by this code?
SciPy
import numpy as np
from scipy.cluster.hierarchy import linkage, dendrogram

np.random.seed(0)
data = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
Z = linkage(data, method='single')
D = dendrogram(Z, no_plot=True)
leaf_order = D['leaves']
print(leaf_order)
A[2, 0, 1, 5, 3, 4]
B[0, 1, 2, 3, 4, 5]
C[5, 3, 4, 2, 0, 1]
D[1, 0, 2, 4, 3, 5]
Attempts:
2 left
💡 Hint
Look at how the linkage method 'single' clusters points based on minimum distance.
data_output
intermediate
2:00remaining
Number of clusters from dendrogram cut
Given this linkage matrix, how many clusters remain if we cut the dendrogram at distance 3?
SciPy
import numpy as np
from scipy.cluster.hierarchy import linkage

data = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
Z = linkage(data, method='complete')
clusters = (Z[:, 2] > 3).sum() + 1
print(clusters)
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Count how many merges have distance greater than 3.
visualization
advanced
3:00remaining
Identify dendrogram linkage method from plot shape
Which linkage method produces this dendrogram shape when clustering the same data?
SciPy
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import linkage, dendrogram
import numpy as np

np.random.seed(1)
data = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])

methods = ['single', 'complete', 'average', 'ward']
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
for ax, method in zip(axs.flatten(), methods):
    Z = linkage(data, method=method)
    dendrogram(Z, ax=ax)
    ax.set_title(method)
plt.tight_layout()
plt.show()
AThe dendrogram with irregular cluster heights is 'average' linkage.
BThe dendrogram with the longest horizontal lines at the bottom is 'single' linkage.
CThe dendrogram with the shortest maximum height is 'complete' linkage.
DThe dendrogram with balanced cluster heights is 'ward' linkage.
Attempts:
2 left
💡 Hint
Ward linkage tries to minimize variance within clusters, producing balanced heights.
🔧 Debug
advanced
2:00remaining
Error in dendrogram plotting code
What error does this code raise when run?
SciPy
from scipy.cluster.hierarchy import dendrogram
import matplotlib.pyplot as plt
import numpy as np

Z = np.array([[0, 1, 0.5, 2], [2, 3, 0.7, 2], [4, 5, 1.2, 4]])
dendrogram(Z)
plt.show()
ATypeError: 'list' object is not a valid linkage matrix
BIndexError: list index out of range
CNo error, dendrogram plots successfully
DValueError: Linkage matrix must be a 2D numpy array
Attempts:
2 left
💡 Hint
Check the type and shape of the linkage matrix input.
🚀 Application
expert
3:00remaining
Extract cluster labels from dendrogram at specific height
Which code snippet correctly assigns cluster labels to data points by cutting the dendrogram at height 1.5?
SciPy
import numpy as np
from scipy.cluster.hierarchy import linkage, fcluster

data = np.array([[1, 2], [1, 4], [1, 0], [4, 2], [4, 4], [4, 0]])
Z = linkage(data, method='average')
A
labels = fcluster(Z, t=1.5, criterion='distance')
print(labels)
B
labels = fcluster(Z, t=1.5, criterion='maxclust')
print(labels)
C
labels = fcluster(Z, t=1.5, criterion='inconsistent')
print(labels)
D
labels = fcluster(Z, t=1.5, criterion='monocrit')
print(labels)
Attempts:
2 left
💡 Hint
Use 'distance' criterion to cut dendrogram at a height threshold.

Practice

(1/5)
1. What is the main purpose of a dendrogram in data science?
easy
A. To visualize hierarchical clustering as a tree
B. To perform linear regression analysis
C. To calculate the mean of a dataset
D. To create a scatter plot of two variables

Solution

  1. Step 1: Understand dendrogram function

    A dendrogram is used to show hierarchical clustering results visually as a tree structure.
  2. Step 2: Compare with other options

    The other options describe different data analysis or visualization methods unrelated to dendrograms.
  3. Final Answer:

    To visualize hierarchical clustering as a tree -> Option A
  4. Quick Check:

    Dendrogram = hierarchical clustering tree [OK]
Hint: Dendrograms always show clusters as tree diagrams [OK]
Common Mistakes:
  • Confusing dendrogram with scatter plot
  • Thinking dendrogram calculates statistics
  • Mixing dendrogram with regression plots
2. Which of the following is the correct way to import the dendrogram function from scipy?
easy
A. from scipy.visualization import dendrogram
B. import scipy.dendrogram
C. import dendrogram from scipy.cluster
D. from scipy.cluster.hierarchy import dendrogram

Solution

  1. Step 1: Recall correct import syntax

    The dendrogram function is located in scipy.cluster.hierarchy, so the correct import is from scipy.cluster.hierarchy import dendrogram.
  2. Step 2: Check other options for syntax errors

    The other options use incorrect module paths or invalid import syntax.
  3. Final Answer:

    from scipy.cluster.hierarchy import dendrogram -> Option D
  4. Quick Check:

    Correct import path = from scipy.cluster.hierarchy import dendrogram [OK]
Hint: Remember dendrogram is in scipy.cluster.hierarchy [OK]
Common Mistakes:
  • Using wrong module path
  • Incorrect import syntax
  • Assuming dendrogram is in scipy.visualization
3. Given the following code, what will be the output type of dn?
from scipy.cluster.hierarchy import dendrogram, linkage
import numpy as np

X = np.array([[1, 2], [3, 4], [5, 6]])
Z = linkage(X, 'single')
dn = dendrogram(Z)
medium
A. A NumPy array of cluster labels
B. A dictionary containing dendrogram data
C. A matplotlib figure object
D. A list of linkage distances

Solution

  1. Step 1: Understand dendrogram return value

    The dendrogram function returns a dictionary with keys like 'icoord', 'dcoord', 'leaves', and 'color_list' describing the dendrogram structure.
  2. Step 2: Check other options

    A NumPy array of cluster labels is incorrect because cluster labels are not returned by dendrogram. A matplotlib figure object is wrong because dendrogram does not return a figure object. A list of linkage distances is incorrect as linkage distances are part of the linkage matrix, not dendrogram output.
  3. Final Answer:

    A dictionary containing dendrogram data -> Option B
  4. Quick Check:

    dendrogram() returns dict = A dictionary containing dendrogram data [OK]
Hint: dendrogram() returns a dict with plotting info [OK]
Common Mistakes:
  • Expecting dendrogram to return a plot object
  • Confusing dendrogram output with linkage matrix
  • Thinking dendrogram returns cluster labels
4. Identify the error in this code snippet for plotting a dendrogram:
from scipy.cluster.hierarchy import dendrogram, linkage
import matplotlib.pyplot as plt

X = [[1, 2], [3, 4], [5, 6]]
Z = linkage(X, 'ward')
dendrogram(Z)
plt.show()
medium
A. Linkage method 'ward' is invalid
B. Missing import for numpy
C. No error; code runs and plots dendrogram correctly
D. X should be a NumPy array, not a list

Solution

  1. Step 1: Check data input type

    Linkage accepts array-like input, so a Python list of lists is valid for X.
  2. Step 2: Verify linkage method and plotting

    'ward' is a valid linkage method. The code imports matplotlib.pyplot as plt and calls plt.show(), so the dendrogram will plot correctly.
  3. Final Answer:

    No error; code runs and plots dendrogram correctly -> Option C
  4. Quick Check:

    List input and 'ward' method are valid [OK]
Hint: Linkage accepts lists; 'ward' is valid method [OK]
Common Mistakes:
  • Assuming input must be NumPy array
  • Thinking 'ward' is invalid linkage method
  • Forgetting plt.show() to display plot
5. You want to visualize clusters with different colors in a dendrogram using scipy.cluster.hierarchy.dendrogram. Which parameter should you set to control the color threshold for cluster coloring?
hard
A. color_threshold
B. linkage_method
C. leaf_rotation
D. distance_metric

Solution

  1. Step 1: Identify parameter for cluster color control

    The parameter color_threshold in dendrogram controls the threshold distance to color clusters differently.
  2. Step 2: Eliminate unrelated parameters

    linkage_method and distance_metric relate to clustering, not coloring. leaf_rotation controls label rotation, not colors.
  3. Final Answer:

    color_threshold -> Option A
  4. Quick Check:

    Cluster colors controlled by color_threshold [OK]
Hint: Use color_threshold to set cluster color boundaries [OK]
Common Mistakes:
  • Confusing color_threshold with linkage method
  • Using leaf_rotation to change colors
  • Assuming distance_metric affects dendrogram colors