class_docstr: K-Means clustering.
  
  Read more in the :ref:`User Guide <k_means>`.
  
  Parameters
  ----------
  
  n_clusters : int, default=8
      The number of clusters to form as well as the number of
      centroids to generate.
  
      For an example of how to choose an optimal value for `n_clusters` refer to
      :ref:`sphx_glr_auto_examples_cluster_plot_kmeans_silhouette_analysis.py`.
  
  init : {'k-means++', 'random'}, callable or array-like of shape             (n_clusters, n_features), default='k-means++'
      Method for initialization:
  
      * 'k-means++' : selects initial cluster centroids using sampling             based on an empirical probability distribution of the points'             contribution to the overall inertia. This technique speeds up             convergence. The algorithm implemented is "greedy k-means++". It             differs from the vanilla k-means++ by making several trials at             each sampling step and choosing the best centroid among them.
  
      * 'random': choose `n_clusters` observations (rows) at random from         data for the initial centroids.
  
      * If an array is passed, it should be of shape (n_clusters, n_features)        and gives the initial centers.
  
      * If a callable is passed, it should take arguments X, n_clusters and a        random state and return an initialization.
  
      For an example of how to use the different `init` strategy, see the example
      entitled :ref:`sphx_glr_auto_examples_cluster_plot_kmeans_digits.py`.
  
  n_init : 'auto' or int, default='auto'
      Number of times the k-means algorithm is run with different centroid
      seeds. The final results is the best output of `n_init` consecutive runs
      in terms of inertia. Several runs are recommended for sparse
      high-dimensional problems (see :ref:`kmeans_sparse_high_dim`).
  
      When `n_init='auto'`, the number of runs depends on the value of init:
      10 if using `init='random'` or `init` is a callable;
      1 if using `init='k-means++'` or `init` is an array-like.
  
      .. versionadded:: 1.2
         Added 'auto' option for `n_init`.
  
      .. versionchanged:: 1.4
         Default value for `n_init` changed to `'auto'`.
  
  max_iter : int, default=300
      Maximum number of iterations of the k-means algorithm for a
      single run.
  
  tol : float, default=1e-4
      Relative tolerance with regards to Frobenius norm of the difference
      in the cluster centers of two consecutive iterations to declare
      convergence.
  
  verbose : int, default=0
      Verbosity mode.
  
  random_state : int, RandomState instance or None, default=None
      Determines random number generation for centroid initialization. Use
      an int to make the randomness deterministic.
      See :term:`Glossary <random_state>`.
  
  copy_x : bool, default=True
      When pre-computing distances it is more numerically accurate to center
      the data first. If copy_x is True (default), then the original data is
      not modified. If False, the original data is modified, and put back
      before the function returns, but small numerical differences may be
      introduced by subtracting and then adding the data mean. Note that if
      the original data is not C-contiguous, a copy will be made even if
      copy_x is False. If the original data is sparse, but not in CSR format,
      a copy will be made even if copy_x is False.
  
  algorithm : {"lloyd", "elkan"}, default="lloyd"
      K-means algorithm to use. The classical EM-style algorithm is `"lloyd"`.
      The `"elkan"` variation can be more efficient on some datasets with
      well-defined clusters, by using the triangle inequality. However it's
      more memory intensive due to the allocation of an extra array of shape
      `(n_samples, n_clusters)`.
  
      .. versionchanged:: 0.18
          Added Elkan algorithm
  
      .. versionchanged:: 1.1
          Renamed "full" to "lloyd", and deprecated "auto" and "full".
          Changed "auto" to use "lloyd" instead of "elkan".
  
  Attributes
  ----------
  cluster_centers_ : ndarray of shape (n_clusters, n_features)
      Coordinates of cluster centers. If the algorithm stops before fully
      converging (see ``tol`` and ``max_iter``), these will not be
      consistent with ``labels_``.
  
  labels_ : ndarray of shape (n_samples,)
      Labels of each point
  
  inertia_ : float
      Sum of squared distances of samples to their closest cluster center,
      weighted by the sample weights if provided.
  
  n_iter_ : int
      Number of iterations run.
  
  n_features_in_ : int
      Number of features seen during :term:`fit`.
  
      .. versionadded:: 0.24
  
  feature_names_in_ : ndarray of shape (`n_features_in_`,)
      Names of features seen during :term:`fit`. Defined only when `X`
      has feature names that are all strings.
  
      .. versionadded:: 1.0
  
  See Also
  --------
  MiniBatchKMeans : Alternative online implementation that does incremental
      updates of the centers positions using mini-batches.
      For large scale learning (say n_samples > 10k) MiniBatchKMeans is
      probably much faster than the default batch implementation.
  
  Notes
  -----
  The k-means problem is solved using either Lloyd's or Elkan's algorithm.
  
  The average complexity is given by O(k n T), where n is the number of
  samples and T is the number of iteration.
  
  The worst case complexity is given by O(n^(k+2/p)) with
  n = n_samples, p = n_features.
  Refer to :doi:`"How slow is the k-means method?" D. Arthur and S. Vassilvitskii -
  SoCG2006.<10.1145/1137856.1137880>` for more details.
  
  In practice, the k-means algorithm is very fast (one of the fastest
  clustering algorithms available), but it falls in local minima. That's why
  it can be useful to restart it several times.
  
  If the algorithm stops before fully converging (because of ``tol`` or
  ``max_iter``), ``labels_`` and ``cluster_centers_`` will not be consistent,
  i.e. the ``cluster_centers_`` will not be the means of the points in each
  cluster. Also, the estimator will reassign ``labels_`` after the last
  iteration to make ``labels_`` consistent with ``predict`` on the training
  set.
  
  Examples
  --------
  
  >>> from sklearn.cluster import KMeans
  >>> import numpy as np
  >>> X = np.array([[1, 2], [1, 4], [1, 0],
  ...               [10, 2], [10, 4], [10, 0]])
  >>> kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto").fit(X)
  >>> kmeans.labels_
  array([1, 1, 1, 0, 0, 0], dtype=int32)
  >>> kmeans.predict([[0, 0], [12, 3]])
  array([1, 0], dtype=int32)
  >>> kmeans.cluster_centers_
  array([[10.,  2.],
         [ 1.,  2.]])
  
  For a more detailed example of K-Means using the iris dataset see
  :ref:`sphx_glr_auto_examples_cluster_plot_cluster_iris.py`.
  
  For examples of common problems with K-Means and how to address them see
  :ref:`sphx_glr_auto_examples_cluster_plot_kmeans_assumptions.py`.
  
  For an example of how to use K-Means to perform color quantization see
  :ref:`sphx_glr_auto_examples_cluster_plot_color_quantization.py`.
  
  For a demonstration of how K-Means can be used to cluster text documents see
  :ref:`sphx_glr_auto_examples_text_plot_document_clustering.py`.
  
  For a comparison between K-Means and MiniBatchKMeans refer to example
  :ref:`sphx_glr_auto_examples_cluster_plot_mini_batch_kmeans.py`.
