`L iO? dZddlZddlmZmZddlmZmZddlZ ddl m Z ddl mZmZmZmZmZddlmZddlmZmZdd lmZmZdd lmZdd lmZmZm Z dd l!m"Z"m#Z#dd l$m%Z%m&Z&ddl'm(Z(ddl)m*Z*ddl+m,Z,m-Z-m.Z.m/Z/ddl0m1Z1m2Z2m3Z3m4Z4ddl5m6Z6m7Z7m8Z8m9Z9ddl:m;Z;mZ>m?Z?e ddgeedddgddgddgdgeeddddgddddddddZ@ d7d ZAd!ZBe ddgddgeCgd"d#dd$d%d&d#d'ddd(d#d) d*ZD d8d+ZEe&dd,- d8d.ZFd9d/ZGe&dd,-eGZHGd0d1eeeeeZIGd2d3eIZJ d:d4ZKGd5d6eIZLy);zK-means clustering.N)ABCabstractmethod)IntegralReal) BaseEstimatorClassNamePrefixFeaturesOutMixin ClusterMixinTransformerMixin _fit_context)ConvergenceWarning)_euclidean_distanceseuclidean_distances) check_arraycheck_random_state)_openmp_effective_n_threads)Interval StrOptionsvalidate_params) row_norms stable_cumsum)_get_threadpool_controller _threadpool_controller_decorator)mean_variance_axis)assign_rows_csr)_check_sample_weight_is_arraylike_not_scalarcheck_is_fitted validate_data) CHUNK_SIZE_inertia_dense_inertia_sparse_is_same_clustering)elkan_iter_chunked_denseelkan_iter_chunked_sparseinit_bounds_denseinit_bounds_sparse)lloyd_iter_chunked_denselloyd_iter_chunked_sparse)_minibatch_update_dense_minibatch_update_sparse array-likez sparse matrixleftclosed random_state)X n_clusters sample_weightx_squared_normsr1n_local_trialsTprefer_skip_nested_validation)r4r5r1r6ct|dtjtjgt |||j }|j d|krtd|j dd|d|t|d }nt||j d }|j d|j dk7r,td |j dd |j ddt|}t||||||\}}||fS)al 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 `. 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_sparsedtyper<r n_samples= should be >= n_clusters=.TsquaredF)r< ensure_2dzThe length of x_squared_norms z, should be equal to the length of n_samples ) rnpfloat64float32rr<shape ValueErrorrr_kmeans_plusplus)r2r3r4r5r1r6centersindicess ]/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/cluster/_kmeans.pykmeans_plusplusrM9snrzz2::.FG(IMwwqzJ $=j\ K  #At4%oQWWPUVQ1771:-,_-B-B1-E,FG334771:,a A  &l3L( : |^GW G c |j\}}tj||f|j}|!dt tj |z}|j |||jz } tj|dt} tj|r|| gj|d<n|| |d<| | d<t|dtjf||d} | |z} td |D]} |j| | z}tj t#|| z|}tj$|d| j&d z | t||||d}tj(| || ||j+dd z}tj,|}||} ||} ||}tj|r||gj|| <n|||| <|| | <|| fS) aComputational 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 `. 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)prT)Y_norm_squaredrBr )size)out)rGrDemptyr<intlogchoicesumfullspissparsetoarrayrnewaxisrangeuniform searchsortedrcliprSminimumreshapeargmin)r2r3r5r4r1r6 n_samples n_featuresrJ center_idrKclosest_dist_sq current_potc rand_vals candidate_idsdistance_to_candidatescandidates_potbest_candidates rLrIrIsNGGIzhh J/qww?GS !344##IARARAT1T#UIggj"C0G {{1~ {^++- y\ GAJ+2:: /4O"M1K1j !$!((n(= K  -/9 :I   t_%9%9A%==Q"6 m a"  ?$:@VW/-2G2GA2NN>2$^4 0@&~6 ;;q>N+,446GAJ>*GAJ# =$@ G rNc|dk(rytj|rt|dd}ntj|d}tj ||zS)z5Return a tolerance which is dependent on the dataset.raxisr )r[r\rrDvarmean)r2tol variancess rL _tolerancerxsL ax {{1~&qq1!4 FF11% 779  ##rN)r2r4 return_n_iterF k-means++auto,-C6?lloyd) r4initn_initmax_iterverboservr1copy_x algorithmryc t|||||||| |  j||} | r.| j| j| j| j fS| j| j| jfS)a'Perform K-means clustering algorithm. Read more in the :ref:`User Guide `. 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 `. 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 ) r3rrrrrvr1rrr4)KMeansfitcluster_centers_labels_inertia_n_iter_) r2r3r4rrrrrvr1rrryests rLk_meansr"s~j   !  c!=c)##S[[#,, KK##S[[#,,>>rNc|jd}|jd}|} tj| } tj||j} tj |dtj } | j} t| dz }tjtj|ddd}tj||j}tj||f|j}tj||j}tj|rt}t}t}nt }t"}t$}||| || |||d}t'|D]}|||| | | ||||| || t| dz }tjtj|ddd}|r|||| | |}t)d |d || | } } tj*| | r|rt)d |d d }n8|dzj-}||kr|rt)d |d|d|dn| | dd|s|||| | | ||||| ||d |||| | |}| || dzfS)aA 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. rr=rQrr )kthrs n_threadsF Iteration , inertia Converged at iteration : strict convergence.T: center shift  within tolerance r@Nupdate_centers)rGrD zeros_likezerosr<rZint32copyr partitionasarrayr[r\r(r&r#r'r%r"r_print array_equalrY)r2r4 centers_initrrrvrrfr3rJ centers_newweight_in_clusterslabels labels_oldcenter_half_distancesdistance_next_center upper_bounds lower_bounds center_shift init_bounds elkan_iter_inertiastrict_convergenceiinertiacenter_shift_tots rL_kmeans_single_elkanrsp I##A&JG--(K*AGG< WWY"(( 3FJ/81<<< ()qq 88IQWW5L88Y 3177CL88Jagg6L {{1~( . "' - !  8_.     !      "!4K @1 D!|| JJ, -11   q-&)LG JqcG95 6*G >>&* -/s2GHI!%  !-a446 3&1!O+,,>se1F 1 ].`      !       q-&)DG 7GQU **rNblaslimitsuser_apic P|jd}|}tj|} tj|jddtj} | j } tj ||j} tj ||j} tj|r t}t}n t}t}d}t|D]}||||| | | | ||r||||| |}td|d|d| |} }tj | | r|rtd|d d }n8| d zj#}||kr|rtd|d |d |dn| | dd|s|||||| | | |d ||||| |}| ||dzfS)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. rrQr=Frrr@rrTrrrNrr )rGrDrrZrrrr<r[r\r*r#r)r"r_rrrY)r2r4rrrrvrr3rJrrrrr lloyd_iterrrrrrs rL_kmeans_single_lloydrosr##A&JG--(K WWQWWQZ288 4FJ*AGG<88Jagg6L {{1~. "- ! 8_#         q-&)LG JqcG9A6 7*G >>&* -/s2GHI!%  !-a446 3&1!O+,,>se1F 1 G#J          q-&)DG 7GQU **rNc r|jd}|jd}tj|dtj}tj||j }t j|r t} t} n t} t} | |||dd|||d |r| |||||} || fS|S)aE 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. rrQr=NF)rrrrrr) rGrDrZrrr<r[r\r*r#r)r") r2r4rJrreturn_inertiarfr3rr_labelsrrs rL_labels_inertiarsL Iq!J WWY"(( 3F88Jgmmr?r@r{rzr rzEExplicit initial center position passed: performing only one init in z instead of n_init=r stacklevel)rGr3rHrxrv_tolr isinstancerstr_n_initcallablerwarningswarn __class____name__RuntimeWarning)rr2default_n_inits rL_check_params_vs_inputz"_BaseKMeans._check_params_vs_inputds2 771: 'QWWQZL(A$//ARRST  q$((+  ;;& $))S)dii;.F  DIIs+ X0E- $))$-   ;;DL #DII .4<<13D MM$$(NN$;$;#<="ll^1. DL4E .rNcy)zIssue an estimator specific warning when vcomp and mkl are both present This method is called by `_check_mkl_vcomp`. Nrn_active_threadss rL_warn_mkl_vcompz_BaseKMeans._warn_mkl_vcompsrNc tj|ryttj|t z }||j krltj}d|Dcgc]}|d c}v}d|Dcgc]}|d|jddfc}v}|r|r|j|yyyycc}wcc}w)z)Check when vcomp and mkl are both presentNvcompprefix)mklintel internal_apithreading_layer) r[r\rVrDceilr! _n_threadsrinfogetr)rr2rfrmodulesmodule has_vcomphas_mkls rL_check_mkl_vcompz_BaseKMeans._check_mkl_vcomps ;;q> rwwy:'=>? doo -02779G7#KF8$4#KKI&%+'4Et)LM+GW$$%56%y .#K+s ( B8=B=c|jd|jk7r&td|jd|jd|jd|jdk7r)td|jd|jddy)z5Check if centers is compatible with X and n_clusters.rz!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)rGr3rH)rr2rJs rL_validate_center_shapez"_BaseKMeans._validate_center_shapes == t .3GMM?C004/@C  == qwwqz )3GMM?C<<=GGAJ`. 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. rrz)r1r5r4rF)rSreplacerP)r1rr<rr)rGr3randintrrrIrXrYrrrrr<rr[r\r])rr2r5rr1r4 init_size n_centroidsrfr3 init_indicesrJ_seedss rL_init_centroidsz_BaseKMeans._init_centroidssO`GGAJ (3(;T__  Y%:'//9iHL,A-l;O I),7M dC T[%8)) /+ JGQc "tx'7 ''-"3"3"55 (E hG %dii 0G d^1j|DG!'uCPG  ' '7 3 ;;w oo'GrNc<|j||jS)aCompute 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)rrrr2yr4s rL fit_predictz_BaseKMeans.fit_predicts.xxx7???rNct||j|}tj|jd|j }t |||j|jd}|S)aPredict 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. rr=F)rr) rrrDonesrGr< _labels_inertia_threadpool_limitrr)rr2r4rs rLpredictz_BaseKMeans.predict*sd"   ! !! $ !'': 1   ! !oo   rNcF|j||j|S)aCompute 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 _transformrs rL fit_transformz_BaseKMeans.fit_transformLs",xxx7BB1EErNc\t||j|}|j|S)aTransform 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. )rrr rs rL transformz_BaseKMeans.transformds+"   ! !! $q!!rNc.t||jS)z.Guts of transform method; no input validation.)rrrs rLr z_BaseKMeans._transformzs"1d&;&;<`. 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 `. 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)rrrrzr{r|r}rNT)rrrrvrr1rrc Nt ||||||||||_| |_y)Nr)rrrr) rr3rrrrvrr1rrrs rLrzKMeans.__init__as< !%   "rNct||d|j|_|jdk(r2|jdk(r"t j dtd|_yyy)N rr!r zQalgorithm='elkan' doesn't make sense for a single cluster. Using 'lloyd' instead.r~)rrr _algorithmr3rrrrr2rs rLrzKMeans._check_params_vs_input{s^ &q&<.. ??g %$//Q*> MM6  &DO+? %rNc6tjd|dy)(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)rrrs rLrzKMeans._warn_mkl_vcomps"  )*:(:! = rNr7c nt||dtjtjgd|jd}|j |t |j}t|||j}t|_ |j}t|}|r+t||jdd}|j||t!j"|s|j%d }||z}|r||z}t'|d }|j(d k(rt*} n%t,} |j/||j0dd \} } t3|j4D]} |j7||||| } |j8r t;d| ||| |j<|j8|j>|j\}}}}| || ksttA|| |jBr|} |}|} |}t!j"|s|js|z }z }tEtG| }||jBkr6tIjJdjM||jBtNd|_(|jPj0d|_)| |_*| |_+|_,|S)aCompute 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:rF)r;r<rrrr=TrrrrrAr!r)r5rr1r4zInitialization complete)rrrvrzkNumber of distinct clusters ({}) found smaller than n_clusters ({}). Possibly due to duplicate points in X.rr)-rrDrErFrrrr1rr<rrrrrrr[r\rurr&rrrrGr_rrrrrrr$r3lensetrrformatr r_n_features_outrrr)rr2rr4r1rinit_is_array_likeX_meanr5 kmeans_single best_inertia best_labelsrrrrrJr best_centers best_n_iterdistinct_clusterss rLrz KMeans.fits8   ::rzz* %  ##A&)$*;*;< ,]AQWWM 57yy5d; t177SID  ' '4 0{{1~VVV^F KA!$At4 ??g %0M0M  ! !!QWWQZ 0$.! kt||$# &A// /)+ 0L||/01> II//1 -FGWg#,&+FKQ$ & & % G# &J{{1~;;V  F "LK 01 t . MM0$//B"  !-#44::1=" $ "  rNr)rrrrrrrrrrrrr rrrs@rLrrsvp$  , ,$+ '7!345$D# #4 & 56rNrc Jt|||| \} } tj|rt|||||| | nt |||||| | |rU|dkDrO|||j zk} | j d|jdzkDr6tj|td|jdzd} d| | <| j }|r|j|jdd|}|rtd|dtj|rct||jtjd tj | djtjd |n|||| <tj"|| || <| S) asIncremental 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_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. rrg?NF)rrSz[MiniBatchKMeans] Reassigning z cluster centers.)r)rr[r\r,r+maxrYrGrDargsortrVrXrrastypeintpwheremin)r2r4rJr weight_sumsr1random_reassignreassignment_ratiorrrr to_reassignindices_dont_reassign n_reassigns new_centerss rL_mini_batch_steprGsF&a9UOFG {{1~ }g{K        -1!$69J$JJ  ?? sQWWQZ/ /$&JJ{$;Caggaj@P K( $&66+{l*C#D K NrNc 2eZdZUdZiej eedddgdgeeddddgeeddddgeedddgdZe e d < dd d d dd ddddddd fd Z fdZ dZ dZdZed ddZed ddZxZS)MiniBatchKMeansa Mini-Batch K-Means clustering. Read more in the :ref:`User Guide `. 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 `. 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/r r) batch_sizecompute_labelsmax_no_improvementrrBrrzdiTr$r{{Gz?) rrrJrrKr1rvrLrrrBc xt |||||||| | |_||_||_| |_| |_y)N)r3rrrr1rvr)rrrLrJrKrrB)rr3rrrJrrKr1rvrLrrrBrs rLrzMiniBatchKMeans.__init__bsT !%  #5$,""4rNct||dt|j|jd|_|j |_|jBd|j z|_|j|jkryd|jz|_nd|j|jkrKtjd|jd|jdtdd|jz|_t|j|jd|_|jdkrtd |jd y) Nr%rz init_size=z" should be larger than n_clusters=z,. Setting it to min(3*n_clusters, n_samples)rrz'reassignment_ratio should be >= 0, got z instead.)rrr?rJrG _batch_sizer _init_sizer3rrrrBrHr's rLrz&MiniBatchKMeans._check_params_vs_inputs& &q&;t ;.. ?? "$"2"22DO0"#doo"5 __t . MM  12""&//!2333  $//1DOdooqwwqz:  " "Q &9**+96  'rNc\tjd|jtzd|y)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)rrrr!rs rLrzMiniBatchKMeans._warn_mkl_vcomps4  "__z9:;/0  2 rNc ||jz}|dz}|dk(r!|jrtd|d|d|y|j||_n>|jdz|dzz }t |d}|jd|z z||zz|_|jr!td|d|d|d|j|j dkDr-||j kr|jrtd |d|y |j |j|j krd |_|j|_n|xjdz c_|j7|j|jk\r|jrtd |d|y y) z7Helper function to encapsulate the early stopping logicr zMinibatch step /z: mean batch inertia: Fg@z, ewa inertia: rNz)Converged (small centers change) at step Trz3Converged (lack of improvement in inertia) at step ) rSrr _ewa_inertiar?r_ewa_inertia_min_no_improvementrL)rstepn_stepsrfcenters_squared_diff batch_inertiaalphas rL_mini_batch_convergencez'MiniBatchKMeans._mini_batch_convergences ))) ax 19||%dV1WI6 -0    $ -D $$s*i!m|z|_4|S#1swYxYw)aCompute 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:rF)r;r<rrr=TrrArNzInit r rWz with method r5rr1rr4rzInertia for init z: rr) r2r4rJrr@r1rArBrrrNr)5rrDrErFrrr1rr<rrrGrrrrrrSrrrTr_rrrrr empty_likerr3rcrXrYrZrbrrlimitrGrerBrrYr`rr.n_steps_rVrrrKrr)rr2rr4r1rfrgrr5validation_indicesX_validsample_weight_validr2init_idxcluster_centersrr init_centersrJrr\rminibatch_indicesr^r]s rLrzMiniBatchKMeans.fits8   ::rzz* %   ##A&)$*;*;< ,]AQWWM 57 ! :yy #D )t177SID  ' '4 0 a!1!12$At4*11!YP&'+,>? dll+ 'H||hl^1T\\N-vNO#22 /)//+ 3O:#// JAw||)(Q,qb RS#w'=. & 7 ':mmG, xxqww? ! $ '(#==9,1A1AA ' ) / /q6 / J 7^ $0$8$8ItGWGW$X!!1)*"/0A"B# + $ !-$($9$9$;'+'>'> LL"oo ! 99s?+-66;3HQ2N+O(+,('2G//w +?;  B!(#44::1=A 277QUd.>.>$>)#KLM   *J%%// + 'DL$- !-- 9DM c  s7CO?;O??Pc t|d}t||dtjtjgdd| }t |dt |j|_t|||j}t |dd |_ t|d }|s|j|t|_|j }t#|r+t%||jd d }|j'|||j)||j*d |j-||||j|j.| |_tj2|j4|j|_d |_t;j=dd5t?|||j0|j0|j6|j|jA|jB|jD|j ddd|jFr0tI|||j0|j\|_%|_&|xjdz c_ |j0j*d |_'|S#1swYzxYw)aUpdate 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. rr:rF)r;r<rrr _random_stater=rjrTrArrgr rr) r4rJrr@r1rArBrrNr)(hasattrrrDrErFgetattrrr1rsrr<rjrrrrrrrrrrGrrTrrr3rcrbrrirGrerBrrKrrrr.)rr2rr4 has_centersr5rs rL partial_fitzMiniBatchKMeans.partial_fits94d$67   ::rzz* %!/ % /#5d6G6G#H -]AQWWM j!4 $At4  ' ' *9;DO99D'-"4qwwTM++At4  ! !!QWWQZ 0%)$8$8 /!////+ %9%D !88DOO177CDL+,D ' ' ) / /q6 / J  +-- 11 LL!// $ 5 5 7#'#:#: //     *J%%// + 'DL$-  #44::1= 3  s A*JJ r7r)rrrrrrrrrrrrrrr`rer rrwrrs@rLrIrIsHT$  , ,$!T&AB$+'!T&I4PxD@$G'afEF $D5 5@>  ?B 5Z6Zx5d6drNrIr)r|Fr}r )r T)FrOFr )MrrabcrrnumbersrrnumpyrD scipy.sparserr[baserr r r r exceptionsr metrics.pairwiserrutilsrrutils._openmp_helpersrutils._param_validationrrr utils.extmathrrutils.parallelrrutils.sparsefuncsrutils.sparsefuncs_fastrutils.validationrrrr_k_means_commonr!r"r#r$_k_means_elkanr%r&r'r(_k_means_lloydr)r*_k_means_minibatchr+r,rMrIrxboolrrrrrrrrGrIrrNrLrs #",H3?KK434   PQ O ,!T&AB&-($/'(#HafEtL #' g gVQUbR$O ,&- #(       [?[?D  b+N"V<   @+=@+FCN$#C v$$ `#%5|]TW`F p[pt  vrq kq rN