def _hc_cut(n_clusters, children, n_leaves):
 if (n_clusters > n_leaves):
  raise ValueError(('Cannot extract more clusters than samples: %s clusters where given for a tree with %s leaves.' % (n_clusters, n_leaves)))
 nodes = [(- (max(children[(-1)]) + 1))]
 for i in xrange((n_clusters - 1)):
  these_children = children[((- nodes[0]) - n_leaves)]
  heappush(nodes, (- these_children[0]))
  heappushpop(nodes, (- these_children[1]))
 label = np.zeros(n_leaves, dtype=np.intp)
 for (i, node) in enumerate(nodes):
  label[_hierarchical._hc_get_descendent((- node), children, n_leaves)] = i
 return label

def _hc_cut ( n_clusters , children , n_leaves ) : if ( n_clusters > n_leaves ) : raise ValueError ( ( 'Cannot extract more clusters than samples: %s clusters where given for a tree with %s leaves.' % ( n_clusters , n_leaves ) ) ) nodes = [ ( - ( max ( children [ ( - 1 ) ] ) + 1 ) ) ] for i in xrange ( ( n_clusters - 1 ) ) : these_children = children [ ( ( - nodes [ 0 ] ) - n_leaves ) ] heappush ( nodes , ( - these_children [ 0 ] ) ) heappushpop ( nodes , ( - these_children [ 1 ] ) ) label = np . zeros ( n_leaves , dtype = np . intp ) for ( i , node ) in enumerate ( nodes ) : label [ _hierarchical . _hc_get_descendent ( ( - node ) , children , n_leaves ) ] = i return label
function cutting the ward tree for a given number of clusters .