class_docstr: Initialization of parameters
  Parameters
      ----------
      n_clusters : int, optional, default: 4
          The number of clusters to form as well as the number of medians to
          generate.
  
      metric : string, or callable, optional, default: 'euclidean'
          What distance metric to use. See :func:metrics.pairwise_distances
          metric can be 'precomputed', the user must then feed the fit method
          with a precomputed kernel matrix and not the design matrix X.
  
      method : string, optional, default: 'per-axis'
          Specify the method of computing the median for multidimensional data. 
          'per-axis' takes the median for each attribute and combines them to make the cluster centre. 
  
          More options can be implemented from the following:
  
          A. Juan and E. Vidal. Fast k-means-like clustering in metric spaces. Pattern Recognition Letters, 15(1):19–25, 1994.
          A. Juan and E. Vidal. Fast Median Search in Metric Spaces. In A. Amin, D. Dori, P. Pudil, and H. Freeman, editors, Advances in Pattern Recognition, volume 1451, pages 905–912. Springer-Verlag, 1998
          Whelan, C., Harrell, G., & Wang, J. (2015). Understanding the K-Medians Problem.
  
      init : {'random'}, optional, default: 'random'
          Specify median initialization method. 'random' selects n_clusters
          elements from the dataset. 
  
          More options can be implemented from the following:
  
          Alfons Juan and Enrique Vidal. 2000. Comparison of Four Initialization Techniques for the K -Medians Clustering Algorithm. In Proceedings of the Joint IAPR International Workshops on Advances in Pattern Recognition. Springer-Verlag, Berlin, Heidelberg, 842–852.
  
      max_iter : int, optional, default : 300
          Specify the maximum number of iterations when fitting. It can be zero in
          which case only the initialization is computed which may be suitable for
          large datasets when the initialization is sufficiently efficient
  
      tol : float, optional, default : 0.0001
          Specify the tolerance of change in cluster centers. If change in cluster centers is less than tolerance, algorithm stops.
  
      random_state : int, RandomState instance or None, optional
          Specify random state for the random number generator. Used to
          initialise medians when init='random'.
  
      Attributes
      ----------
      cluster_centers_ : array, shape = (n_clusters, n_features)
              or None if metric == 'precomputed'
          Cluster centers, i.e. medians (elements from the original dataset)
  
      medoid_indices_ : array, shape = (n_clusters,)
          The indices of the median rows in X
  
      labels_ : array, shape = (n_samples,)
          Labels of each point
  
      inertia_ : float
          Sum of distances of samples to their closest cluster center.
          
      score_ : float
          Negative of the inertia. The more negative the score, the higher the variation in cluster points, the worse the clustering. 
