`L i{B dZddlZddlmZddlZddlmZddlm Z ddl m Z m Z m Z ddlmZdd lmZmZmZd d lmZmZmZd Zed dgd geeedhzegeeddddgdgdddddddZdZed dgd geeedhzegdddddZed gd gdddZed gd gddd Zy)!z Unsupervised evaluation metrics.N)Integral)issparse) LabelEncoder)_safe_indexingcheck_random_state check_X_y)_atol_for_type)Interval StrOptionsvalidate_params)_VALID_METRICSpairwise_distancespairwise_distances_chunkedc:d|cxkr|ksntd|zy)zCheck that number of labels are valid. Parameters ---------- n_labels : int Number of labels. n_samples : int Number of samples. zGNumber of labels is %d. Valid values are 2 to n_samples - 1 (inclusive)N) ValueError)n_labels n_sampless k/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/metrics/cluster/_unsupervised.pycheck_number_of_labelsrs. x #) # U    $z array-likez sparse matrix precomputedrleft)closed random_state)Xlabelsmetric sample_sizerT)prefer_skip_nested_validation euclidean)r r!rc <|pt||ddg\}}t|}|j|jdd|}|dk(r"||j|j||}}n ||||}}t t jt||fd|i|S)a Compute the mean Silhouette Coefficient of all samples. The Silhouette Coefficient is calculated using the mean intra-cluster distance (``a``) and the mean nearest-cluster distance (``b``) for each sample. The Silhouette Coefficient for a sample is ``(b - a) / max(a, b)``. To clarify, ``b`` is the distance between a sample and the nearest cluster that the sample is not a part of. Note that Silhouette Coefficient is only defined if number of labels is ``2 <= n_labels <= n_samples - 1``. This function returns the mean Silhouette Coefficient over all samples. To obtain the values for each sample, use :func:`silhouette_samples`. The best value is 1 and the worst value is -1. Values near 0 indicate overlapping clusters. Negative values generally indicate that a sample has been assigned to the wrong cluster, as a different cluster is more similar. Read more in the :ref:`User Guide `. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples_a, n_samples_a) if metric == "precomputed" or (n_samples_a, n_features) otherwise An array of pairwise distances between samples, or a feature array. labels : array-like of shape (n_samples,) Predicted labels for each sample. metric : str or callable, default='euclidean' The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by :func:`~sklearn.metrics.pairwise_distances`. If ``X`` is the distance array itself, use ``metric="precomputed"``. sample_size : int, default=None The size of the sample to use when computing the Silhouette Coefficient on a random subset of the data. If ``sample_size is None``, no sampling is used. random_state : int, RandomState instance or None, default=None Determines random number generation for selecting a subset of samples. Used when ``sample_size is not None``. Pass an int for reproducible results across multiple function calls. See :term:`Glossary `. **kwds : optional keyword parameters Any further parameters are passed directly to the distance function. If using a scipy.spatial.distance metric, the parameters are still metric dependent. See the scipy docs for usage examples. Returns ------- silhouette : float Mean Silhouette Coefficient for all samples. References ---------- .. [1] `Peter J. Rousseeuw (1987). "Silhouettes: a Graphical Aid to the Interpretation and Validation of Cluster Analysis". Computational and Applied Mathematics 20: 53-65. `_ .. [2] `Wikipedia entry on the Silhouette Coefficient `_ Examples -------- >>> from sklearn.datasets import make_blobs >>> from sklearn.cluster import KMeans >>> from sklearn.metrics import silhouette_score >>> X, y = make_blobs(random_state=42) >>> kmeans = KMeans(n_clusters=2, random_state=42) >>> silhouette_score(X, kmeans.fit_predict(X)) 0.49... Ncsccsr accept_sparserrr ) r r permutationshapeTfloatnpmeansilhouette_samples)rrr r!rkwdsindicess rsilhouette_scorer2)srau~F 6),7 **1771:6| D ] "' W-//vA' F7OvA +AvMfMMN OOrc  |jd}tj|t|f|j}t |r|j dk7r tdt|D]}|j}|j||||dz}|j||||dz} tj||} ||xxtj| | t|z cc<nCt|D]5}||} |} ||xxtj| | t|z cc<7||z} tj|||| f} || } tj|| <||z}|j!d}| |fS)aAccumulate silhouette statistics for vertical chunk of X. Parameters ---------- D_chunk : {array-like, sparse matrix} of shape (n_chunk_samples, n_samples) Precomputed distances for a chunk. If a sparse matrix is provided, only CSR format is accepted. start : int First index in the chunk. labels : array-like of shape (n_samples,) Corresponding cluster labels, encoded as {0, ..., n_clusters-1}. label_freqs : array-like Distribution of cluster labels in ``labels``. rdtyper&z=Expected CSR matrix. Please pass sparse matrix in CSR format.r)weights minlengthaxis)r*r-zeroslenr5rformat TypeErrorrangeindptrr1datatakebincountarangeinfmin)D_chunkstartr label_freqsn_chunk_samplescluster_distancesir?r1sample_weights sample_labelsend intra_indexintra_cluster_distancesinter_cluster_distancess r_silhouette_reducerRsmmA&O #k*+7== >>U "O ' A^^FoofQi&Q-@G$\\&)fQUmDNGGFG4M a BKK~[AQ%   ' A$QZN"M a BKK~[AQ%    / !C99_-veC/@AK/ <%'VVk"$/333; "$; ;;r)rrr )r c 0t||dg\}}|dk(rtd}|jjdk(rPt |j}t j t j|j|kDr*|t j |jdk7r|t}|j|}t|}t j|}tt|j|||d<tj t"||} t%t'|fd | i|} | \} } t j(| } t j(| } |d z j+|d } t j,d d 5| | z} ddd| | z }t j,d d 5|t j.| | z}dddt j0|S#1swY\xYw#1swY*xYw)a Compute the Silhouette Coefficient for each sample. The Silhouette Coefficient is a measure of how well samples are clustered with samples that are similar to themselves. Clustering models with a high Silhouette Coefficient are said to be dense, where samples in the same cluster are similar to each other, and well separated, where samples in different clusters are not very similar to each other. The Silhouette Coefficient is calculated using the mean intra-cluster distance (``a``) and the mean nearest-cluster distance (``b``) for each sample. The Silhouette Coefficient for a sample is ``(b - a) / max(a, b)``. Note that Silhouette Coefficient is only defined if number of labels is 2 ``<= n_labels <= n_samples - 1``. This function returns the Silhouette Coefficient for each sample. The best value is 1 and the worst value is -1. Values near 0 indicate overlapping clusters. Read more in the :ref:`User Guide `. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples_a, n_samples_a) if metric == "precomputed" or (n_samples_a, n_features) otherwise An array of pairwise distances between samples, or a feature array. If a sparse matrix is provided, CSR format should be favoured avoiding an additional copy. labels : array-like of shape (n_samples,) Label values for each sample. metric : str or callable, default='euclidean' The metric to use when calculating distance between instances in a feature array. If metric is a string, it must be one of the options allowed by :func:`~sklearn.metrics.pairwise_distances`. If ``X`` is the distance array itself, use "precomputed" as the metric. Precomputed distance matrices must have 0 along the diagonal. **kwds : optional keyword parameters Any further parameters are passed directly to the distance function. If using a ``scipy.spatial.distance`` metric, the parameters are still metric dependent. See the scipy docs for usage examples. Returns ------- silhouette : array-like of shape (n_samples,) Silhouette Coefficients for each sample. References ---------- .. [1] `Peter J. Rousseeuw (1987). "Silhouettes: a Graphical Aid to the Interpretation and Validation of Cluster Analysis". Computational and Applied Mathematics 20: 53-65. `_ .. [2] `Wikipedia entry on the Silhouette Coefficient `_ Examples -------- >>> from sklearn.metrics import silhouette_samples >>> from sklearn.datasets import make_blobs >>> from sklearn.cluster import KMeans >>> X, y = make_blobs(n_samples=50, random_state=42) >>> kmeans = KMeans(n_clusters=3, random_state=42) >>> labels = kmeans.fit_predict(X) >>> silhouette_samples(X, labels) array([...]) r&r'rzgThe precomputed distance matrix contains non-zero elements on the diagonal. Use np.fill_diagonal(X, 0).frr )rrH reduce_funcrclip)modeignore)divideinvalidN)r rr5kindr r-anyabsdiagonalr fit_transformr;rBrclasses_ functoolspartialrRzipr concatenaterAerrstatemaximum nan_to_num)rrr r0 error_msgatollerrHrUresultsintra_clust_distsinter_clust_distsdenom sil_sampless rr/r/sb!VE7;IAv D  77<<3 !!''*DvvbffQZZ\*T12 VVAJJLA% &O B   f %FF I++f%K3r{{+Y7DN##6{K-aQ[QDQRG+2(('89'89 1_ " "6 " 7E Hh 7#U"#$&77K Hh 7Hrzz"35FGG H == %%##HHsH H H  H)rrc&t||\}}t}|j|}|j\}}t |j }t ||d\}}tj|d}t|D]i} ||| k(} tj| d} |t | tj| |z dzzz }|tj| | z dzz }kt|dk(rdS|||z z||dz zz S)aCompute the Calinski and Harabasz score. It is also known as the Variance Ratio Criterion. The score is defined as ratio of the sum of between-cluster dispersion and of within-cluster dispersion. Read more in the :ref:`User Guide `. Parameters ---------- X : array-like of shape (n_samples, n_features) A list of ``n_features``-dimensional data points. Each row corresponds to a single data point. labels : array-like of shape (n_samples,) Predicted labels for each sample. Returns ------- score : float The resulting Calinski-Harabasz score. References ---------- .. [1] `T. Calinski and J. Harabasz, 1974. "A dendrite method for cluster analysis". Communications in Statistics `_ Examples -------- >>> from sklearn.datasets import make_blobs >>> from sklearn.cluster import KMeans >>> from sklearn.metrics import calinski_harabasz_score >>> X, _ = make_blobs(random_state=0) >>> kmeans = KMeans(n_clusters=3, random_state=0,).fit(X) >>> calinski_harabasz_score(X, kmeans.labels_) 114.8... )rqrr8rrqg?) r rr_r*r;r`rr-r.r>sumr,) rrrjr_r extra_disp intra_dispr.k cluster_kmean_ks rcalinski_harabasz_scorery>s ^!V$IAv B   f %F77LIq2;;H8Y/%J 7711 D 8_8fkN +c)nrvvv}.B'CCC bffi&0Q677 8     9x/ 0J(S.4Q R rct||\}}t}|j|}|j\}}t |j }t ||tj|}tj|t |dft}t|D]K}t|||k(} | jd} | ||<tjt| | g||<Mt|} tj|dstj| drytj | | dk(<|dddf|z} tj"| | z d} ttj| S)aMCompute the Davies-Bouldin score. The score is defined as the average similarity measure of each cluster with its most similar cluster, where similarity is the ratio of within-cluster distances to between-cluster distances. Thus, clusters which are farther apart and less dispersed will result in a better score. The minimum score is zero, with lower values indicating better clustering. Read more in the :ref:`User Guide `. .. versionadded:: 0.20 Parameters ---------- X : array-like of shape (n_samples, n_features) A list of ``n_features``-dimensional data points. Each row corresponds to a single data point. labels : array-like of shape (n_samples,) Predicted labels for each sample. Returns ------- score: float The resulting Davies-Bouldin score. References ---------- .. [1] Davies, David L.; Bouldin, Donald W. (1979). `"A Cluster Separation Measure" `__. IEEE Transactions on Pattern Analysis and Machine Intelligence. PAMI-1 (2): 224-227 Examples -------- >>> from sklearn.metrics import davies_bouldin_score >>> X = [[0, 1], [1, 1], [3, 4]] >>> labels = [0, 0, 1] >>> davies_bouldin_score(X, labels) 0.12... rr4r8rqNr)r rr_r*r;r`rr-r:r,r>rr.averagerallcloserDmax)rrrjrrsr intra_dists centroidsrvrwcentroidcentroid_distancescombined_intra_distsscoress rdavies_bouldin_scorersUf!V$IAv B   f %F77LIq2;;H8Y/((8$K(C!I.e>q>) ! $6y8*$MN A O ,I6 {{;"bkk2Da&H24&&)Q./&q$w/+= VV(+==A FF  !!r) __doc__ranumbersrnumpyr- scipy.sparser preprocessingrutilsrrr utils._array_apir utils._param_validationr r r pairwiserrrrsetcallabler2rRr/ryrrrrs^& !)BB. VU $O ,.c.1]OCDhO 1d6BDI'( #' %$TWP WPt3