
    -iO?                       S r SSKrSSKJrJr  SSKJrJr  SSKr	SSK
Jr  SSKJrJrJrJrJr  SSKJr  SSKJrJr  SS	KJrJr  SS
KJr  SSKJrJrJ r   SSK!J"r"J#r#  SSK$J%r%J&r&  SSK'J(r(  SSK)J*r*  SSK+J,r,J-r-J.r.J/r/  SSK0J1r1J2r2J3r3J4r4  SSK5J6r6J7r7J8r8J9r9  SSK:J;r;J<r<  SSK=J>r>J?r?  \ " SS/\" \SSSS9/SS/SS/S/\" \SSSS9S/S.SS9SSSSS.S j5       r@ S7S  jrAS! rB\ " SS/SS/\C/S".S#S9SS$S%S&S#S'SSS(S#S).
S* j5       rD    S8S+ jrE\&" SS,S-9    S8S. j5       rFS9S/ jrG\&" SS,S-9" \G5      rH " S0 S1\\\\\5      rI " S2 S3\I5      rJ    S:S4 jrK " S5 S6\I5      rLg);zK-means clustering.    N)ABCabstractmethod)IntegralReal   )BaseEstimatorClassNamePrefixFeaturesOutMixinClusterMixinTransformerMixin_fit_context)ConvergenceWarning)_euclidean_distanceseuclidean_distances)check_arraycheck_random_state)_openmp_effective_n_threads)Interval
StrOptionsvalidate_params)	row_normsstable_cumsum)_get_threadpool_controller _threadpool_controller_decorator)mean_variance_axis)assign_rows_csr)_check_sample_weight_is_arraylike_not_scalarcheck_is_fittedvalidate_data   )
CHUNK_SIZE_inertia_dense_inertia_sparse_is_same_clustering)elkan_iter_chunked_denseelkan_iter_chunked_sparseinit_bounds_denseinit_bounds_sparse)lloyd_iter_chunked_denselloyd_iter_chunked_sparse)_minibatch_update_dense_minibatch_update_sparse
array-likezsparse matrixleftclosedrandom_state)X
n_clusterssample_weightx_squared_normsr1   n_local_trialsTprefer_skip_nested_validation)r4   r5   r1   r6   c                   [        U S[        R                  [        R                  /S9  [	        X U R
                  S9nU R                  S   U:  a  [        SU R                  S    SU S35      eUc  [        U SS	9nO[        X0R
                  S
S9nUR                  S   U R                  S   :w  a,  [        SUR                  S    SU R                  S    S35      e[        U5      n[        XX2XE5      u  pgXg4$ )a  Init n_clusters seeds according to k-means++.

.. versionadded:: 0.24

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    The data to pick seeds from.

n_clusters : int
    The number of centroids to initialize.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in `X`. If `None`, all observations
    are assigned equal weight. `sample_weight` is ignored if `init`
    is a callable or a user provided array.

    .. versionadded:: 1.3

x_squared_norms : array-like of shape (n_samples,), default=None
    Squared Euclidean norm of each data point.

random_state : int or RandomState instance, default=None
    Determines random number generation for centroid initialization. Pass
    an int for reproducible output across multiple function calls.
    See :term:`Glossary <random_state>`.

n_local_trials : int, default=None
    The number of seeding trials for each center (except the first),
    of which the one reducing inertia the most is greedily chosen.
    Set to None to make the number of trials depend logarithmically
    on the number of seeds (2+log(k)) which is the recommended setting.
    Setting to 1 disables the greedy cluster selection and recovers the
    vanilla k-means++ algorithm which was empirically shown to work less
    well than its greedy variant.

Returns
-------
centers : ndarray of shape (n_clusters, n_features)
    The initial centers for k-means.

indices : ndarray of shape (n_clusters,)
    The index location of the chosen centers in the data array X. For a
    given index and center, X[index] = center.

Notes
-----
Selects initial cluster centers for k-mean clustering in a smart way
to speed up convergence. see: Arthur, D. and Vassilvitskii, S.
"k-means++: the advantages of careful seeding". ACM-SIAM symposium
on Discrete algorithms. 2007

Examples
--------

>>> from sklearn.cluster import kmeans_plusplus
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [10, 2], [10, 4], [10, 0]])
>>> centers, indices = kmeans_plusplus(X, n_clusters=2, random_state=0)
>>> centers
array([[10,  2],
       [ 1,  0]])
>>> indices
array([3, 2])
csr)accept_sparsedtyper<   r   
n_samples= should be >= n_clusters=.TsquaredF)r<   	ensure_2dzThe length of x_squared_norms z, should be equal to the length of n_samples )r   npfloat64float32r   r<   shape
ValueErrorr   r   _kmeans_plusplus)r2   r3   r4   r5   r1   r6   centersindicess           J/var/www/html/venv/lib/python3.13/site-packages/sklearn/cluster/_kmeans.pykmeans_plusplusrM   9   s   n rzz2::.FG(IMwwqzJ$=j\K
 	

 #At4%oWWPUVQ1771:-,_-B-B1-E,F G334771:,aA
 	

 &l3L (	|G     c                    U R                   u  pg[        R                  " X4U R                  S9nUc"  S[	        [        R
                  " U5      5      -   nUR                  XcUR                  5       -  S9n	[        R                  " US[        S9n
[        R                  " U 5      (       a  X	/   R                  5       US'   OX	   US'   XS'   [        US[        R                  4   XSS9nX-  n[        S	U5       H  nUR                  US
9U-  n[        R                   " [#        X;-  5      U5      n[        R$                  " USUR&                  S	-
  US9  [        X   XSS9n[        R(                  " UUUS9  UUR+                  SS	5      -  n[        R,                  " U5      nUU   nUU   nUU   n[        R                  " U 5      (       a  U U/   R                  5       X'   OU U   X'   UX'   M     X4$ )a  Computational component for initialization of n_clusters by
k-means++. Prior validation of data is assumed.

Parameters
----------
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
    The data to pick seeds for.

n_clusters : int
    The number of seeds to choose.

sample_weight : ndarray of shape (n_samples,)
    The weights for each observation in `X`.

x_squared_norms : ndarray of shape (n_samples,)
    Squared Euclidean norm of each data point.

random_state : RandomState instance
    The generator used to initialize the centers.
    See :term:`Glossary <random_state>`.

n_local_trials : int, default=None
    The number of seeding trials for each center (except the first),
    of which the one reducing inertia the most is greedily chosen.
    Set to None to make the number of trials depend logarithmically
    on the number of seeds (2+log(k)); this is the default.

Returns
-------
centers : ndarray of shape (n_clusters, n_features)
    The initial centers for k-means.

indices : ndarray of shape (n_clusters,)
    The index location of the chosen centers in the data array X. For a
    given index and center, X[index] = center.
r=   Nr   )pr   T)Y_norm_squaredrB   r    )size)out)rG   rD   emptyr<   intlogchoicesumfullspissparsetoarrayr   newaxisrangeuniformsearchsortedr   cliprS   minimumreshapeargmin)r2   r3   r5   r4   r1   r6   	n_samples
n_featuresrJ   	center_idrK   closest_dist_sqcurrent_potc	rand_valscandidate_idsdistance_to_candidatescandidates_potbest_candidates                      rL   rI   rI      s   N GGIhh
/qww?G  S
!344 ##IARARAT1T#UIggj"C0G	{{1~~{^++-
\
AJ +2::4O "1K 1j! !((n(=K	-9:I
 	t_%9%9A%==Q "6a"

 	

?$:@VW/-2G2GA2NN >2$^40@&~6 ;;q>>N+,446GJ>*GJ#
= "@ rN   c                     US:X  a  g[         R                  " U 5      (       a  [        U SS9S   nO[        R                  " U SS9n[        R
                  " U5      U-  $ )z5Return a tolerance which is dependent on the dataset.r   axisr    )r[   r\   r   rD   varmean)r2   tol	variancess      rL   
_tolerancerx     sN    
ax	{{1~~&qq1!4	FF11%	779##rN   )r2   r4   return_n_iterF	k-means++auto,  -C6?lloyd)
r4   initn_initmax_iterverboserv   r1   copy_x	algorithmry   c       
             [        UUUUUUUU	U
S9	R                  XS9nU(       a.  UR                  UR                  UR                  UR
                  4$ UR                  UR                  UR                  4$ )a  Perform K-means clustering algorithm.

Read more in the :ref:`User Guide <k_means>`.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    The observations to cluster. It must be noted that the data
    will be converted to C ordering, which will cause a memory copy
    if the given data is not C-contiguous.

n_clusters : int
    The number of clusters to form as well as the number of
    centroids to generate.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in `X`. If `None`, all observations
    are assigned equal weight. `sample_weight` is not used during
    initialization if `init` is a callable or a user provided array.

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 centers for k-mean
      clustering in a smart way to speed up convergence. See section
      Notes in k_init for more details.
    - `'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.

n_init : 'auto' or int, default="auto"
    Number of time the k-means algorithm will be run with different
    centroid seeds. The final results will be the best output of
    n_init consecutive runs in terms of inertia.

    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 to run.

verbose : bool, default=False
    Verbosity mode.

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.

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".

return_n_iter : bool, default=False
    Whether or not to return the number of iterations.

Returns
-------
centroid : ndarray of shape (n_clusters, n_features)
    Centroids found at the last iteration of k-means.

label : ndarray of shape (n_samples,)
    The `label[i]` is the code or index of the centroid the
    i'th observation is closest to.

inertia : float
    The final value of the inertia criterion (sum of squared distances to
    the closest centroid for all observations in the training set).

best_n_iter : int
    Number of iterations corresponding to the best results.
    Returned only if `return_n_iter` is set to True.

Examples
--------
>>> import numpy as np
>>> from sklearn.cluster import k_means
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [10, 2], [10, 4], [10, 0]])
>>> centroid, label, inertia = k_means(
...     X, n_clusters=2, n_init="auto", random_state=0
... )
>>> centroid
array([[10.,  2.],
       [ 1.,  2.]])
>>> label
array([1, 1, 1, 0, 0, 0], dtype=int32)
>>> inertia
16.0
)	r3   r   r   r   r   rv   r1   r   r   r4   )KMeansfitcluster_centers_labels_inertia_n_iter_)r2   r3   r4   r   r   r   r   rv   r1   r   r   ry   ests                rL   k_meansr   "  s|    j !
 
c!c)  ##S[[#,,KK##S[[#,,>>rN   c                    U R                   S   nUR                   S   nUn	[        R                  " U	5      n
[        R                  " XR                  S9n[        R
                  " US[        R                  S9nUR                  5       n[        U	5      S-  n[        R                  " [        R                  " U5      SSS9S   n[        R                  " XpR                  S9n[        R                  " Xx4U R                  S9n[        R                  " XR                  S9n[        R                  " U 5      (       a  [        n[        n[        nO[         n["        n[$        nU" U U	UUUUUS9  Sn['        U5       H  nU" U UU	U
UUUUUUUU5        [        U
5      S-  n[        R                  " [        R                  " U5      SSS9S   nU(       a  U" XXU5      n[)        S	U S
U 35        Xp[        R*                  " X5      (       a  U(       a  [)        SU S35        Sn  O?US-  R-                  5       nUU::  a  U(       a  [)        SU SU SU S35          OXSS& M     U(       d  U" U UU	U	UUUUUUUUSS9  U" XXU5      nUUU	WS-   4$ )a  A single run of k-means elkan, assumes preparation completed prior.

Parameters
----------
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
    The observations to cluster. If sparse matrix, must be in CSR format.

sample_weight : array-like of shape (n_samples,)
    The weights for each observation in X.

centers_init : ndarray of shape (n_clusters, n_features)
    The initial centers.

max_iter : int, default=300
    Maximum number of iterations of the k-means algorithm to run.

verbose : bool, default=False
    Verbosity mode.

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.
    It's not advised to set `tol=0` since convergence might never be
    declared due to rounding errors. Use a very small number instead.

n_threads : int, default=1
    The number of OpenMP threads to use for the computation. Parallelism is
    sample-wise on the main cython loop which assigns each sample to its
    closest center.

Returns
-------
centroid : ndarray of shape (n_clusters, n_features)
    Centroids found at the last iteration of k-means.

label : ndarray of shape (n_samples,)
    label[i] is the code or index of the centroid the
    i'th observation is closest to.

inertia : float
    The final value of the inertia criterion (sum of squared distances to
    the closest centroid for all observations in the training set).

n_iter : int
    Number of iterations run.
r   r=   rQ   r   r    )kthrs   	n_threadsF
Iteration 
, inertia Converged at iteration : strict convergence.T: center shift  within tolerance r@   Nupdate_centers)rG   rD   
zeros_likezerosr<   rZ   int32copyr   	partitionasarrayr[   r\   r(   r&   r#   r'   r%   r"   r_   printarray_equalrY   )r2   r4   centers_initr   r   rv   r   rf   r3   rJ   centers_newweight_in_clusterslabels
labels_oldcenter_half_distancesdistance_next_centerupper_boundslower_boundscenter_shiftinit_bounds
elkan_iter_inertiastrict_convergenceiinertiacenter_shift_tots                             rL   _kmeans_single_elkanr     s   p 
I##A&J G--(K*GG<WWY"((3FJ/81<<<


()qq	 88IWW5L88Y3177CL88Jgg6L	{{1~~(.
"'-
!	 8_! 	
" !4K @1 D!||JJ,-11 

  q)LGJqcG956*>>&--/s2GHI!% !-a4463&1!O+,,>se1F 1] ` !  	
  q)DG7GQU**rN   blaslimitsuser_apic                 r   UR                   S   nUn[        R                  " U5      n	[        R                  " U R                   S   S[        R                  S9n
U
R                  5       n[        R                  " XpR                  S9n[        R                  " XpR                  S9n[        R                  " U 5      (       a  [        n[        nO[        n[        nSn[        U5       H  nU" U UUU	UU
UU5        U(       a  U" XXU5      n[        SU SU S35        Xp[        R                   " X5      (       a  U(       a  [        SU S	35        S
n  O?US-  R#                  5       nUU::  a  U(       a  [        SU SU SU S35          OXSS& M     U(       d  U" U UUUUU
UUSS9	  U" XXU5      nU
UUWS-   4$ )a  A single run of k-means lloyd, assumes preparation completed prior.

Parameters
----------
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
    The observations to cluster. If sparse matrix, must be in CSR format.

sample_weight : ndarray of shape (n_samples,)
    The weights for each observation in X.

centers_init : ndarray of shape (n_clusters, n_features)
    The initial centers.

max_iter : int, default=300
    Maximum number of iterations of the k-means algorithm to run.

verbose : bool, default=False
    Verbosity mode

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.
    It's not advised to set `tol=0` since convergence might never be
    declared due to rounding errors. Use a very small number instead.

n_threads : int, default=1
    The number of OpenMP threads to use for the computation. Parallelism is
    sample-wise on the main cython loop which assigns each sample to its
    closest center.

Returns
-------
centroid : ndarray of shape (n_clusters, n_features)
    Centroids found at the last iteration of k-means.

label : ndarray of shape (n_samples,)
    label[i] is the code or index of the centroid the
    i'th observation is closest to.

inertia : float
    The final value of the inertia criterion (sum of squared distances to
    the closest centroid for all observations in the training set).

n_iter : int
    Number of iterations run.
r   rQ   r=   Fr   r   r@   r   r   Tr   r   r   Nr   r    )rG   rD   r   rZ   r   r   r   r<   r[   r\   r*   r#   r)   r"   r_   r   r   rY   )r2   r4   r   r   r   rv   r   r3   rJ   r   r   r   r   r   
lloyd_iterr   r   r   r   r   s                       rL   _kmeans_single_lloydr   o  s   r ##A&J G--(KWWQWWQZ2884FJ*GG<88Jgg6L	{{1~~.
"-
!8_		
 q)LGJqcG9A67*>>&--/s2GHI!% !-a4463&1!O+,,>se1F 1G J  
	
 q)DG7GQU**rN   c                 r   U R                   S   nUR                   S   n[        R                  " US[        R                  S9n[        R                  " XbR
                  S9n[        R                  " U 5      (       a  [        n	[        n
O[        n	[        n
U	" U UUSSUUUSS9	  U(       a  U
" XX'U5      nX{4$ U$ )aN  E step of the K-means EM algorithm.

Compute the labels and the inertia of the given samples and centers.

Parameters
----------
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
    The input samples to assign to the labels. If sparse matrix, must
    be in CSR format.

sample_weight : ndarray of shape (n_samples,)
    The weights for each observation in X.

x_squared_norms : ndarray of shape (n_samples,)
    Precomputed squared euclidean norm of each data point, to speed up
    computations.

centers : ndarray of shape (n_clusters, n_features)
    The cluster centers.

n_threads : int, default=1
    The number of OpenMP threads to use for the computation. Parallelism is
    sample-wise on the main cython loop which assigns each sample to its
    closest center.

return_inertia : bool, default=True
    Whether to compute and return the inertia.

Returns
-------
labels : ndarray of shape (n_samples,)
    The resulting assignment.

inertia : float
    Sum of squared distances of samples to their closest cluster center.
    Inertia is only returned if return_inertia is True.
r   rQ   r=   NF)r   r   r   r   r   r   )rG   rD   rZ   r   r   r<   r[   r\   r*   r#   r)   r"   )r2   r4   rJ   r   return_inertiarf   r3   r   r   _labelsr   r   s               rL   _labels_inertiar     s    L 
Iq!JWWY"((3F88Jmm<L	{{1~~+"*!	!
 1WiHMrN   c                   &  ^  \ rS rSr% Sr\" \SSSS9/\" SS15      \S	/\" S
15      \" \SSSS9/\" \SSSS9/\" \	SSSS9/S/S/S.r
\\S'   S rSS jr\S 5       rS rS rS r  S S jrS S jrS rS S jrS rS rS S jrU 4S jrSrU =r$ )!_BaseKMeansi?  z)Base class for KMeans and MiniBatchKMeansr    Nr.   r/   rz   randomr-   r{   r   r   r1   r3   r   r   r   rv   r   r1   _parameter_constraintsc                X    Xl         X l        X@l        XPl        X0l        X`l        Xpl        g N)r3   r   r   rv   r   r   r1   )selfr3   r   r   r   rv   r   r1   s           rL   __init___BaseKMeans.__init__Q  s(     %	 (rN   c                    UR                   S   U R                  :  a)  [        SUR                   S    SU R                   S35      e[        XR                  5      U l        U R                  S:X  a  [        U R                  [        5      (       a  U R                  S:X  a  SU l
        Op[        U R                  [        5      (       a  U R                  S:X  a  X l
        O:[        U R                  5      (       a  X l
        OSU l
        OU R                  U l
        [        U R                  5      (       aX  U R                  S:w  aG  [        R                  " S	U R                  R                    S
U R                   S3["        SS9  SU l
        g g g )Nr   r>   r?   r@   r{   rz   r    r   zEExplicit initial center position passed: performing only one init in z instead of n_init=r   
stacklevel)rG   r3   rH   rx   rv   _tolr   
isinstancer   str_n_initcallabler   warningswarn	__class____name__RuntimeWarning)r   r2   default_n_inits      rL   _check_params_vs_input"_BaseKMeans._check_params_vs_inputd  s8   771:'QWWQZL(A$//ARRST 
 q((+	 ;;& $))S))dii;.F DIIs++		X0E-$))$$- ;;DL#DII..4<<13DMM$$(NN$;$;#< ="ll^1.  DL 4E.rN   c                     g)zvIssue an estimator specific warning when vcomp and mkl are both present

This method is called by `_check_mkl_vcomp`.
N r   n_active_threadss     rL   _warn_mkl_vcomp_BaseKMeans._warn_mkl_vcomp  s    rN   c           	         [         R                  " U5      (       a  g[        [        R                  " U[
        -  5      5      nX0R                  :  a}  [        5       R                  5       nSU Vs/ s H  oUS   PM	     sn;   nSU Vs/ s H  nUS   UR                  SS5      4PM     sn;   nU(       a  U(       a  U R                  U5        ggggs  snf s  snf )z)Check when vcomp and mkl are both presentNvcompprefix)mklintelinternal_apithreading_layer)r[   r\   rV   rD   ceilr!   
_n_threadsr   infogetr   )r   r2   rf   r   modulesmodule	has_vcomphas_mkls           rL   _check_mkl_vcomp_BaseKMeans._check_mkl_vcomp  s     ;;q>>rwwy:'=>?oo-02779G7#K78$47#KKI&%+%F '4Et)LM%+ G W$$%56 %y .#K+s   /C!Cc                    UR                   S   U R                  :w  a&  [        SUR                    SU R                   S35      eUR                   S   UR                   S   :w  a)  [        SUR                    SUR                   S    S35      eg)z5Check if centers is compatible with X and n_clusters.r   z!The shape of the initial centers z' does not match the number of clusters r@   r    z3 does not match the number of features of the data N)rG   r3   rH   )r   r2   rJ   s      rL   _validate_center_shape"_BaseKMeans._validate_center_shape  s    ==t.3GMM? C004/@C  ==qwwqz)3GMM? C<<=GGAJ<qJ  *rN   c           
      `    [        U USS[        R                  [        R                  /SSS9nU$ )Nr:   FC)r;   resetr<   orderaccept_large_sparse)r   rD   rE   rF   r   r2   s     rL   _check_test_data_BaseKMeans._check_test_data  s5    ::rzz* %
 rN   c                    UR                   S   nUc  U R                  OUn	Ub2  Xh:  a-  UR                  SX5      n
X   nX*   nUR                   S   nXZ   n[        U[        5      (       a  US:X  a  [        UU	UUUS9u  pO[        U[        5      (       a-  US:X  a'  UR                  UU	SXUR                  5       -  S9nX   nOZ[        U R                  5      (       a  UnO=[        U5      (       a-  U" XUS9n[        XR                  SSS	9nU R                  X5        [        R                  " W5      (       a  UR!                  5       nU$ )
a  Compute the initial centroids.

Parameters
----------
X : {ndarray, sparse matrix} of shape (n_samples, n_features)
    The input samples.

x_squared_norms : ndarray of shape (n_samples,)
    Squared euclidean norm of each data point. Pass it if you have it
    at hands already to avoid it being recomputed here.

init : {'k-means++', 'random'}, callable or ndarray of shape                 (n_clusters, n_features)
    Method for initialization.

random_state : RandomState instance
    Determines random number generation for centroid initialization.
    See :term:`Glossary <random_state>`.

sample_weight : ndarray of shape (n_samples,)
    The weights for each observation in X. `sample_weight` is not used
    during initialization if `init` is a callable or a user provided
    array.

init_size : int, default=None
    Number of samples to randomly sample for speeding up the
    initialization (sometimes at the expense of accuracy).

n_centroids : int, default=None
    Number of centroids to initialize.
    If left to 'None' the number of centroids will be equal to
    number of clusters to form (self.n_clusters).

Returns
-------
centers : ndarray of shape (n_clusters, n_features)
    Initial centroids of clusters.
r   rz   )r1   r5   r4   r   F)rS   replacerP   )r1   r   r<   r   r   )rG   r3   randintr   r   rI   rX   rY   r   r   r   r   r<   r   r[   r\   r]   )r   r2   r5   r   r1   r4   	init_sizen_centroidsrf   r3   init_indicesrJ   _seedss                 rL   _init_centroids_BaseKMeans._init_centroids  sI   ` GGAJ	(3(;T__
 Y%:'//9HLA-;O
I)7MdC  T[%8)) /+JGQ c""tx'7 ''"3"3"55	 ( E hG%dii00Gd^^1|DG!'uCPG''3;;woo'GrN   c                 4    U R                  XS9R                  $ )aX  Compute cluster centers and predict cluster index for each sample.

Convenience method; equivalent to calling fit(X) followed by
predict(X).

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    New data to transform.

y : Ignored
    Not used, present here for API consistency by convention.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in X. If None, all observations
    are assigned equal weight.

Returns
-------
labels : ndarray of shape (n_samples,)
    Index of the cluster each sample belongs to.
r   )r   r   r   r2   yr4   s       rL   fit_predict_BaseKMeans.fit_predict  s    . xxx7???rN   c                     [        U 5        U R                  U5      n[        R                  " UR                  S   UR
                  S9n[        UUU R                  U R                  SS9nU$ )a  Predict the closest cluster each sample in X belongs to.

In the vector quantization literature, `cluster_centers_` is called
the code book and each value returned by `predict` is the index of
the closest code in the code book.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    New data to predict.

Returns
-------
labels : ndarray of shape (n_samples,)
    Index of the cluster each sample belongs to.
r   r=   F)r   r   )	r   r   rD   onesrG   r<    _labels_inertia_threadpool_limitr   r   )r   r2   r4   r   s       rL   predict_BaseKMeans.predict*  sd    " 	!!!$ 
!'':1!!oo 
 rN   c                 >    U R                  XS9R                  U5      $ )aL  Compute clustering and transform X to cluster-distance space.

Equivalent to fit(X).transform(X), but more efficiently implemented.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    New data to transform.

y : Ignored
    Not used, present here for API consistency by convention.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in X. If None, all observations
    are assigned equal weight.

Returns
-------
X_new : ndarray of shape (n_samples, n_clusters)
    X transformed in the new space.
r   )r   
_transformr  s       rL   fit_transform_BaseKMeans.fit_transformL  s     , xxx7BB1EErN   c                 \    [        U 5        U R                  U5      nU R                  U5      $ )a  Transform X to a cluster-distance space.

In the new space, each dimension is the distance to the cluster
centers. Note that even if X is sparse, the array returned by
`transform` will typically be dense.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    New data to transform.

Returns
-------
X_new : ndarray of shape (n_samples, n_clusters)
    X transformed in the new space.
)r   r   r  r   s     rL   	transform_BaseKMeans.transformd  s+    " 	!!!$q!!rN   c                 ,    [        XR                  5      $ )z.Guts of transform method; no input validation.)r   r   r   s     rL   r  _BaseKMeans._transformz  s    "1&;&;<<rN   c                     [        U 5        U R                  U5      n[        X1UR                  S9n[	        XU R
                  U R                  5      u  pEU* $ )a  Opposite of the value of X on the K-means objective.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    New data.

y : Ignored
    Not used, present here for API consistency by convention.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in X. If None, all observations
    are assigned equal weight.

Returns
-------
score : float
    Opposite of the value of X on the K-means objective.
r=   )r   r   r   r<   r  r   r   )r   r2   r	  r4   r  scoress         rL   score_BaseKMeans.score~  sR    ( 	!!!$,]QWWM4d33T__
	 wrN   c                 F   > [         TU ]  5       nSUR                  l        U$ )NT)super__sklearn_tags__
input_tagssparse)r   tagsr   s     rL   r   _BaseKMeans.__sklearn_tags__  s!    w')!%rN   )	r   r   r   r   r3   r   r1   rv   r   r   NN)r   
__module____qualname____firstlineno____doc__r   r   r   r   r   r   dict__annotations__r   r   r   r   r   r   r   r  r
  r  r  r  r  r  r   __static_attributes____classcell__r   s   @rL   r   r   ?  s     4  !T&AB[(34hMx Xq$v6
 h4?@q$v67;'($D )&!F  7(
& Tl@2 DF0",=< rN   r   c            
          ^  \ rS rSr% Sr0 \R                  ES/\" SS15      /S.Er\\	S'    SSS	S
SSSSSS.U 4S jjjr
U 4S jrS r\" SS9SS j5       rSrU =r$ )r   i  a  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` strategies, see
    :ref:`sphx_glr_auto_examples_cluster_plot_kmeans_digits.py`.

    For an evaluation of the impact of initialization, see the example
    :ref:`sphx_glr_auto_examples_cluster_plot_kmeans_stability_low_dim_dense.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 examples of common problems with K-Means and how to address them see
:ref:`sphx_glr_auto_examples_cluster_plot_kmeans_assumptions.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`.

For a comparison between K-Means and BisectingKMeans refer to example
:ref:`sphx_glr_auto_examples_cluster_plot_bisect_kmeans.py`.
booleanr~   elkan)r   r   r   rz   r{   r|   r}   r   NT)r   r   r   rv   r   r1   r   r   c          
      D   > [         T
U ]  UUUUUUUS9  Xl        Xl        g )Nr   )r  r   r   r   )r   r3   r   r   r   rv   r   r1   r   r   r   s             rL   r   KMeans.__init__a  s8     	!% 	 	
 "rN   c                    > [         TU ]  USS9  U R                  U l        U R                  S:X  a4  U R                  S:X  a#  [
        R                  " S[        5        SU l        g g g )N
   r   r1  r    zQalgorithm='elkan' doesn't make sense for a single cluster. Using 'lloyd' instead.r~   )r  r   r   
_algorithmr3   r   r   r   r   r2   r   s     rL   r   KMeans._check_params_vs_input{  s^    &q&<..??g%$//Q*>MM6  &DO +?%rN   c                 8    [         R                  " SU S35        g)(Warn when vcomp and mkl are both presentzKMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can avoid it by setting the environment variable OMP_NUM_THREADS=r@   N)r   r   r   s     rL   r   KMeans._warn_mkl_vcomp  s"    ) *:(:!=	
rN   r7   c                    [        U US[        R                  [        R                  /SU R                  SS9nU R                  U5        [        U R                  5      n[        X1UR                  S9n[        5       U l        U R                  n[        U5      nU(       a&  [        XQR                  SSS9nU R                  X5        [         R"                  " U5      (       d  UR%                  SS	9nX-  nU(       a  XW-  n['        USS
9nU R(                  S:X  a  [*        n	O$[,        n	U R/                  XR0                  S   5        Su  p[3        U R4                  5       H  nU R7                  UUUUUS9nU R8                  (       a  [;        S5        U	" UUUU R<                  U R8                  U R>                  U R                  S9u  pnnU
b$  X:  d  Mt  [A        XU RB                  5      (       a  M  UnUnUn
UnM     [         R"                  " U5      (       d  U R                  (       d  UW-  nWW-  n[E        [G        U5      5      nUU RB                  :  a4  [H        RJ                  " SRM                  UU RB                  5      [N        SS9  WU l(        U RP                  R0                  S   U l)        Xl*        Xl+        WU l,        U $ )a"  Compute k-means clustering.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Training instances to cluster. It must be noted that the data
    will be converted to C ordering, which will cause a memory
    copy if the given data is not C-contiguous.
    If a sparse matrix is passed, a copy will be made if it's not in
    CSR format.

y : Ignored
    Not used, present here for API consistency by convention.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in X. If None, all observations
    are assigned equal weight. `sample_weight` is not used during
    initialization if `init` is a callable or a user provided array.

    .. versionadded:: 0.20

Returns
-------
self : object
    Fitted estimator.
r:   r   F)r;   r<   r   r   r   r=   Tr   r   rr   rA   r1  r%  )r5   r   r1   r4   zInitialization complete)r   r   rv   r   zkNumber of distinct clusters ({}) found smaller than n_clusters ({}). Possibly due to duplicate points in X.r   r   )-r   rD   rE   rF   r   r   r   r1   r   r<   r   r   r   r   r   r   r[   r\   ru   r   r7  r   r   r   rG   r_   r   r  r   r   r   r   r$   r3   lensetr   r   formatr   r   _n_features_outr   r   r   )r   r2   r	  r4   r1   r   init_is_array_likeX_meanr5   kmeans_singlebest_inertiabest_labelsr   r   r   r   rJ   r   best_centersbest_n_iterdistinct_clusterss                        rL   r   
KMeans.fit  st   8 ::rzz* %
 	##A&)$*;*;<,]QWWM57 yy5d;t77SID''0 {{1~~VVV^FKA! $At4??g%0M0M!!!WWQZ0$.!t||$A// /)+ 0 L ||/0 1>II//1-FWg #&+FQQ$&&%G %J {{1~~;;VF"LK 01t.MM0$//B" !-#44::1="$"rN   )	r7  rA  r   r   r   r   r   r   r      r%  )r   r&  r'  r(  r)  r   r   r   r*  r+  r   r   r   r   r   r,  r-  r.  s   @rL   r   r     s    vp$

,
,$+ '7!345$D  # # #4&
 5 6rN   r   c
           	      Z   [        XX)S9u  p[        R                  " U 5      (       a  [        XX#XJU	5        O[	        U UUUUU
U	5        U(       Ga[  US:  GaT  XGUR                  5       -  :  nUR                  5       SU R                  S   -  :  a6  [        R                  " U5      [        SU R                  S   -  5      S nSX'   UR                  5       nU(       a  UR                  U R                  S   SUS9nU(       a  [        SU S35        [        R                  " U 5      (       a^  [        U UR                  [        R                  SS	9[        R                   " U5      S   R                  [        R                  SS	9U5        OX   X<'   [        R"                  " XL)    5      XL'   U$ )
a  Incremental update of the centers for the Minibatch K-Means algorithm.

Parameters
----------

X : {ndarray, sparse matrix} of shape (n_samples, n_features)
    The original data array. If sparse, must be in CSR format.

x_squared_norms : ndarray of shape (n_samples,)
    Squared euclidean norm of each data point.

sample_weight : ndarray of shape (n_samples,)
    The weights for each observation in `X`.

centers : ndarray of shape (n_clusters, n_features)
    The cluster centers before the current iteration

centers_new : ndarray of shape (n_clusters, n_features)
    The cluster centers after the current iteration. Modified in-place.

weight_sums : ndarray of shape (n_clusters,)
    The vector in which we keep track of the numbers of points in a
    cluster. This array is modified in place.

random_state : RandomState instance
    Determines random number generation for low count centers reassignment.
    See :term:`Glossary <random_state>`.

random_reassign : boolean, default=False
    If True, centers with very low counts are randomly reassigned
    to observations.

reassignment_ratio : float, default=0.01
    Control the fraction of the maximum number of counts for a
    center to be reassigned. A higher value means that low count
    centers are more likely to be reassigned, which means that the
    model will take longer to converge, but should converge in a
    better clustering.

verbose : bool, default=False
    Controls the verbosity.

n_threads : int, default=1
    The number of OpenMP threads to use for the computation.

Returns
-------
inertia : float
    Sum of squared distances of samples to their closest cluster center.
    The inertia is computed after finding the labels and before updating
    the centers.
r   r   g      ?NF)r   rS   z[MiniBatchKMeans] Reassigning z cluster centers.)r   )r   r[   r\   r,   r+   maxrY   rG   rD   argsortrV   rX   r   r   astypeintpwheremin)r2   r4   rJ   r   weight_sumsr1   random_reassignreassignment_ratior   r   r   r   to_reassignindices_dont_reassignn_reassignsnew_centerss                   rL   _mini_batch_stepr[    s   F &aUOF 
{{1~~ gK	
 	 	
 -1!9J$JJ ??sQWWQZ//$&JJ{$;Caggaj@P<Q<S$T!16K.!oo'&--
E . K 6{mCTUV{{1~~&&rwwU&;HH[)!,33BGG%3H	 ,->(
 $&66+l*C#D NrN   c                   "  ^  \ rS rSr% Sr0 \R                  E\" \SSSS9/S/\" \SSSS9S/\" \SSSS9S/\" \	SSSS9/S	.Er\
\S
'    SSSSSSSSSSSSS.U 4S jjjrU 4S jrS rS rS r\" SS9SS j5       r\" SS9SS j5       rSrU =r$ )MiniBatchKMeansi  a`  
Mini-Batch K-Means clustering.

Read more in the :ref:`User Guide <mini_batch_kmeans>`.

Parameters
----------

n_clusters : int, default=8
    The number of clusters to form as well as the number of
    centroids to generate.

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 evaluation of the impact of initialization, see the example
    :ref:`sphx_glr_auto_examples_cluster_plot_kmeans_stability_low_dim_dense.py`.

max_iter : int, default=100
    Maximum number of iterations over the complete dataset before
    stopping independently of any early stopping criterion heuristics.

batch_size : int, default=1024
    Size of the mini batches.
    For faster computations, you can set the ``batch_size`` greater than
    256 * number of cores to enable parallelism on all cores.

    .. versionchanged:: 1.0
       `batch_size` default changed from 100 to 1024.

verbose : int, default=0
    Verbosity mode.

compute_labels : bool, default=True
    Compute label assignment and inertia for the complete dataset
    once the minibatch optimization has converged in fit.

random_state : int, RandomState instance or None, default=None
    Determines random number generation for centroid initialization and
    random reassignment. Use an int to make the randomness deterministic.
    See :term:`Glossary <random_state>`.

tol : float, default=0.0
    Control early stopping based on the relative center changes as
    measured by a smoothed, variance-normalized of the mean center
    squared position changes. This early stopping heuristics is
    closer to the one used for the batch variant of the algorithms
    but induces a slight computational and memory overhead over the
    inertia heuristic.

    To disable convergence detection based on normalized center
    change, set tol to 0.0 (default).

max_no_improvement : int, default=10
    Control early stopping based on the consecutive number of mini
    batches that does not yield an improvement on the smoothed inertia.

    To disable convergence detection based on inertia, set
    max_no_improvement to None.

init_size : int, default=None
    Number of samples to randomly sample for speeding up the
    initialization (sometimes at the expense of accuracy): the
    only algorithm is initialized by running a batch KMeans on a
    random subset of the data. This needs to be larger than n_clusters.

    If `None`, the heuristic is `init_size = 3 * batch_size` if
    `3 * batch_size < n_clusters`, else `init_size = 3 * n_clusters`.

n_init : 'auto' or int, default="auto"
    Number of random initializations that are tried.
    In contrast to KMeans, the algorithm is only run once, using the best of
    the `n_init` initializations as measured by 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:
    3 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'` in version.

reassignment_ratio : float, default=0.01
    Control the fraction of the maximum number of counts for a center to
    be reassigned. A higher value means that low count centers are more
    easily reassigned, which means that the model will take longer to
    converge, but should converge in a better clustering. However, too high
    a value may cause convergence issues, especially with a small batch
    size.

Attributes
----------

cluster_centers_ : ndarray of shape (n_clusters, n_features)
    Coordinates of cluster centers.

labels_ : ndarray of shape (n_samples,)
    Labels of each point (if compute_labels is set to True).

inertia_ : float
    The value of the inertia criterion associated with the chosen
    partition if compute_labels is set to True. If compute_labels is set to
    False, it's an approximation of the inertia based on an exponentially
    weighted average of the batch inertiae.
    The inertia is defined as the sum of square distances of samples to
    their cluster center, weighted by the sample weights if provided.

n_iter_ : int
    Number of iterations over the full dataset.

n_steps_ : int
    Number of minibatches processed.

    .. versionadded:: 1.0

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
--------
KMeans : The classic implementation of the clustering method based on the
    Lloyd's algorithm. It consumes the whole set of input data at each
    iteration.

Notes
-----
See https://www.eecs.tufts.edu/~dsculley/papers/fastkmeans.pdf

When there are too few points in the dataset, some centers may be
duplicated, which means that a proper clustering in terms of the number
of requesting clusters and the number of returned clusters will not
always match. One solution is to set `reassignment_ratio=0`, which
prevents reassignments of clusters that are too small.

See :ref:`sphx_glr_auto_examples_cluster_plot_birch_vs_minibatchkmeans.py` for a
comparison with :class:`~sklearn.cluster.BIRCH`.

Examples
--------
>>> from sklearn.cluster import MiniBatchKMeans
>>> import numpy as np
>>> X = np.array([[1, 2], [1, 4], [1, 0],
...               [4, 2], [4, 0], [4, 4],
...               [4, 5], [0, 1], [2, 2],
...               [3, 2], [5, 5], [1, -1]])
>>> # manually fit on batches
>>> kmeans = MiniBatchKMeans(n_clusters=2,
...                          random_state=0,
...                          batch_size=6,
...                          n_init="auto")
>>> kmeans = kmeans.partial_fit(X[0:6,:])
>>> kmeans = kmeans.partial_fit(X[6:12,:])
>>> kmeans.cluster_centers_
array([[3.375, 3.  ],
       [0.75 , 0.5 ]])
>>> kmeans.predict([[0, 0], [4, 4]])
array([1, 0], dtype=int32)
>>> # fit on the whole data
>>> kmeans = MiniBatchKMeans(n_clusters=2,
...                          random_state=0,
...                          batch_size=6,
...                          max_iter=10,
...                          n_init="auto").fit(X)
>>> kmeans.cluster_centers_
array([[3.55102041, 2.48979592],
       [1.06896552, 1.        ]])
>>> kmeans.predict([[0, 0], [4, 4]])
array([1, 0], dtype=int32)

For a comparison of Mini-Batch K-Means clustering with other clustering algorithms,
see :ref:`sphx_glr_auto_examples_cluster_plot_cluster_comparison.py`
r    Nr.   r/   r0  r   )
batch_sizecompute_labelsmax_no_improvementr   rV  r   rz   d   i   T        r5  r{   {Gz?)r   r   r^  r   r_  r1   rv   r`  r   r   rV  c          
      h   > [         TU ]  UUUUUUUS9  Xl        X@l        X`l        Xl        Xl        g )N)r3   r   r   r   r1   rv   r   )r  r   r`  r^  r_  r   rV  )r   r3   r   r   r^  r   r_  r1   rv   r`  r   r   rV  r   s                rL   r   MiniBatchKMeans.__init__b  sJ      	!% 	 	
 #5$,""4rN   c                   > [         TU ]  USS9  [        U R                  UR                  S   5      U l        U R                  U l        U R                  cC  SU R
                  -  U l        U R                  U R                  :  a  SU R                  -  U l        OcU R                  U R                  :  aI  [        R                  " SU R                   SU R                   S3[        SS9  SU R                  -  U l        [        U R                  UR                  S   5      U l        U R                  S:  a  [        S	U R                   S
35      eg )N   r6  r   z
init_size=z" should be larger than n_clusters=z,. Setting it to min(3*n_clusters, n_samples)r   r   z'reassignment_ratio should be >= 0, got z	 instead.)r  r   rS  r^  rG   _batch_sizer   
_init_sizer3   r   r   r   rV  rH   r8  s     rL   r   &MiniBatchKMeans._check_params_vs_input  s'   &q&;t
; ..??"$"2"22DO0"#doo"5__t.MM  1 2""&//!2 333   $//1DOdooqwwqz: ""Q&9**+96  'rN   c                 ^    [         R                  " SU R                  [        -   SU 35        g)r;  zMiniBatchKMeans is known to have a memory leak on Windows with MKL, when there are less chunks than available threads. You can prevent it by setting batch_size >= z8 or by setting the environment variable OMP_NUM_THREADS=N)r   r   r   r!   r   s     rL   r   MiniBatchKMeans._warn_mkl_vcomp  s4     "__z9: ;/02	
rN   c           
      J   XPR                   -  nUS-   nUS:X  a&  U R                  (       a  [        SU SU SU 35        gU R                  c  XPl        O=U R                   S-  US-   -  n[	        US5      nU R                  SU-
  -  XV-  -   U l        U R                  (       a!  [        SU SU SU SU R                   35        U R
                  S:  a2  X@R
                  ::  a#  U R                  (       a  [        S	U SU 35        g
U R                  b  U R                  U R                  :  a  SU l        U R                  U l        OU =R                  S-  sl        U R                  b=  U R                  U R                  :  a#  U R                  (       a  [        SU SU 35        g
g)z7Helper function to encapsulate the early stopping logicr    zMinibatch step /z: mean batch inertia: Fg       @z, ewa inertia: rb  z)Converged (small centers change) at step Tr   z3Converged (lack of improvement in inertia) at step )	rh  r   r   _ewa_inertiarS  r   _ewa_inertia_min_no_improvementr`  )r   stepn_stepsrf   centers_squared_diffbatch_inertiaalphas          rL   _mini_batch_convergence'MiniBatchKMeans._mini_batch_convergence  s    	))) ax 19||%dV1WI 6  -0 
 $ -$$s*i!m<EqME $ 1 1QY ?-BW WD <<!$q	1G /1B1B0CE 99s?3yy@||A$q	RS   (D,=,=@U@U,U#$D $($5$5D!  A%  ##/$$(?(??||IfAgY( rN   c                     U =R                   U R                  -  sl         U R                  S:H  R                  5       (       d  U R                   SU R                  -  :  a  SU l         gg)zCheck if a random reassignment needs to be done.

Do random reassignments each time 10 * n_clusters samples have been
processed.

If there are empty clusters we always want to reassign.
r   r5  TF)_n_since_last_reassignrh  _countsanyr3   )r   s    rL   _random_reassign MiniBatchKMeans._random_reassign  s\     	##t'7'77#LLA""$$(C(C )
 +,D'rN   r7   c                    [        U US[        R                  [        R                  /SSS9nU R	                  U5        [        U R                  5      n[        X1UR                  S9n[        5       U l
        UR                  u  pVU R                  n[        U5      (       a&  [        XqR                  SSS9nU R                  X5        U R!                  XR"                  5        [%        USS9nUR'                  S	XPR(                  5      n	X   n
X9   nS
n[+        U R,                  5       H  nU R.                  (       a!  [1        SUS-    SU R,                   SU 35        U R3                  UUUUU R(                  US9n[5        U
UUU R                  S9u  nnU R.                  (       a!  [1        SUS-    SU R,                   SU 35        Ub  UU:  d  M  UnUnM     Wn[        R6                  " U5      n[        R8                  " U R:                  UR                  S9U l        S
U l        S
U l         S	U l!        S	U l"        U RF                  U-  U R"                  -  n[I        5       RK                  SSS9   [+        U5       H  nUR'                  S	XPR"                  5      n[M        UU   UU   UUU R<                  UU RO                  5       U RP                  U R.                  U R                  S9
nU RR                  S:  a  [        RT                  " UU-
  S-  5      nOS	nUUnnU RW                  UUUUU5      (       d  M    O   S
S
S
5        UU l,        U RX                  R                  S	   U l-        WS-   U l.        [_        [        R`                  " US-   U R"                  -  U-  5      5      U l1        U Rd                  (       a/  [5        UUU RX                  U R                  S9u  U l3        U l4        U $ U R>                  U-  U l4        U $ ! , (       d  f       N= f)aC  Compute the centroids on X by chunking it into mini-batches.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Training instances to cluster. It must be noted that the data
    will be converted to C ordering, which will cause a memory copy
    if the given data is not C-contiguous.
    If a sparse matrix is passed, a copy will be made if it's not in
    CSR format.

y : Ignored
    Not used, present here for API consistency by convention.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in X. If None, all observations
    are assigned equal weight. `sample_weight` is not used during
    initialization if `init` is a callable or a user provided array.

    .. versionadded:: 0.20

Returns
-------
self : object
    Fitted estimator.
r:   r   F)r;   r<   r   r   r=   Tr   rA   r   NzInit r    rn  z with method r5   r   r1   r   r4   r   zInertia for init z: r   r   )
r2   r4   rJ   r   rT  r1   rU  rV  r   r   rb  r   )5r   rD   rE   rF   r   r   r1   r   r<   r   r   rG   r   r   r   r   r   rh  r   r   ri  r_   r   r   r   r  r  
empty_liker   r3   r{  ro  rp  rq  rz  r   r   limitr[  r}  rV  r   rY   rw  r   rA  n_steps_rV   r   r   r_  r   r   )r   r2   r	  r4   r1   rf   rg   r   r5   validation_indicesX_validsample_weight_validrE  init_idxcluster_centersr  r   init_centersrJ   r   rs  r   minibatch_indicesru  rt  s                            rL   r   MiniBatchKMeans.fit  s   8 ::rzz* %
 	##A&)$*;*;<,]QWWM57 !	 yy#D))t77SID''0a!1!12 $At4 *11!YP'+? dll+H||hl^1T\\N-vNO #22 /)//+ 3 O :#//	JAw ||)(Q,qb	RS#w'=.&7 ,: mmG, xxqww? ! $  '(#==9,1A1AA')//q6/J7^$0$8$8IGWGW$X! !1)*"/0A"B# + $!-$($9$9$;'+'>'> LL"oo! 99s?+-66;3HQ2N+O(+,('2G //w	+?  ; $ KB !(#44::1=A277QUd.>.>$>)#KLM*J%%//	+'DL$-  !--	9DMc KJs   -CO=5O==
Pc                    [        U S5      n[        U US[        R                  [        R                  /SSU(       + S9n[        U S[        U R                  5      5      U l        [        X1UR                  S9n[        U SS	5      U l        [        US
S9nU(       d  U R                  U5        [        5       U l        U R                   n[#        U5      (       a&  [%        XaR                  S
SS9nU R'                  X5        U R)                  XR*                  S	   5        U R-                  UUUU R                  U R.                  US9U l        [        R2                  " U R4                  UR                  S9U l        S	U l        [;        5       R=                  SSS9   [?        UUU R0                  U R0                  U R6                  U R                  U RA                  5       U RB                  U RD                  U R                  S9
  SSS5        U RF                  (       a-  [I        UUU R0                  U R                  S9u  U l%        U l&        U =R                  S-  sl        U R0                  R*                  S	   U l'        U $ ! , (       d  f       N= f)a$  Update k means estimate on a single mini-batch X.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Training instances to cluster. It must be noted that the data
    will be converted to C ordering, which will cause a memory copy
    if the given data is not C-contiguous.
    If a sparse matrix is passed, a copy will be made if it's not in
    CSR format.

y : Ignored
    Not used, present here for API consistency by convention.

sample_weight : array-like of shape (n_samples,), default=None
    The weights for each observation in X. If None, all observations
    are assigned equal weight. `sample_weight` is not used during
    initialization if `init` is a callable or a user provided array.

Returns
-------
self : object
    Return updated estimator.
r   r:   r   F)r;   r<   r   r   r   _random_stater=   r  r   TrA   r   r  r    r   r   )	r4   rJ   r   rT  r1   rU  rV  r   r   Nr   )(hasattrr   rD   rE   rF   getattrr   r1   r  r   r<   r  r   r   r   r   r   r   r   r   r   rG   r  ri  r   r   r3   r{  rz  r   r  r[  r}  rV  r   r_  r  r   r   rA  )r   r2   r	  r4   has_centersr5   r   s          rL   partial_fitMiniBatchKMeans.partial_fit  s/   4 d$67::rzz* %!/
 %/#5d6G6G#H
 -]QWWMj!4 $At4''*9;DO 99D'--"4wwTM++A4!!!WWQZ0 %)$8$8 /!////+ %9 %D! 88DOO177CDL +,D'')//q6/J+-- 11 LL!// $ 5 5 7#'#:#:// K *J%%//	+'DL$- 	#44::1=3 KJs   A'I77
J)rh  r{  ro  rp  ri  rA  rz  r   rq  r  r^  r   r_  r   r   r   r`  r   r  rV  rK  r%  )r   r&  r'  r(  r)  r   r   r   r   r   r*  r+  r   r   r   rw  r}  r   r   r  r,  r-  r.  s   @rL   r]  r]    s    HT$

,
,$!T&AB$+'!T&I4PxD@$G'afEF$D  5 5 5@>	
?B  5Z 6Zx 5d 6drN   r]  r   )r|   Fr}   r    )r    T)Frc  Fr    )Mr)  r   abcr   r   numbersr   r   numpyrD   scipy.sparser"  r[   baser   r	   r
   r   r   
exceptionsr   metrics.pairwiser   r   utilsr   r   utils._openmp_helpersr   utils._param_validationr   r   r   utils.extmathr   r   utils.parallelr   r   utils.sparsefuncsr   utils.sparsefuncs_fastr   utils.validationr   r   r   r   _k_means_commonr!   r"   r#   r$   _k_means_elkanr%   r&   r'   r(   _k_means_lloydr)   r*   _k_means_minibatchr+   r,   rM   rI   rx   boolr   r   r   r   r  r   r   r[  r]  r   rN   rL   <module>r     s   
  # "    , H 3 ? K K 4 3 4    P Q O,!T&AB&-($/'(#HafEtL #'
 g
gV QUbR$ O,&-
 #( 	[?[?D b+N "V<
 @+ =@+FCN $Dv$$  
`#%5|]TW`Fp[ pt vrq	k q	rN   