class_docstr_tok: ["Initialization","of","parameters","Parameters","--","--","--","--","--","n_clusters",":","int",",","optional",",","default",":","4","The","number","of","clusters","to","form","as","well","as","the","number","of","medians","to","generate",".","metric",":","string",",","or","callable",",","optional",",","default",":","'euclidean'","What","distance","metric","to","use",".","See",":","func",":","metrics.pairwise_distances","metric","can","be","'precomputed","'",",","the","user","must","then","feed","the","fit","method","with","a","precomputed","kernel","matrix","and","not","the","design","matrix","X.","method",":","string",",","optional",",","default",":","'per-axis'","Specify","the","method","of","computing","the","median","for","multidimensional","data",".","'per-axis","'","takes","the","median","for","each","attribute","and","combines","them","to","make","the","cluster","centre",".","More","options","can","be","implemented","from","the","following",":","A.","Juan","and","E.","Vidal",".","Fast","k-means-like","clustering","in","metric","spaces",".","Pattern","Recognition","Letters",",","15","(","1",")",":19\u201325",",","1994",".","A.","Juan","and","E.","Vidal",".","Fast","Median","Search","in","Metric","Spaces",".","In","A.","Amin",",","D.","Dori",",","P.","Pudil",",","and","H.","Freeman",",","editors",",","Advances","in","Pattern","Recognition",",","volume","1451",",","pages","905\u2013912",".","Springer-Verlag",",","1998","Whelan",",","C.",",","Harrell",",","G.",",","&","Wang",",","J",".","(","2015",")",".","Understanding","the","K-Medians","Problem",".","init",":","{","'random","'","}",",","optional",",","default",":","'random'","Specify","median","initialization","method",".","'random","'","selects","n_clusters","elements","from","the","dataset",".","More","options","can","be","implemented","from","the","following",":","Alfons","Juan","and","Enrique","Vidal",".","2000",".","Comparison","of","Four","Initialization","Techniques","for","the","K","-Medians","Clustering","Algorithm",".","In","Proceedings","of","the","Joint","IAPR","International","Workshops","on","Advances","in","Pattern","Recognition",".","Springer-Verlag",",","Berlin",",","Heidelberg",",","842\u2013852",".","max_iter",":","int",",","optional",",","default",":","300","Specify","the","maximum","number","of","iterations","when","fitting",".","It","can","be","zero","in","which","case","only","the","initialization","is","computed","which","may","be","suitable","for","large","datasets","when","the","initialization","is","sufficiently","efficient","tol",":","float",",","optional",",","default",":","0.0001","Specify","the","tolerance","of","change","in","cluster","centers",".","If","change","in","cluster","centers","is","less","than","tolerance",",","algorithm","stops",".","random_state",":","int",",","RandomState","instance","or","None",",","optional","Specify","random","state","for","the","random","number","generator",".","Used","to","initialise","medians","when","init='random","'",".","Attributes","--","--","--","--","--","cluster_centers_",":","array",",","shape","=","(","n_clusters",",","n_features",")","or","None","if","metric","==","'precomputed'","Cluster","centers",",","i.e",".","medians","(","elements","from","the","original","dataset",")","medoid_indices_",":","array",",","shape","=","(","n_clusters",",",")","The","indices","of","the","median","rows","in","X","labels_",":","array",",","shape","=","(","n_samples",",",")","Labels","of","each","point","inertia_",":","float","Sum","of","distances","of","samples","to","their","closest","cluster","center",".","score_",":","float","Negative","of","the","inertia",".","The","more","negative","the","score",",","the","higher","the","variation","in","cluster","points",",","the","worse","the","clustering","."]
docstr: Fits the model to the data
docstr_tok: ["Fits","the","model","to","the","data"]
code: def fit(self, X, Y=None):
      """Fits the model to the data"""
  
      self._check_arguments()
      random_state_object = self._get_random_state(self.random_state)
  
      if Y:
          raise Exception("Clustering fit takes only one parameter")
  
      X = check_array(X, accept_sparse=["csc", "csr"])
  
      n_samples, n_features = X.shape[0], X.shape[1]
  
      if self.n_clusters > n_samples:
          raise ValueError(
              "Number of clusters %s cannot be greater than number of samples %s"
              % (self.n_clusters, n_samples)
          )
  
      centers = X[
          self._initialize_centers(
              X, self.n_clusters, random_state_object
          )
      ]
      # print(centers)
      distances = pairwise_distances(centers, X, metric=self.metric)
      # print("Distances:", distances)
  
      medians = [None] * self.n_clusters
      labels = None
  
      if self.method == "per-axis":
          for i in range(self.max_iter):
              old_centers = np.copy(centers)
              labels = np.argmin(distances, axis=0)
  
              for item in range(self.n_clusters):
                  indices = np.argwhere(labels == item)
                  medians[item] = np.median(X[indices], axis=0)
  
              centers = np.squeeze(np.asarray(medians))
              distances = pairwise_distances(
                  centers, X, metric=self.metric
              )
  
              if np.all(np.abs(old_centers - centers) < self.tol):
                  break
              elif i == self.max_iter - 1:
                  warnings.warn(
                      "Maximum number of iteration reached before "
                      "convergence. Consider increasing max_iter to "
                      "improve the fit.",
                      ConvergenceWarning,
                  )
      self.cluster_centers_ = centers
      self.labels_ = np.argmin(distances, axis=0)
      self.inertia_ = self._compute_inertia(distances, self.labels_)
      self.score_ = -self.inertia_
  
imports: import random
  import numpy
  import warnings
  from sklearn.exceptions import ConvergenceWarning
  from sklearn.base import BaseEstimator, ClusterMixin
  from sklearn.utils import check_array
  from sklearn.utils.validation import check_is_fitted
  from sklearn.metrics.pairwise import pairwise_distances, pairwise_distances_argmin
label_desc: Use image node_id 7 for calling the KMedians obj's underlying member method code with example usage: obj.fit(X, Y) without return types