#NEWLINE# def fcluster( Z , t , criterion = 'inconsistent' , depth = 2 , R = None , monocrit = None ) : #NEWLINE# #INDENT# "\n    Forms flat clusters from the hierarchical clustering defined by\n    the linkage matrix ``Z``.\n\n    Parameters\n    ----------\n    Z : ndarray\n        The hierarchical clustering encoded with the matrix returned\n        by the `linkage` function.\n    t : float\n        The threshold to apply when forming flat clusters.\n    criterion : str, optional\n        The criterion to use in forming flat clusters. This can\n        be any of the following values:\n\n          ``inconsistent`` : If a cluster node and all its\n              descendants have an inconsistent value less than or equal\n              to `t` then all its leaf descendants belong to the\n              same flat cluster. When no non-singleton cluster meets\n              this criterion, every node is assigned to its own\n              cluster. (Default)\n\n          ``distance`` : Forms flat clusters so that the original\n              observations in each flat cluster have no greater a\n              cophenetic distance than `t`.\n\n          ``maxclust`` : Finds a minimum threshold ``r`` so that\n              the cophenetic distance between any two original\n              observations in the same flat cluster is no more than\n              ``r`` and no more than `t` flat clusters are formed.\n\n          ``monocrit`` : Forms a flat cluster from a cluster node c\n              with index i when ``monocrit[j] <= t``.\n\n              For example, to threshold on the maximum mean distance\n              as computed in the inconsistency matrix R with a\n              threshold of 0.8 do::\n\n                  MR = maxRstat(Z, R, 3)\n                  cluster(Z, t=0.8, criterion='monocrit', monocrit=MR)\n\n          ``maxclust_monocrit`` : Forms a flat cluster from a\n              non-singleton cluster node ``c`` when ``monocrit[i] <=\n              r`` for all cluster indices ``i`` below and including\n              ``c``. ``r`` is minimized such that no more than ``t``\n              flat clusters are formed. monocrit must be\n              monotonic. For example, to minimize the threshold t on\n              maximum inconsistency values so that no more than 3 flat\n              clusters are formed, do::\n\n                  MI = maxinconsts(Z, R)\n                  cluster(Z, t=3, criterion='maxclust_monocrit', monocrit=MI)\n\n    depth : int, optional\n        The maximum depth to perform the inconsistency calculation.\n        It has no meaning for the other criteria. Default is 2.\n    R : ndarray, optional\n        The inconsistency matrix to use for the 'inconsistent'\n        criterion. This matrix is computed if not provided.\n    monocrit : ndarray, optional\n        An array of length n-1. `monocrit[i]` is the\n        statistics upon which non-singleton i is thresholded. The\n        monocrit vector must be monotonic, i.e. given a node c with\n        index i, for all node indices j corresponding to nodes\n        below c, ``monocrit[i] >= monocrit[j]``.\n\n    Returns\n    -------\n    fcluster : ndarray\n        An array of length n. T[i] is the flat cluster number to\n        which original observation i belongs.\n\n    " #NEWLINE# Z = np . asarray ( Z , order = 'c' ) #NEWLINE# is_valid_linkage ( Z , throw = True , name = 'Z' ) #NEWLINE# n = ( Z . shape [ 0 ] + 1 ) #NEWLINE# T = np . zeros ( ( n , ) , dtype = 'i' ) #NEWLINE# [ Z ] = _copy_arrays_if_base_present ( [ Z ] ) #NEWLINE# if ( criterion == 'inconsistent' ) : #NEWLINE# #INDENT# if ( R is None ) : #NEWLINE# #INDENT# R = inconsistent ( Z , depth ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# R = np . asarray ( R , order = 'c' ) #NEWLINE# is_valid_im ( R , throw = True , name = 'R' ) #NEWLINE# [ R ] = _copy_arrays_if_base_present ( [ R ] ) #NEWLINE# #UNINDENT# _hierarchy . cluster_in ( Z , R , T , float ( t ) , int ( n ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( criterion == 'distance' ) : #NEWLINE# #INDENT# _hierarchy . cluster_dist ( Z , T , float ( t ) , int ( n ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( criterion == 'maxclust' ) : #NEWLINE# #INDENT# _hierarchy . cluster_maxclust_dist ( Z , T , int ( n ) , int ( t ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( R == 'monocrit' ) : #NEWLINE# #INDENT# [ monocrit ] = _copy_arrays_if_base_present ( [ monocrit ] ) #NEWLINE# _hierarchy . cluster_monocrit ( Z , monocrit , T , float ( t ) , int ( n ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( criterion == 'maxclust_monocrit' ) : #NEWLINE# #INDENT# [ monocrit ] = _copy_arrays_if_base_present ( [ monocrit ] ) #NEWLINE# _hierarchy . cluster_maxclust_monocrit ( Z , monocrit , T , int ( n ) , int ( t ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid cluster formation criterion: %s' % str ( criterion ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# return T