Skip to learning content

DATA SCIENCE PYTHON PLAYGROUND

Machine Learning · Learn / Refresh

← Clustering and Discovery lessonsQUESTIONS · MODELS · EVIDENCE
Hierarchical discovery · ML-U08 · 12–18 MIN

Turn a hierarchy into groups

Choose a cut using evidence and purpose.

Exercises within this concept

  1. FollowRead and run the PythonCurrent exercise
  2. ChangeAdapt the Python
  3. TransferExplain the Python result

Understand the idea

A cut assigns observations to groups at a chosen resolution. Several cuts can be informative. Combine the dendrogram with sizes, profiles and the exploratory question.

Choose a cut using evidence and purpose.Merge distanceA cut chooses a descriptive grouping resolution.
Schematic · Choose a cut using evidence and purpose.Scroll the diagram horizontally if needed.

Python skill: Cuts the same stored hierarchy into three groups.

Meet the syntax

cut_tree(linkage_matrix, n_clusters=3).ravel()
cut_tree(linkage_matrix, n_clusters=3)
Cuts the same stored hierarchy into three groups.
.ravel()
Turns the one-column assignment array into a one-dimensional label vector aligned to the fitted rows.

Follow the code

Use the numbered comments to connect each Python block to the workflow above.

answer=cut_tree(linkage_matrix,n_clusters=3).ravel()

This practice: Read and run the Python. Next: Change · Turn a hierarchy into groups.

Given data · CLUSTER36

36 observations. Deterministic teaching observations; values illustrate the concept rather than a real population claim. The dataframe df is supplied afresh for each Run.

Reference labels are omitted from this preview and must remain outside fitting.

CLUSTER36 · first 8 prepared rows
length_mmwidth_cm
1106.656.36006
1262.6613.292
317.1385.44237
1044.748.89315
994.127.01435
1307.7912.7223
1023.1113.9453
1163.636.99248

Column meanings and units

Column names describe the supplied features and target. Keep the stated units and row identities when making comparisons.

Synthetic data are deliberately small and reproducible. Their patterns illustrate an idea; they are not evidence about a real population.

Input schema
ColumnStored type
length_mmfloat64
width_cmfloat64
Supplied setup · available if you need to inspect it

This code runs before your editor on every Run. These are the objects your exercise uses.

from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
X=df.copy()
scaler=StandardScaler()
scaled=scaler.fit_transform(X)
from scipy.cluster.hierarchy import linkage,dendrogram,cut_tree
linkage_matrix=linkage(scaled,method='ward')

Your task · Follow

Cut the fitted Ward hierarchy into three groups and store one group assignment per row in answer.

Hint 1 — Think

A cut converts one hierarchy into a requested resolution.

Hint 2 — Tools

cut_tree and ravel.

Hint 3 — Approach

Cut the supplied linkage at the declared group count and flatten the assignments.

Explained solution
answer=cut_tree(linkage_matrix,n_clusters=3).ravel()

The one-dimensional labels align to the hierarchy’s original row order and can support later profiles.

Helpful prior knowledge: Hierarchical merging · Choosing k requires evidence and judgement These links are guidance, not locks.

Sources and API context

Examples run with this Playground’s scikit-learn 1.4.2 / Pyodide 0.26.4 runtime.

Your task · Follow

Cut the fitted Ward hierarchy into three groups and store one group assignment per row in answer.

answer

Ctrl/⌘+Enter: Run · Tab: indent · Esc then Tab: leave editor

Python loads when you run. Code and results stay in this activity only.

Run your code to inspect its output. Check uses that same run.