class_docstr_tok: ["K-Means","clustering",".","Read","more","in","the",":","ref",":","`","User","Guide","<","k_means",">","`",".","Parameters","--","--","--","--","--","n_clusters",":","int",",","default=8","The","number","of","clusters","to","form","as","well","as","the","number","of","centroids","to","generate",".","For","an","example","of","how","to","choose","an","optimal","value","for","`","n_clusters","`","refer","to",":","ref",":","`","sphx_glr_auto_examples_cluster_plot_kmeans_silhouette_analysis.py","`",".","init",":","{","'","k-means++","'",",","'random","'","}",",","callable","or","array-like","of","shape","(","n_clusters",",","n_features",")",",","default=","'","k-means++'","Method","for","initialization",":","*","'","k-means++","'",":","selects","initial","cluster","centroids","using","sampling","based","on","an","empirical","probability","distribution","of","the","points","'","contribution","to","the","overall","inertia",".","This","technique","speeds","up","convergence",".","The","algorithm","implemented","is","``","greedy","k-means++","''",".","It","differs","from","the","vanilla","k-means++","by","making","several","trials","at","each","sampling","step","and","choosing","the","best","centroid","among","them",".","*","'random","'",":","choose","`","n_clusters","`","observations","(","rows",")","at","random","from","data","for","the","initial","centroids",".","*","If","an","array","is","passed",",","it","should","be","of","shape","(","n_clusters",",","n_features",")","and","gives","the","initial","centers",".","*","If","a","callable","is","passed",",","it","should","take","arguments","X",",","n_clusters","and","a","random","state","and","return","an","initialization",".","For","an","example","of","how","to","use","the","different","`","init","`","strategy",",","see","the","example","entitled",":","ref",":","`","sphx_glr_auto_examples_cluster_plot_kmeans_digits.py","`",".","n_init",":","'auto","'","or","int",",","default='auto'","Number","of","times","the","k-means","algorithm","is","run","with","different","centroid","seeds",".","The","final","results","is","the","best","output","of","`","n_init","`","consecutive","runs","in","terms","of","inertia",".","Several","runs","are","recommended","for","sparse","high-dimensional","problems","(","see",":","ref",":","`","kmeans_sparse_high_dim","`",")",".","When","`","n_init='auto","'","`",",","the","number","of","runs","depends","on","the","value","of","init",":","10","if","using","`","init='random","'","`","or","`","init","`","is","a","callable",";","1","if","using","`","init=","'","k-means++","'","`","or","`","init","`","is","an","array-like",".","..","versionadded",":",":","1.2","Added","'auto","'","option","for","`","n_init","`",".","..","versionchanged",":",":","1.4","Default","value","for","`","n_init","`","changed","to","`","'auto","'","`",".","max_iter",":","int",",","default=300","Maximum","number","of","iterations","of","the","k-means","algorithm","for","a","single","run",".","tol",":","float",",","default=1e-4","Relative","tolerance","with","regards","to","Frobenius","norm","of","the","difference","in","the","cluster","centers","of","two","consecutive","iterations","to","declare","convergence",".","verbose",":","int",",","default=0","Verbosity","mode",".","random_state",":","int",",","RandomState","instance","or","None",",","default=None","Determines","random","number","generation","for","centroid","initialization",".","Use","an","int","to","make","the","randomness","deterministic",".","See",":","term",":","`","Glossary","<","random_state",">","`",".","copy_x",":","bool",",","default=True","When","pre-computing","distances","it","is","more","numerically","accurate","to","center","the","data","first",".","If","copy_x","is","True","(","default",")",",","then","the","original","data","is","not","modified",".","If","False",",","the","original","data","is","modified",",","and","put","back","before","the","function","returns",",","but","small","numerical","differences","may","be","introduced","by","subtracting","and","then","adding","the","data","mean",".","Note","that","if","the","original","data","is","not","C-contiguous",",","a","copy","will","be","made","even","if","copy_x","is","False",".","If","the","original","data","is","sparse",",","but","not","in","CSR","format",",","a","copy","will","be","made","even","if","copy_x","is","False",".","algorithm",":","{","``","lloyd","''",",","``","elkan","''","}",",","default=","''","lloyd","''","K-means","algorithm","to","use",".","The","classical","EM-style","algorithm","is","`","``","lloyd","''","`",".","The","`","``","elkan","''","`","variation","can","be","more","efficient","on","some","datasets","with","well-defined","clusters",",","by","using","the","triangle","inequality",".","However","it's","more","memory","intensive","due","to","the","allocation","of","an","extra","array","of","shape","`","(","n_samples",",","n_clusters",")","`",".","..","versionchanged",":",":","0.18","Added","Elkan","algorithm","..","versionchanged",":",":","1.1","Renamed","``","full","''","to","``","lloyd","''",",","and","deprecated","``","auto","''","and","``","full","''",".","Changed","``","auto","''","to","use","``","lloyd","''","instead","of","``","elkan","''",".","Attributes","--","--","--","--","--","cluster_centers_",":","ndarray","of","shape","(","n_clusters",",","n_features",")","Coordinates","of","cluster","centers",".","If","the","algorithm","stops","before","fully","converging","(","see","``","tol","``","and","``","max_iter","``",")",",","these","will","not","be","consistent","with","``","labels_","``",".","labels_",":","ndarray","of","shape","(","n_samples",",",")","Labels","of","each","point","inertia_",":","float","Sum","of","squared","distances","of","samples","to","their","closest","cluster","center",",","weighted","by","the","sample","weights","if","provided",".","n_iter_",":","int","Number","of","iterations","run",".","n_features_in_",":","int","Number","of","features","seen","during",":","term",":","`","fit","`",".","..","versionadded",":",":","0.24","feature_names_in_",":","ndarray","of","shape","(","`","n_features_in_","`",",",")","Names","of","features","seen","during",":","term",":","`","fit","`",".","Defined","only","when","`","X","`","has","feature","names","that","are","all","strings",".","..","versionadded",":",":","1.0","See","Also","--","--","--","--","MiniBatchKMeans",":","Alternative","online","implementation","that","does","incremental","updates","of","the","centers","positions","using","mini-batches",".","For","large","scale","learning","(","say","n_samples",">","10k",")","MiniBatchKMeans","is","probably","much","faster","than","the","default","batch","implementation",".","Notes","--","--","-","The","k-means","problem","is","solved","using","either","Lloyd","'s","or","Elkan","'s","algorithm",".","The","average","complexity","is","given","by","O","(","k","n","T",")",",","where","n","is","the","number","of","samples","and","T","is","the","number","of","iteration",".","The","worst","case","complexity","is","given","by","O","(","n^","(","k+2\/p",")",")","with","n","=","n_samples",",","p","=","n_features",".","Refer","to",":","doi",":","`","``","How","slow","is","the","k-means","method","?","''","D.","Arthur","and","S.","Vassilvitskii","-","SoCG2006.","<","10.1145\/1137856.1137880",">","`","for","more","details",".","In","practice",",","the","k-means","algorithm","is","very","fast","(","one","of","the","fastest","clustering","algorithms","available",")",",","but","it","falls","in","local","minima",".","That","'s","why","it","can","be","useful","to","restart","it","several","times",".","If","the","algorithm","stops","before","fully","converging","(","because","of","``","tol","``","or","``","max_iter","``",")",",","``","labels_","``","and","``","cluster_centers_","``","will","not","be","consistent",",","i.e",".","the","``","cluster_centers_","``","will","not","be","the","means","of","the","points","in","each","cluster",".","Also",",","the","estimator","will","reassign","``","labels_","``","after","the","last","iteration","to","make","``","labels_","``","consistent","with","``","predict","``","on","the","training","set",".","Examples","--","--","--","--",">",">",">","from","sklearn.cluster","import","KMeans",">",">",">","import","numpy","as","np",">",">",">","X","=","np.array","(","[","[","1",",","2","]",",","[","1",",","4","]",",","[","1",",","0","]",",","...","[","10",",","2","]",",","[","10",",","4","]",",","[","10",",","0","]","]",")",">",">",">","kmeans","=","KMeans","(","n_clusters=2",",","random_state=0",",","n_init=","''","auto","''",")",".fit","(","X",")",">",">",">","kmeans.labels_","array","(","[","1",",","1",",","1",",","0",",","0",",","0","]",",","dtype=int32",")",">",">",">","kmeans.predict","(","[","[","0",",","0","]",",","[","12",",","3","]","]",")","array","(","[","1",",","0","]",",","dtype=int32",")",">",">",">","kmeans.cluster_centers_","array","(","[","[","10.",",","2",".","]",",","[","1.",",","2",".","]","]",")","For","a","more","detailed","example","of","K-Means","using","the","iris","dataset","see",":","ref",":","`","sphx_glr_auto_examples_cluster_plot_cluster_iris.py","`",".","For","examples","of","common","problems","with","K-Means","and","how","to","address","them","see",":","ref",":","`","sphx_glr_auto_examples_cluster_plot_kmeans_assumptions.py","`",".","For","an","example","of","how","to","use","K-Means","to","perform","color","quantization","see",":","ref",":","`","sphx_glr_auto_examples_cluster_plot_color_quantization.py","`",".","For","a","demonstration","of","how","K-Means","can","be","used","to","cluster","text","documents","see",":","ref",":","`","sphx_glr_auto_examples_text_plot_document_clustering.py","`",".","For","a","comparison","between","K-Means","and","MiniBatchKMeans","refer","to","example",":","ref",":","`","sphx_glr_auto_examples_cluster_plot_mini_batch_kmeans.py","`","."]
code: def _check_params_vs_input(self, X):
      super()._check_params_vs_input(X, default_n_init=10)
  
      self._algorithm = self.algorithm
      if self._algorithm == "elkan" and self.n_clusters == 1:
          warnings.warn(
              (
                  "algorithm='elkan' doesn't make sense for a single "
                  "cluster. Using 'lloyd' instead."
              ),
              RuntimeWarning,
          )
          self._algorithm = "lloyd"
  
