
    -i2                         S r SSKrSSKJr  SSKrSSKJr  SSK	J
r
JrJr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SSK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!   " S S\\\
5      r"g)z!
Nearest Centroid Classification
    N)Real)sparse   )BaseEstimatorClassifierMixin_fit_context)#DiscriminantAnalysisPredictionMixin)pairwise_distancespairwise_distances_argmin)LabelEncoder)get_tags)available_if)Interval
StrOptions)check_classification_targets)csc_median_axis_0)check_is_fittedvalidate_datac                   R  ^  \ rS rSr% Sr\" SS15      /\" \SSSS9S/S	\" S
S15      /S.r\	\
S'    SSSS.S jjr\" SS9S 5       rU 4S jrS rS r\" \5      " \R&                  5      r\" \5      " \R(                  5      r\" \5      " \R*                  5      rU 4S jrSrU =r$ )NearestCentroid   av  Nearest centroid classifier.

Each class is represented by its centroid, with test samples classified to
the class with the nearest centroid.

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

Parameters
----------
metric : {"euclidean", "manhattan"}, default="euclidean"
    Metric to use for distance computation.

    If `metric="euclidean"`, the centroid for the samples corresponding to each
    class is the arithmetic mean, which minimizes the sum of squared L1 distances.
    If `metric="manhattan"`, the centroid is the feature-wise median, which
    minimizes the sum of L1 distances.

    .. versionchanged:: 1.5
        All metrics but `"euclidean"` and `"manhattan"` were deprecated and
        now raise an error.

    .. versionchanged:: 0.19
        `metric='precomputed'` was deprecated and now raises an error

shrink_threshold : float, default=None
    Threshold for shrinking centroids to remove features.

priors : {"uniform", "empirical"} or array-like of shape (n_classes,),         default="uniform"
    The class prior probabilities. By default, the class proportions are
    inferred from the training data.

    .. versionadded:: 1.6

Attributes
----------
centroids_ : array-like of shape (n_classes, n_features)
    Centroid of each class.

classes_ : array of shape (n_classes,)
    The unique classes labels.

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

deviations_ : ndarray of shape (n_classes, n_features)
    Deviations (or shrinkages) of the centroids of each class from the
    overall centroid. Equal to eq. (18.4) if `shrink_threshold=None`,
    else (18.5) p. 653 of [2]. Can be used to identify features used
    for classification.

    .. versionadded:: 1.6

within_class_std_dev_ : ndarray of shape (n_features,)
    Pooled or within-class standard deviation of input data.

    .. versionadded:: 1.6

class_prior_ : ndarray of shape (n_classes,)
    The class prior probabilities.

    .. versionadded:: 1.6

See Also
--------
KNeighborsClassifier : Nearest neighbors classifier.

Notes
-----
When used for text classification with tf-idf vectors, this classifier is
also known as the Rocchio classifier.

References
----------
[1] Tibshirani, R., Hastie, T., Narasimhan, B., & Chu, G. (2002). Diagnosis of
multiple cancer types by shrunken centroids of gene expression. Proceedings
of the National Academy of Sciences of the United States of America,
99(10), 6567-6572. The National Academy of Sciences.

[2] Hastie, T., Tibshirani, R., Friedman, J. (2009). The Elements of Statistical
Learning Data Mining, Inference, and Prediction. 2nd Edition. New York, Springer.

Examples
--------
>>> from sklearn.neighbors import NearestCentroid
>>> import numpy as np
>>> X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
>>> y = np.array([1, 1, 1, 2, 2, 2])
>>> clf = NearestCentroid()
>>> clf.fit(X, y)
NearestCentroid()
>>> print(clf.predict([[-0.8, -1]]))
[1]
	manhattan	euclideanr   Nneither)closedz
