bundles / scipy latest / scipy / cluster / hierarchy / fcluster
function
scipy.cluster.hierarchy:fcluster
Signature
def fcluster ( Z , t , criterion = inconsistent , depth = 2 , R = None , monocrit = None ) Summary
Form flat clusters from the hierarchical clustering defined by the given linkage matrix.
Parameters
Z: ndarrayThe hierarchical clustering encoded with the matrix returned by the
linkagefunction.t: scalarFor criteria 'inconsistent', 'distance' or 'monocrit',
this is the threshold to apply when forming flat clusters.
For 'maxclust' or 'maxclust_monocrit' criteria,
this would be max number of clusters requested.
criterion: str, optionalThe criterion to use in forming flat clusters. This can be any of the following values:
inconsistent:If a cluster node and all its descendants have an inconsistent value less than or equal to
t, then all its leaf descendants belong to the same flat cluster. When no non-singleton cluster meets this criterion, every node is assigned to its own cluster. (Default)distance:Forms flat clusters so that the original observations in each flat cluster have no greater a cophenetic distance than
t.maxclust:Finds a minimum threshold
rso that the cophenetic distance between any two original observations in the same flat cluster is no more thanrand no more thantflat clusters are formed.monocrit:Forms a flat cluster from a cluster node c with index i when
monocrit[j] <= t.For example, to threshold on the maximum mean distance as computed in the inconsistency matrix R with a threshold of 0.8 do
MR = maxRstat(Z, R, 3) fcluster(Z, t=0.8, criterion='monocrit', monocrit=MR)
maxclust_monocrit:Forms a flat cluster from a non-singleton cluster node
cwhenmonocrit[i] <= rfor all cluster indicesibelow and includingc.ris minimized such that no more thantflat clusters are formed. monocrit must be monotonic. For example, to minimize the threshold t on maximum inconsistency values so that no more than 3 flat clusters are formed, doMI = maxinconsts(Z, R) fcluster(Z, t=3, criterion='maxclust_monocrit', monocrit=MI)
depth: int, optionalThe maximum depth to perform the inconsistency calculation. It has no meaning for the other criteria. Default is 2.
R: ndarray, optionalThe inconsistency matrix to use for the
'inconsistent'criterion. This matrix is computed if not provided.monocrit: ndarray, optionalAn array of length n-1.
monocrit[i]is the statistics upon which non-singleton i is thresholded. The monocrit vector must be monotonic, i.e., given a node c with index i, for all node indices j corresponding to nodes below c,monocrit[i] >= monocrit[j].
Returns
fcluster: ndarrayAn array of length
n.T[i]is the flat cluster number to which original observationibelongs.
Notes
Array API Standard Support
fcluster has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ✅ ⛔ JAX ⚠️ no JIT ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy.cluster.hierarchy import ward, fcluster from scipy.spatial.distance import pdist✓
X = [[0, 0], [0, 1], [1, 0], [0, 4], [0, 3], [1, 4], [4, 0], [3, 0], [4, 1], [4, 4], [3, 4], [4, 3]]✓
Z = ward(pdist(X))
✓Z
✗fcluster(Z, t=0.9, criterion='distance')
✓fcluster(Z, t=1.1, criterion='distance')
✓fcluster(Z, t=3, criterion='distance')
✓fcluster(Z, t=9, criterion='distance')
✓See also
- linkage
for information about hierarchical clustering methods work.
Aliases
-
scipy.cluster.hierarchy.fcluster