imports: import warnings
  from abc import ABC, abstractmethod
  from numbers import Integral, Real
  import numpy
  import scipy.sparse
  from ..base import BaseEstimator, ClassNamePrefixFeaturesOutMixin, ClusterMixin, TransformerMixin, _fit_context
  from ..exceptions import ConvergenceWarning
  from ..metrics.pairwise import _euclidean_distances, euclidean_distances
  from ..utils import check_array, check_random_state
  from ..utils._openmp_helpers import _openmp_effective_n_threads
  from ..utils._param_validation import Interval, StrOptions, validate_params
  from ..utils.extmath import row_norms, stable_cumsum
  from ..utils.fixes import threadpool_info, threadpool_limits
  from ..utils.sparsefuncs import mean_variance_axis
  from ..utils.sparsefuncs_fast import assign_rows_csr
  from ..utils.validation import _check_sample_weight, _is_arraylike_not_scalar, check_is_fitted
  from ._k_means_common import CHUNK_SIZE, _inertia_dense, _inertia_sparse, _is_same_clustering
  from ._k_means_elkan import elkan_iter_chunked_dense, elkan_iter_chunked_sparse, init_bounds_dense, init_bounds_sparse
  from ._k_means_lloyd import lloyd_iter_chunked_dense, lloyd_iter_chunked_sparse
  from ._k_means_minibatch import _minibatch_update_dense, _minibatch_update_sparse
label_desc: Use image node_id 2 for calling the KMeans obj's underlying member method code with example usage: obj._check_params_vs_input(X) without return types