array-like	empiricaluniformmetricshrink_thresholdpriors_parameter_constraints)r    r!   c                (    Xl         X l        X0l        g )Nr   )selfr   r    r!   s       V/var/www/html/venv/lib/python3.13/site-packages/sklearn/neighbors/_nearest_centroid.py__init__NearestCentroid.__init__   s      0    T)prefer_skip_nested_validationc                    U R                   S:X  a  [        XUS/S9u  pO9[        U 5      R                  R                  (       a  SOSn[        U UUUSS/S9u  p[
        R                  " U5      n[        U5        UR                  u  pV[        5       nUR                  U5      nUR                  =U l        n	U	R                  n
U
S:  a  [        S	U
-  5      eU R                  S
:X  aH  [        R                   " USS9u  p[        R"                  " U5      [%        ['        U5      5      -  U l        OXU R                  S:X  a#  [        R*                  " SU
-  /U
-  5      U l        O%[        R*                  " U R                  5      U l        U R(                  S:  R-                  5       (       a  [        S5      e[        R.                  " U R(                  R1                  5       S5      (       dG  [2        R4                  " S[6        5        U R(                  U R(                  R1                  5       -  U l        [        R8                  " X4[        R:                  S9U l        [        R>                  " U
5      n[A        U
5       H  nX:H  n[        R0                  " U5      X'   U(       a  [        RB                  " U5      S   nU R                   S:X  aI  U(       d&  [        RD                  " X   SS9U R<                  U'   M|  [G        X   5      U R<                  U'   M  X   RI                  SS9U R<                  U'   M     [        RJ                  " XR<                  U   -
  SS9S-  n[        RJ                  " [        RL                  " UR1                  SS9XZ-
  -  5      SS9U l'        [-        U RN                  S:H  5      (       a  [2        R4                  " S5        SnU(       aT  [        RP                  " URS                  SS9URU                  SS9-
  RW                  5       S:H  5      (       a  [        U5      eU(       d<  [        RP                  " [        RX                  " USS9S:H  5      (       a  [        U5      eURI                  SS9n[        RL                  " SU-  SU-  -
  5      nU RN                  [        RD                  " U RN                  5      -   nUR[                  ['        U5      S5      nUU-  n[        RJ                  " U R<                  U-
  U-  SS9U l.        U R^                  (       a  [        R`                  " U R\                  5      n[        Rb                  " U R\                  5      U R^                  -
  U l.        [        Rd                  " U R\                  SSU R\                  S9  U =R\                  U-  sl.        UU R\                  -  n[        RJ                  " UU-   SS9U l        U $ )a  
Fit the NearestCentroid model according to the given training data.

Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
    Training vector, where `n_samples` is the number of samples and
    `n_features` is the number of features.
    Note that centroid shrinking cannot be used with sparse matrices.
y : array-like of shape (n_samples,)
    Target values.

Returns
-------
self : object
    Fitted estimator.
r   csc)accept_sparse	allow-nanTcsr)ensure_all_finiter,   r   z>The number of classes has to be greater than one; got %d classr   )return_inverser      r   zpriors must be non-negativeg      ?zAThe priors do not sum to 1. Normalizing such that it sums to one.dtype)axisF)copyzself.within_class_std_dev_ has at least 1 zero standard deviation.Inputs within the same classes for at least 1 feature are identical.z2All features have zero variance. Division by zero.N)out)3r   r   r   
input_tags	allow_nanspissparser   shaper   fit_transformclasses_size
ValueErrorr!   npuniquebincountfloatlenclass_prior_asarrayanyisclosesumwarningswarnUserWarningemptyfloat64
centroids_zerosrangewheremedianr   meanarraysqrtwithin_class_std_dev_allmaxmintoarrayptpreshapedeviations_r    signabsclip)r$   Xyr/   is_X_sparse	n_samples
n_featuresley_indclasses	n_classes_class_countsnk	cur_classcenter_maskvarianceerr_msgdataset_centroid_msmmmssignsmsds                            r%   fitNearestCentroid.fit   s   * ;;+% !E7CDAq  (~88BB  !"3$enDA kk!n$Q' !	^  #"$++-LL	q=P 
 ;;+% ii$?OA "L 9E#a&M ID[[I% "

A	M?Y+F GD "

4;; 7D!&&((:;;zz$++//1377MMS !% 1 1D4E4E4I4I4K KD ((I#:"**M XXi y)I,KFF;/BM hh{3A6{{k)"131>PQ1RDOOI.1B1>1RDOOI.-.^-@-@a-@-H	* *  88A 66UCqH%'XXGGHLLaL(I,ABC%&
" t))Q.//MMW
 G266155a5=155a5=#@"I"I"Kq"PQQW%%qq(9Q(>!?!?W%%FFFNGGS2X#	/23 &&43M3M)NNYYs1vq!!V88__00B6U

   GGD,,-E!vvd&6&67$:O:OODGGD$$a43C3CD%t'''C hh'83'>UKDOr(   c                   > [        U 5        [        R                  " U R                  S[	        U R
                  5      -  5      R                  5       (       a`  [        U 5      R                  R                  (       a  SOSn[        U UUSSS9nU R
                  [        XR                  U R                  S9   $ [        TU ]=  U5      $ )a0  Perform classification on an array of test vectors `X`.

The predicted class `C` for each sample in `X` is returned.

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

Returns
-------
y_pred : ndarray of shape (n_samples,)
    The predicted classes.
r1   r-   Tr.   F)r/   r,   resetr   )r   r@   rH   rE   rD   r=   rX   r   r7   r8   r   r   rO   r   superpredict)r$   rb   r/   	__class__s      r%   r   NearestCentroid.predict  s     	::d''S-?)?@DDFF  (~88BB  "3#A ==)!__T[[Q  7?1%%r(   c           	         [        U S5        [        XSSS[        R                  S9n[        R                  " UR
                  S   U R                  R                  4[        R                  S9nU R                  S:g  nUS S 2U4==   U R                  U   -  ss'   U R                  R                  5       nUS S 2U4==   U R                  U   -  ss'   [        U R                  R                  5       Hs  n[        X%U/   U R                  S9R                  5       nUS	-  n[        R                  " U* S
[        R                   " U R"                  U   5      -  -   5      US S 2U4'   Mu     U$ )NrO   TFr.   )r5   r|   r,   r3   r   r2   r}   r   g       @)r   r   r@   rN   rM   r;   r=   r>   rW   rO   r5   rQ   r
   r   ravelsqueezelogrE   )r$   rb   X_normalizeddiscriminant_scoremaskcentroids_normalized	class_idx	distancess           r%   _decision_function"NearestCentroid._decision_function8  sG   l+$$e5


  XX"DMM$6$67rzz
 ))Q.QW!;!;D!AA#335QW%)C)CD)II%t}}112I*I;?eg  !OI/1zz
S266$*;*;I*F#GGG0q)|, 3 "!r(   c                      U R                   S:H  $ )Nr   r}   )r$   s    r%   _check_euclidean_metric'NearestCentroid._check_euclidean_metricT  s    {{k))r(   c                    > [         TU ]  5       nU R                  S:H  UR                  l        SUR                  l        U$ )Nnan_euclideanT)r~   __sklearn_tags__r   r7   r8   r   )r$   tagsr   s     r%   r    NearestCentroid.__sklearn_tags__c  s6    w')$(KK?$B!!%r(   )rO   rE   r=   r^   r   r!   r    rW   )r   )__name__
__module____qualname____firstlineno____doc__r   r   r   r"   dict__annotations__r&   r   ry   r   r   r   r   r	   decision_functionpredict_probapredict_log_probar   __static_attributes____classcell__)r   s   @r%   r   r      s    eP {K89:%dAtIFM[),D!EF$D  	 	 5{ 6{z &D"8* %%<=+== !!89+99M %%<=+== r(   r   )#r   rJ   numbersr   numpyr@   scipyr   r9   baser   r   r   discriminant_analysisr	   metrics.pairwiser
   r   preprocessingr   utilsr   utils._available_ifr   utils._param_validationr   r   utils.multiclassr   utils.sparsefuncsr   utils.validationr   r   r    r(   r%   <module>r      sP        ? ? G )  . : ; 1 =J'-Jr(   