code: def hierarch_cluster(M):
      
      import scipy as sp
      import scipy.cluster
      link = sp.cluster.hierarchy.linkage(M)
      indices = sp.cluster.hierarchy.leaves_list(link)
      Mclus = np.array(M[:, indices])
      Mclus = Mclus[indices, :]
      if False:
          pl.matshow(Mclus)
          pl.colorbar()
      return Mclus, indices
docs: Cluster matrix using hierarchical clustering.
  
      Parameters
      ----------
      M : np.ndarray
          Matrix, for example, distance matrix.
  
      Returns
      -------
      Mclus : np.ndarray
          Clustered matrix.
      indices : np.ndarray
          Indices used to cluster the matrix.