class_docstr: Class for training and visualizing a self-organizing map.
  
  Attributes:
  
      codebook     The codebook of the self-organizing map.
  
      bmus         The BMUs corresponding to the data points.
  
  :param n_columns: The number of columns in the map.
  :type n_columns: int.
  :param n_rows: The number of rows in the map.
  :type n_rows: int.
  :param initialcodebook: Optional parameter to start the training with a
                          given codebook.
  :type initialcodebook: 2D numpy.array of float32.
  :param kerneltype: Optional parameter to specify which kernel to use:
  
                         * 0: dense CPU kernel (default)
                         * 1: dense GPU kernel (if compiled with it)
  :type kerneltype: int.
  :param maptype: Optional parameter to specify the map topology:
  
                         * "planar": Planar map (default)
                         * "toroid": Toroid map
  :type maptype: str.
  :param gridtype: Optional parameter to specify the grid form of the nodes:
  
                         * "rectangular": rectangular neurons (default)
                         * "hexagonal": hexagonal neurons
  :type gridtype: str.
  :param compactsupport: Optional parameter to cut off map updates beyond the
                         training radius with the Gaussian neighborhood.
                         Default: True.
  :type compactsupport: bool.
  :param neighborhood: Optional parameter to specify the neighborhood:
  
                         * "gaussian": Gaussian neighborhood (default)
                         * "bubble": bubble neighborhood function
  :type neighborhood: str.
  :param vect_distance: Optional parameter to specify the vector distance function:
  
                         * "euclidean": Euclidean (default)
                         * "norm-inf": infinite norm (max absolute distance among components)
                         * "norm-p": p-th root of sum of absolute differences ^ p (only supported by kerneltype 0)
  :type vect_distance: str.
  :param std_coeff: Optional parameter to set the coefficient in the Gaussian
                    neighborhood function exp(-||x-y||^2/(2*(coeff*radius)^2))
                    Default: 0.5
  :type std_coeff: float.
  :param initialization: Optional parameter to specify the initalization:
  
                         * "random": random weights in the codebook
                         * "pca": codebook is initialized from the first
                           subspace spanned by the first two eigenvectors of
                           the correlation matrix
  :type initialization: str.
  :param verbose: Optional parameter to specify verbosity (0, 1, or 2).
  :type verbose: int.
