§
    ¨éUg   ã                   ó¨   — d Z ddlmZ ddlZddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZ ddlmZ  G d„ de
e	¦  «        ZdS )z!
Nearest Centroid Classification
é    )ÚRealN)Úsparseé   )ÚBaseEstimatorÚClassifierMixinÚ_fit_context)Úpairwise_distances_argmin)ÚLabelEncoder)ÚIntervalÚ
StrOptions)Úcheck_classification_targets)Úcsc_median_axis_0)Úcheck_is_fittedc                   óœ   — e Zd ZU dZ eddh¦  «        g eeddd¬¦  «        dgdœZee	d	<   ddd
œd„Z
 ed¬¦  «        d„ ¦   «         Zd„ ZdS )ÚNearestCentroidaõ	  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.

    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

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

    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]

    For a more detailed example see:
    :ref:`sphx_glr_auto_examples_neighbors_plot_nearest_centroid.py`
    Ú	manhattanÚ	euclideanr   NÚneither)Úclosed©ÚmetricÚshrink_thresholdÚ_parameter_constraints)r   c                ó"   — || _         || _        d S )Nr   )Úselfr   r   s      úb/var/www/surfInsights/venv3-11/lib/python3.11/site-packages/sklearn/neighbors/_nearest_centroid.pyÚ__init__zNearestCentroid.__init__l   s   € ØˆŒØ 0ˆÔÐÐó    T)Úprefer_skip_nested_validationc                 ó˜  — | j         dk    r|                      ||dg¬¦  «        \  }}n|                      ||ddg¬¦  «        \  }}t          j        |¦  «        }|r| j        rt          d¦  «        ‚t          |¦  «         |j        \  }}t          ¦   «         }| 	                    |¦  «        }|j
        x| _
        }|j        }	|	dk     rt          d|	z  ¦  «        ‚t          j        |	|ft          j        ¬¦  «        | _        t          j        |	¦  «        }
t#          |	¦  «        D ]¯}||k    }t          j        |¦  «        |
|<   |rt          j        |¦  «        d	         }| j         dk    rE|s%t          j        ||         d	¬
¦  «        | j        |<   Œmt+          ||         ¦  «        | j        |<   Œ‹||                              d	¬
¦  «        | j        |<   Œ°| j        �rmt          j        t          j        |d	¬
¦  «        d	k    ¦  «        rt          d¦  «        ‚t          j        |d	¬
¦  «        }t          j        d|
z  d|z  z
  ¦  «        }|| j        |         z
  dz  }|                     d	¬
¦  «        }t          j        |||	z
  z  ¦  «        }|t          j        |¦  «        z  }|                     t7          |¦  «        d¦  «        }||z  }| j        |z
  |z  }t          j        |¦  «        }t          j        |¦  «        | j        z
  }t          j        |d	d|¬¦  «         ||z  }||z  }|t          j        dd…f         |z   | _        | S )a0  
        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Úcsrz2threshold shrinking not supported for sparse inputr   z>The number of classes has to be greater than one; got %d class)Údtyper   )Úaxisz2All features have zero variance. Division by zero.g      ð?é   N)Úout) r   Ú_validate_dataÚspÚissparser   Ú
ValueErrorr   Úshaper
   Úfit_transformÚclasses_ÚsizeÚnpÚemptyÚfloat64Ú
