#NEWLINE# def _first_K_level_cluster( self , k ) : #NEWLINE# #INDENT# '\n        Use the first K clusters to appear in the level set tree as foreground\n        clusters. In general, K-1 clusters will appear at a lower level than\n        the K\'th cluster; this function returns all members from all K clusters\n        (rather than only the members in the upper level set where the K\'th\n        cluster appears). There are not always K clusters available in a level\n        set tree - see LevelSetTree.findKCut for details on default behavior in\n        this case.\n\n        Parameters\n        ----------\n        k : int\n            Desired number of clusters.\n\n        Returns\n        -------\n        labels : 2-dimensional numpy array\n            Each row corresponds to an observation. The first column indicates\n            the index of the observation in the original data matrix, and the\n            second column is the integer cluster label (starting at 0). Note\n            that the set of observations in this "foreground" set is typically\n            smaller than the original dataset.\n\n        nodes : list\n            Indices of tree nodes corresponding to foreground clusters.\n        ' #NEWLINE# cut = self . _find_K_cut ( k ) #NEWLINE# nodes = [ e for ( e , v ) in self . nodes . iteritems ( ) if ( ( v . start_level <= cut ) and ( v . end_level > cut ) ) ] #NEWLINE# points = [ ] #NEWLINE# cluster = [ ] #NEWLINE# for c in nodes : #NEWLINE# #INDENT# cluster_pts = self . nodes [ c ] . members #NEWLINE# points . extend ( cluster_pts ) #NEWLINE# cluster += ( [ c ] * len ( cluster_pts ) ) #NEWLINE# #UNINDENT# labels = _np . array ( [ points , cluster ] , dtype = _np . int ) . T #NEWLINE# return labels