class_docstr_tok: ["Class","for","training","and","visualizing","a","self-organizing","map",".","Attributes",":","codebook","The","codebook","of","the","self-organizing","map",".","bmus","The","BMUs","corresponding","to","the","data","points",".",":","param","n_columns",":","The","number","of","columns","in","the","map",".",":","type","n_columns",":","int",".",":","param","n_rows",":","The","number","of","rows","in","the","map",".",":","type","n_rows",":","int",".",":","param","initialcodebook",":","Optional","parameter","to","start","the","training","with","a","given","codebook",".",":","type","initialcodebook",":","2D","numpy.array","of","float32",".",":","param","kerneltype",":","Optional","parameter","to","specify","which","kernel","to","use",":","*","0",":","dense","CPU","kernel","(","default",")","*","1",":","dense","GPU","kernel","(","if","compiled","with","it",")",":","type","kerneltype",":","int",".",":","param","maptype",":","Optional","parameter","to","specify","the","map","topology",":","*","``","planar","''",":","Planar","map","(","default",")","*","``","toroid","''",":","Toroid","map",":","type","maptype",":","str",".",":","param","gridtype",":","Optional","parameter","to","specify","the","grid","form","of","the","nodes",":","*","``","rectangular","''",":","rectangular","neurons","(","default",")","*","``","hexagonal","''",":","hexagonal","neurons",":","type","gridtype",":","str",".",":","param","compactsupport",":","Optional","parameter","to","cut","off","map","updates","beyond","the","training","radius","with","the","Gaussian","neighborhood",".","Default",":","True",".",":","type","compactsupport",":","bool",".",":","param","neighborhood",":","Optional","parameter","to","specify","the","neighborhood",":","*","``","gaussian","''",":","Gaussian","neighborhood","(","default",")","*","``","bubble","''",":","bubble","neighborhood","function",":","type","neighborhood",":","str",".",":","param","vect_distance",":","Optional","parameter","to","specify","the","vector","distance","function",":","*","``","euclidean","''",":","Euclidean","(","default",")","*","``","norm-inf","''",":","infinite","norm","(","max","absolute","distance","among","components",")","*","``","norm-p","''",":","p-th","root","of","sum","of","absolute","differences","^","p","(","only","supported","by","kerneltype","0",")",":","type","vect_distance",":","str",".",":","param","std_coeff",":","Optional","parameter","to","set","the","coefficient","in","the","Gaussian","neighborhood","function","exp","(","-||x-y||^2\/","(","2","*","(","coeff","*","radius",")","^2",")",")","Default",":","0.5",":","type","std_coeff",":","float",".",":","param","initialization",":","Optional","parameter","to","specify","the","initalization",":","*","``","random","''",":","random","weights","in","the","codebook","*","``","pca","''",":","codebook","is","initialized","from","the","first","subspace","spanned","by","the","first","two","eigenvectors","of","the","correlation","matrix",":","type","initialization",":","str",".",":","param","verbose",":","Optional","parameter","to","specify","verbosity","(","0",",","1",",","or","2",")",".",":","type","verbose",":","int","."]
docstr: Cluster the codebook. The clusters of the data instances can be
  assigned based on the BMUs. The method populates the class variable
  Somoclu.clusters. If viewing methods are called after clustering, but
  without colors for best matching units, colors will be automatically
  assigned based on cluster membership.
  
  :param algorithm: Optional parameter to specify a scikit-learn
                    clustering algorithm. The default is K-means with
                    eight clusters.
  :type filename: sklearn.base.ClusterMixin.
docstr_tok: ["Cluster","the","codebook",".","The","clusters","of","the","data","instances","can","be","assigned","based","on","the","BMUs",".","The","method","populates","the","class","variable","Somoclu.clusters",".","If","viewing","methods","are","called","after","clustering",",","but","without","colors","for","best","matching","units",",","colors","will","be","automatically","assigned","based","on","cluster","membership",".",":","param","algorithm",":","Optional","parameter","to","specify","a","scikit-learn","clustering","algorithm",".","The","default","is","K-means","with","eight","clusters",".",":","type","filename",":","sklearn.base.ClusterMixin","."]
code: def cluster(self, algorithm=None):
      """Cluster the codebook. The clusters of the data instances can be
      assigned based on the BMUs. The method populates the class variable
      Somoclu.clusters. If viewing methods are called after clustering, but
      without colors for best matching units, colors will be automatically
      assigned based on cluster membership.
  
      :param algorithm: Optional parameter to specify a scikit-learn
                        clustering algorithm. The default is K-means with
                        eight clusters.
      :type filename: sklearn.base.ClusterMixin.
      """
  
      import sklearn.base
  
      if algorithm is None:
          import sklearn.cluster
  
          algorithm = sklearn.cluster.KMeans()
      elif not isinstance(algorithm, sklearn.base.ClusterMixin):
          raise Exception(
              "Cannot use algorithm of type " + type(algorithm)
          )
      original_shape = self.codebook.shape
      self.codebook.shape = (self._n_columns * self._n_rows, self.n_dim)
      linear_clusters = algorithm.fit_predict(self.codebook)
      self.codebook.shape = original_shape
      self.clusters = np.zeros(
          (self._n_rows, self._n_columns), dtype=int
      )
      for i, c in enumerate(linear_clusters):
          self.clusters[i // self._n_columns, i % self._n_columns] = c
  
imports: from __future__ import division, print_function
  import sys
  import matplotlib.cm
  import matplotlib.pyplot
  import matplotlib.collections
  import numpy
  from scipy.spatial.distance import cdist
label_desc: Use image node_id 15 for calling the Somoclu obj's underlying member method code with example usage: obj.cluster(algorithm) without return types