centroids_ÚzerosÚrangeÚsumÚwhereÚmedianr   ÚmeanÚallÚptpÚsqrtÚreshapeÚlenÚsignÚabsÚclipÚnewaxis)r   ÚXÚyÚis_X_sparseÚ	n_samplesÚ
n_featuresÚleÚy_indÚclassesÚ	n_classesÚnkÚ	cur_classÚcenter_maskÚdataset_centroid_ÚmÚvarianceÚsÚmmÚmsÚ	deviationÚsignsÚmsds                         r   ÚfitzNearestCentroid.fitp   s_  € ð* Œ;˜+Ò%Ð%Ø×&Ò& q¨!¸E¸7Ð&ÑCÔC‰DˆAˆqˆqà×&Ò& q¨!¸EÀ5¸>Ð&ÑJÔJ‰DˆAˆqÝ”k !‘n”nˆØð 	S˜4Ô0ð 	SÝÐQÑRÔRÐRÝ$ QÑ'Ô'Ð'à !¤Ñˆ	�:Ý‰^Œ^ˆØ× Ò  Ñ#Ô#ˆØ"$¤+Ð-ˆŒ˜Ø”Lˆ	Ø�qŠ=ˆ=ÝØPØññô ð õ œ( I¨zÐ#:Å"Ä*ÐMÑMÔMˆŒåŒX�iÑ Ô ˆå˜yÑ)Ô)ð 	Ið 	IˆIØ 9Ò,ˆKÝœF ;Ñ/Ô/ˆBˆy‰MØð 7Ý œh {Ñ3Ô3°AÔ6�àŒ{˜kÒ)Ð)à"ð SÝ13´¸1¸[¼>ÐPQÐ1RÑ1RÔ1R�D”O IÑ.Ð.å1BÀ1À[Ä>Ñ1RÔ1R�D”O IÑ.Ð.à-.¨{¬^×-@Ò-@ÀaÐ-@Ñ-HÔ-H�” 	Ñ*Ð*àÔ ñ 	EÝŒv•b”f˜Q QÐ'Ñ'Ô'¨1Ò,Ñ-Ô-ð WÝ Ð!UÑVÔVÐVÝ "¤¨°Ð 2Ñ 2Ô 2Ðõ ”˜˜r™ c¨I¡oÑ6Ñ7Ô7ˆAà˜DœO¨EÔ2Ñ2°qÑ8ˆHØ—|’|¨�|Ñ+Ô+ˆHÝ”˜ I°	Ñ$9Ñ:Ñ;Ô;ˆAØ•”˜1‘”ÑˆAØ—’�3˜q™6œ6 1Ñ%Ô%ˆBØ�a‘ˆBØœÐ+<Ñ<ÀÑBˆIõ ”G˜IÑ&Ô&ˆEÝœ˜yÑ)Ô)¨DÔ,AÑAˆIÝŒG�I˜q $¨IÐ6Ñ6Ô6Ð6Ø˜ÑˆIà�y‘.ˆCØ/µ´
¸A¸A¸A°Ô>ÀÑDˆDŒOØˆr   c                 óž   — t          | ¦  «         |                      |dd¬¦  «        }| j        t          || j        | j        ¬¦  «                 S )a}  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)
            Test samples.

        Returns
        -------
        C : ndarray of shape (n_samples,)
            The predicted classes.
        r#   F)r"   Úreset)r   )r   r(   r.   r	   r3   r   )r   rC   s     r   ÚpredictzNearestCentroid.predictÇ   sQ   € õ 	˜ÑÔÐà×Ò °¸eÐÑDÔDˆØŒ}Ý% a¨¬ÀÄÐMÑMÔMô
ð 	
r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   ÚdictÚ__annotations__r   r   rX   r[   © r   r   r   r      sÏ   € € € € € € ðLð Lð^ �:˜{¨KÐ8Ñ9Ô9Ð:Ø%˜X d¨A¨t¸IÐFÑFÔFÈÐMð$ð $Ð˜Dð ð ñ ð
1¸tð 1ð 1ð 1ð 1ð 1ð €\°Ð5Ñ5Ô5ðTð Tñ 6Ô5ðTðl
ð 
ð 
ð 
ð 
r   r   )r_   Únumbersr   Únumpyr0   Úscipyr   r)   Úbaser   r   r   Úmetrics.pairwiser	   Úpreprocessingr
   Úutils._param_validationr   r   Úutils.multiclassr   Úutils.sparsefuncsr   Úutils.validationr   r   rb   r   r   ú<module>rm      s  ððð ð Ð Ð Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð à ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ð ?Ø 8Ð 8Ð 8Ð 8Ð 8Ð 8Ø (Ð (Ð (Ð (Ð (Ð (Ø :Ð :Ð :Ð :Ð :Ð :Ð :Ð :Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø .Ð .Ð .Ð .Ð .Ð .ðC
ð C
ð C
ð C
ð C
�o }ñ C
ô C
ð C
ð C
ð C
r   