October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk4 min

Get One Flat-Cluster Label per Observation with SciPy fcluster

SciPy’s fcluster cuts an existing linkage hierarchy into flat labels. Choose its criterion carefully: t can mean a statistic threshold or a maximum cluster count.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use scipy.cluster.hierarchy.fcluster to turn an existing linkage matrix into one flat-cluster label for each original observation. The key choice is criterion: it determines whether t is an inconsistency threshold, a cophenetic-distance threshold, or a maximum cluster count.

What `fcluster` returns

The SciPy v1.18.0 API reference describes fcluster as forming flat clusters from the hierarchical clustering defined by a linkage matrix. Its output is an array of length n, where T[i] is the cluster identifier assigned to original observation i. The identifiers are labels, not measurements of cluster quality or distance.

As an Amazon Associate I earn from qualifying purchases.

The function cuts a hierarchy that already exists; it does not build that hierarchy. In the usual workflow, linkage creates the hierarchy and fcluster turns it into flat assignments.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Build the linkage matrix first

Z is normally the (n - 1) × 4 matrix returned by scipy.cluster.hierarchy.linkage. Each row records the two cluster indices merged, the distance at which they are merged, and the number of original observations in the resulting cluster. linkage accepts observation vectors or a condensed pairwise-distance vector.

from scipy.cluster.hierarchy import fcluster, linkage
from scipy.spatial.distance import pdist

Z = linkage(pdist(X), method="ward")
labels = fcluster(Z, t=3, criterion="maxclust")

Here, X contains the observations. labels has one cluster identifier per row of X. Since the criterion is maxclust, t=3 requests no more than three clusters; it is not a distance cutoff of three.

The hierarchy itself depends on how it was made. SciPy documents linkage methods including single, complete, average, weighted, centroid, median, and Ward, each of which defines cluster distance differently. The choice of metric and linkage method shapes the hierarchy before fcluster applies its cut; changing the cut criterion does not undo those upstream choices. See the SciPy linkage reference.

Choose a criterion, then interpret `t`

The most important distinction is whether you have a meaningful statistic threshold or a desired upper bound on the number of groups. The meaning of t depends on the selected criterion.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
criterion What t means How the cut works
inconsistent (default) An inconsistency threshold Keeps a node and its descendants together when their inconsistency values are no greater than t. If no non-singleton node qualifies, observations remain separate.
distance A cophenetic-distance threshold Groups observations only when their within-cluster cophenetic distance does not exceed t.
maxclust The maximum number of clusters requested Finds a distance threshold that produces no more than t clusters; it does not promise exactly t.
monocrit A threshold on a supplied monotonic criterion Forms clusters according to whether the supplied statistic meets the threshold rule.
maxclust_monocrit The maximum number of clusters requested Minimizes the monotonic-statistic threshold while producing no more than t clusters.

Use distance when the desired rule is an interpretable cophenetic-distance ceiling. Use maxclust when the primary requirement is a bound on group count. Choose inconsistent when the cut should depend on inconsistency statistics. The monocrit options are for cases where you have constructed the relevant statistic and can ensure it is monotonic over the hierarchy.

Set a threshold for your own hierarchy

With criterion="distance", raising t generally permits more merges: a cut can move from many singleton clusters through intermediate groupings to one cluster. The threshold’s useful scale depends on the distances and linkage method that produced your particular Z. Values shown in SciPy examples illustrate behavior on their example data; they are not universal recommendations.

Before choosing a cut, decide whether your requirement is a distance ceiling or a maximum group count, identify the statistic the criterion uses, and check that the linkage method and distance representation are appropriate for the data.

Parameters and input requirements

  • Z must be a valid linkage matrix produced by linkage or a compatible representation.
  • depth sets the maximum depth used to calculate inconsistency; it defaults to 2 and has no meaning for criteria other than inconsistent.
  • R supplies the inconsistency matrix for the inconsistent criterion. If omitted, SciPy computes it.
  • monocrit must be an array of length n - 1, with values monotonic over the hierarchy.

For the full signature, criterion definitions, and parameter details, consult the SciPy v1.18.0 fcluster API reference.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Common mistakes to avoid

  • Reading every t as a distance: with maxclust and maxclust_monocrit, it is a requested maximum cluster count.
  • Expecting exactly the requested count: maxclust guarantees no more than t clusters, not necessarily exactly that many.
  • Passing a non-monotonic statistic: the monocrit input must satisfy the monotonicity requirement.
  • Choosing a cut without regard to how Z was built: the distance representation and linkage method have already shaped which merges the hierarchy contains.
  • Copying an example threshold as a rule: an example value is tied to its example hierarchy, not a general setting for other data.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Version and array-backend note

The SciPy v1.18.0 reference marks Python Array API support as experimental. It lists NumPy on CPU; PyTorch on CPU; JAX on CPU without JIT; and Dask on CPU with graph computation. It lists no GPU support for the CuPy, PyTorch, or JAX combinations covered there. Backend behavior can depend on the installed SciPy version, so check the reference for that version before relying on a backend combination.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.