def hcluster(features, distfcn=L2dist):
 distances = {}
 node = [ClusterLeafNode(array(f), id=i) for (i, f) in enumerate(features)]
 while (len(node) > 1):
  closest = float('Inf')
  for (ni, nj) in combinations(node, 2):
   if ((ni, nj) not in distances):
    distances[(ni, nj)] = distfcn(ni.vec, nj.vec)
   d = distances[(ni, nj)]
   if (d < closest):
    closest = d
    lowestpair = (ni, nj)
  (ni, nj) = lowestpair
  new_vec = ((ni.vec + nj.vec) / 2.0)
  new_node = ClusterNode(new_vec, left=ni, right=nj, distance=closest)
  node.remove(ni)
  node.remove(nj)
  node.append(new_node)
 return node[0]

def hcluster ( features , distfcn = L2dist ) : distances = { } node = [ ClusterLeafNode ( array ( f ) , id = i ) for ( i , f ) in enumerate ( features ) ] while ( len ( node ) > 1 ) : closest = float ( 'Inf' ) for ( ni , nj ) in combinations ( node , 2 ) : if ( ( ni , nj ) not in distances ) : distances [ ( ni , nj ) ] = distfcn ( ni . vec , nj . vec ) d = distances [ ( ni , nj ) ] if ( d < closest ) : closest = d lowestpair = ( ni , nj ) ( ni , nj ) = lowestpair new_vec = ( ( ni . vec + nj . vec ) / 2.0 ) new_node = ClusterNode ( new_vec , left = ni , right = nj , distance = closest ) node . remove ( ni ) node . remove ( nj ) node . append ( new_node ) return node [ 0 ]
cluster the rows of features using hierarchical clustering .