
    -iM                         S r SSKrSSKrSSKJrJr  SSKJr  SSKr	SSK
Jr  SSKJrJrJrJr  SSK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!  SSK"J#r#  SSK$J%r%J&r&J'r'   " S S\\\5      r(g)z!
Neighborhood Component Analysis
    N)IntegralReal)warn)minimize   )BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin_fit_context)PCA)ConvergenceWarning)pairwise_distances)LabelEncoder)Interval
StrOptions)softmax)"_get_additional_lbfgs_options_dict)check_classification_targets)check_random_state)check_arraycheck_is_fittedvalidate_datac                     ^  \ rS rSr% Sr\" \SSSS9S/\" 1 Sk5      \R                  /S/\" \SSSS9/\" \
S	SSS9/\S/S
/S/S.r\\S'    SSSSSSS	SS.S jjr\" SS9S 5       rS rS rS rSS jrU 4S jr\S 5       rSrU =r$ ) NeighborhoodComponentsAnalysis"   a  Neighborhood Components Analysis.

Neighborhood Component Analysis (NCA) is a machine learning algorithm for
metric learning. It learns a linear transformation in a supervised fashion
to improve the classification accuracy of a stochastic nearest neighbors
rule in the transformed space.

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

Parameters
----------
n_components : int, default=None
    Preferred dimensionality of the projected space.
    If None it will be set to `n_features`.

init : {'auto', 'pca', 'lda', 'identity', 'random'} or ndarray of shape             (n_features_a, n_features_b), default='auto'
    Initialization of the linear transformation. Possible options are
    `'auto'`, `'pca'`, `'lda'`, `'identity'`, `'random'`, and a numpy
    array of shape `(n_features_a, n_features_b)`.

    - `'auto'`
        Depending on `n_components`, the most reasonable initialization
        is chosen. If `n_components <= min(n_features, n_classes - 1)`
        we use `'lda'`, as it uses labels information. If not, but
        `n_components < min(n_features, n_samples)`, we use `'pca'`, as
        it projects data in meaningful directions (those of higher
        variance). Otherwise, we just use `'identity'`.

    - `'pca'`
        `n_components` principal components of the inputs passed
        to :meth:`fit` will be used to initialize the transformation.
        (See :class:`~sklearn.decomposition.PCA`)

    - `'lda'`
        `min(n_components, n_classes)` most discriminative
        components of the inputs passed to :meth:`fit` will be used to
        initialize the transformation. (If `n_components > n_classes`,
        the rest of the components will be zero.) (See
        :class:`~sklearn.discriminant_analysis.LinearDiscriminantAnalysis`)

    - `'identity'`
        If `n_components` is strictly smaller than the
        dimensionality of the inputs passed to :meth:`fit`, the identity
        matrix will be truncated to the first `n_components` rows.

    - `'random'`
        The initial transformation will be a random array of shape
        `(n_components, n_features)`. Each value is sampled from the
        standard normal distribution.

    - numpy array
        `n_features_b` must match the dimensionality of the inputs passed
        to :meth:`fit` and n_features_a must be less than or equal to that.
        If `n_components` is not `None`, `n_features_a` must match it.

warm_start : bool, default=False
    If `True` and :meth:`fit` has been called before, the solution of the
    previous call to :meth:`fit` is used as the initial linear
    transformation (`n_components` and `init` will be ignored).

max_iter : int, default=50
    Maximum number of iterations in the optimization.

tol : float, default=1e-5
    Convergence tolerance for the optimization.

callback : callable, default=None
    If not `None`, this function is called after every iteration of the
    optimizer, taking as arguments the current solution (flattened
    transformation matrix) and the number of iterations. This might be
    useful in case one wants to examine or store the transformation
    found after each iteration.

verbose : int, default=0
    If 0, no progress messages will be printed.
    If 1, progress messages will be printed to stdout.
    If > 1, progress messages will be printed and the `disp`
    parameter of :func:`scipy.optimize.minimize` will be set to
    `verbose - 2`.

random_state : int or numpy.RandomState, default=None
    A pseudo random number generator object or a seed for it if int. If
    `init='random'`, `random_state` is used to initialize the random
    transformation. If `init='pca'`, `random_state` is passed as an
    argument to PCA when initializing the transformation. Pass an int
    for reproducible results across multiple function calls.
    See :term:`Glossary <random_state>`.

Attributes
----------
components_ : ndarray of shape (n_components, n_features)
    The linear transformation learned during fitting.

n_features_in_ : int
    Number of features seen during :term:`fit`.

    .. versionadded:: 0.24

n_iter_ : int
    Counts the number of iterations performed by the optimizer.

random_state_ : numpy.RandomState
    Pseudo random number generator object used during initialization.

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
--------
sklearn.discriminant_analysis.LinearDiscriminantAnalysis : Linear
    Discriminant Analysis.
sklearn.decomposition.PCA : Principal component analysis (PCA).

References
----------
.. [1] J. Goldberger, G. Hinton, S. Roweis, R. Salakhutdinov.
       "Neighbourhood Components Analysis". Advances in Neural Information
       Processing Systems. 17, 513-520, 2005.
       http://www.cs.nyu.edu/~roweis/papers/ncanips.pdf

.. [2] Wikipedia entry on Neighborhood Components Analysis
       https://en.wikipedia.org/wiki/Neighbourhood_components_analysis

Examples
--------
>>> from sklearn.neighbors import NeighborhoodComponentsAnalysis
>>> from sklearn.neighbors import KNeighborsClassifier
>>> from sklearn.datasets import load_iris
>>> from sklearn.model_selection import train_test_split
>>> X, y = load_iris(return_X_y=True)
>>> X_train, X_test, y_train, y_test = train_test_split(X, y,
... stratify=y, test_size=0.7, random_state=42)
>>> nca = NeighborhoodComponentsAnalysis(random_state=42)
>>> nca.fit(X_train, y_train)
NeighborhoodComponentsAnalysis(...)
>>> knn = KNeighborsClassifier(n_neighbors=3)
>>> knn.fit(X_train, y_train)
KNeighborsClassifier(...)
>>> print(knn.score(X_test, y_test))
0.933333...
>>> knn.fit(nca.transform(X_train), y_train)
KNeighborsClassifier(...)
>>> print(knn.score(nca.transform(X_test), y_test))
0.961904...
   Nleft)closed>   ldapcaautorandomidentitybooleanr   verboserandom_staten_componentsinit
warm_startmax_itertolcallbackr%   r&   _parameter_constraintsr!   F2   gh㈵>)r)   r*   r+   r,   r-   r%   r&   c                d    Xl         X l        X0l        X@l        XPl        X`l        Xpl        Xl        g Nr'   )	selfr(   r)   r*   r+   r,   r-   r%   r&   s	            I/var/www/html/venv/lib/python3.13/site-packages/sklearn/neighbors/_nca.py__init__'NeighborhoodComponentsAnalysis.__init__   s.     )	$  (    T)prefer_skip_nested_validationc                    [        XUSS9u  p[        U5        [        5       R                  U5      nU R                  bF  U R                  UR
                  S   :  a)  [        SU R                   SUR
                  S    S35      eU R                  (       aq  [        U S5      (       a`  U R                  R
                  S   UR
                  S   :w  a6  [        S	UR
                  S    S
U R                  R
                  S    S35      eU R                  n[        U[        R                  5      (       a  [        U5      nUR
                  S   UR
                  S   :w  a,  [        SUR
                  S    SUR
                  S    S35      eUR
                  S   UR
                  S   :  a,  [        SUR
                  S    SUR
                  S    S35      eU R                  bF  U R                  UR
                  S   :w  a)  [        SU R                   SUR
                  S    S35      e[        U R                   5      U l        [$        R$                  " 5       nUSS2[        R&                  4   U[        R&                  SS24   :H  n[        R(                  " U R+                  XU5      5      nU R,                  S:  a  U R,                  S-
  OSnSU R.                  XS4SUU R0                  [3        SSU R4                  0[7        SU5      D6U R8                  S.nSU l        [=        S0 UD6n	U	R>                  RA                  SUR
                  S   5      U l	        [$        R$                  " 5       U-
  nU R,                  (       aj  U RB                  RD                  n
U	RF                  (       d)  [I        SRK                  XRL                  5      [N        5        [Q        SRK                  X5      5        U $ )a  Fit the model according to the given training data.

Parameters
----------
X : array-like of shape (n_samples, n_features)
    The training samples.

y : array-like of shape (n_samples,)
    The corresponding training labels.

Returns
-------
self : object
    Fitted estimator.
r   )ensure_min_samplesNr   zDThe preferred dimensionality of the projected space `n_components` (z8) cannot be greater than the given data dimensionality (z)!components_zThe new inputs dimensionality (zT) does not match the input dimensionality of the previously learned transformation (z).zThe input dimensionality (zc) of the given linear transformation `init` must match the dimensionality of the given inputs `X` (r   zThe output dimensionality (z]) of the given linear transformation `init` cannot be greater than its input dimensionality (zV) does not match the output dimensionality of the given linear transformation `init` (zL-BFGS-Bg      Tmaxiterdisp)methodfunargsjacx0r,   optionsr-   z[{}] NCA did not converge: {}z[{}] Training took {:8.2f}s. ))r   r   r   fit_transformr(   shape
ValueErrorr*   hasattrr:   r)   
isinstancenpndarrayr   r   r&   random_state_timenewaxisravel_initializer%   _loss_grad_lbfgsr,   dictr+   r   	_callbackn_iter_r   xreshape	__class____name__successr   formatmessager   print)r2   Xyr)   t_trainsame_class_masktransformationr=   optimizer_params
opt_resultcls_names              r3   fit"NeighborhoodComponentsAnalysis.fit   s   $ TaA>$Q'N((+ (T->->-K3373D3D2E F##$771:,b2  OOm,,  &&q)QWWQZ71!''!* >66:6F6F6L6LQ6O5PPRT  yydBJJ''t$Dzz!}
* 0A @??@wwqzl"N  zz!}tzz!}, 1$**Q- A>>Bjjm_BP    ,1B1BdjjQRm1S 77;7H7H6I J  $zz!}oR	1  00A0AB ))+ ArzzM*a

A.>> $"2"21">? $(<<!#3t||a ((. 88 4VTB 
 1 01
 &<<//AGGAJ? ))+'<<~~..H %%3:: "4"4 '	 077JKr6   c                     [        U 5        [        XSS9n[        R                  " XR                  R
                  5      $ )aD  Apply the learned transformation to the given data.

Parameters
----------
X : array-like of shape (n_samples, n_features)
    Data samples.

Returns
-------
X_embedded: ndarray of shape (n_samples, n_components)
    The data samples transformed.

Raises
------
NotFittedError
    If :meth:`fit` has not been called before.
F)reset)r   r   rJ   dotr:   T)r2   r]   s     r3   	transform(NeighborhoodComponentsAnalysis.transform\  s3    & 	$/vva))++,,r6   c                    UnU R                   (       a  [        U S5      (       a  U R                  nU$ [        U[        R
                  5      (       a   U$ UR                  u  pVU R                  =(       d    UnUS:X  aH  [        [        R                  " U5      5      nU[        XhS-
  5      ::  a  SnOU[        Xe5      :  a  SnOSnUS:X  a%  [        R                  " XqR                  S   5      nU$ US:X  a)  U R                  R                  XqR                  S   4S9nU$ US	;   Ga;  [        R                  " 5       n	US:X  aj  [        XpR                  S
9n
U R                   (       a(  [#        SSS9  [$        R&                  R)                  5         U
R+                  U5        U
R                  nOuUS:X  ao  SSKJn  U" US9nU R                   (       a(  [#        SSS9  [$        R&                  R)                  5         UR+                  X5        UR0                  R2                  SU nU R                   (       a0  [#        SR5                  [        R                  " 5       U	-
  5      5        U$ )a  Initialize the transformation.

Parameters
----------
X : array-like of shape (n_samples, n_features)
    The training samples.

y : array-like of shape (n_samples,)
    The training labels.

init : str or ndarray of shape (n_features_a, n_features_b)
    The validated initialization of the linear transformation.

Returns
-------
transformation : ndarray of shape (n_components, n_features)
    The initialized linear transformation.

r:   r!   r   r   r    r#   r"   )size>   r   r    )r(   r&   z Finding principal components...  )endr   )LinearDiscriminantAnalysis)r(   z*Finding most discriminative components... Nzdone in {:5.2f}s)r*   rH   r:   rI   rJ   rK   rF   r(   lenuniquemineyerL   standard_normalrM   r   r%   r\   sysstdoutflushre   discriminant_analysisrq   	scalings_rj   rZ   )r2   r]   r^   r)   ra   	n_samples
n_featuresr(   	n_classes	init_timer    rq   r   s                r3   rP   *NeighborhoodComponentsAnalysis._initializet  s    * ??wt];;!--NT S bjj))P M %&GG!I,,:
Lv~		!-	3zq=#AA D!C
$>> D%Dz!!#ggaj!A6 5 !!%!3!3!C!C&
3 "D "2 - ' IIK	5=%1@R@RC ||@bI

((*GGAJ%(__NU]R4,OC||JPRS

((*GGAM%(]]__]l%CN<<,33DIIK)4KLMr6   c                 ~    U R                   b  U R                  XR                  5        U =R                  S-  sl        g)zCalled after each iteration of the optimizer.

Parameters
----------
transformation : ndarray of shape (n_components * n_features,)
    The solution computed by the optimizer in this iteration.
Nr   )r-   rT   )r2   ra   s     r3   rS   (NeighborhoodComponentsAnalysis._callback  s,     ==$MM.,,7r6   c                    U R                   S:X  a  U =R                   S-  sl         U R                  (       am  / SQnSnUR                  " U6 nU R                  R                  n[        SR                  U5      5        [        SR                  XUS[        U5      -  5      5        [        R                  " 5       n	UR                  SUR                  S   5      n[        R                  " X!R                  5      n
[        U
S	S
9n[        R                  " U[        R                  5        [!        U* 5      nX-  n[        R"                  " USS	S9n[        R"                  " U5      nXU-  -
  nXR                  -   n[        R                  " UUR#                  SS9* 5        SU
R                  R                  U5      R                  U5      -  nU R                  (       ar  [        R                  " 5       U	-
  n	Sn[        UR                  U R                  R                  U R                   X5      5        [$        R&                  R)                  5         XN-  UUR+                  5       -  4$ )a  Compute the loss and the loss gradient w.r.t. `transformation`.

Parameters
----------
transformation : ndarray of shape (n_components * n_features,)
    The raveled linear transformation on which to compute loss and
    evaluate gradient.

X : ndarray of shape (n_samples, n_features)
    The training samples.

same_class_mask : ndarray of shape (n_samples, n_samples)
    A mask where `mask[i, j] == 1` if `X[i]` and `X[j]` belong
    to the same class, and `0` otherwise.

Returns
-------
loss : float
    The loss computed for the given transformation.

gradient : ndarray of shape (n_components * n_features,)
    The new (flattened) gradient of the loss.
r   r   )	IterationzObjective ValuezTime(s)z{:>10} {:>20} {:>10}z[{}]z[{}] {}
[{}] {}-r;   T)squared)axiskeepdims)r   r   z[{}] {:>10} {:>20.6e} {:>10.2f})rT   r%   rZ   rW   rX   r\   rr   rM   rV   rF   rJ   ri   rj   r   fill_diagonalinfr   sumrw   rx   ry   rO   )r2   ra   r]   r`   signheader_fields
header_fmtheaderrd   	t_funcall
X_embeddedp_ijmasked_p_ijplossweighted_p_ijweighted_p_ij_symgradient
values_fmts                      r3   rQ   /NeighborhoodComponentsAnalysis._loss_grad_lbfgs  s   2 <<1LLAL|| K3
#**M:>>22fmmH-.&-- (C#f+4E IIK	'//AGGAJ?VVA//0
 "*d;
rvv&u~ ,FF;Q6vvay $Qh.)OO;
*]->->A->-F,FGz||''(9:>>qAA <<		i/I:J!!NN++T\\4
 JJ{D8>>#3333r6   c                 F   > [         TU ]  5       nSUR                  l        U$ )NT)super__sklearn_tags__target_tagsrequired)r2   tagsrW   s     r3   r   /NeighborhoodComponentsAnalysis.__sklearn_tags__  s#    w')$(!r6   c                 4    U R                   R                  S   $ )z&Number of transformed output features.r   )r:   rF   )r2   s    r3   _n_features_out.NeighborhoodComponentsAnalysis._n_features_out  s     %%a((r6   )r-   r:   r)   r+   r(   rT   r&   rL   r,   r%   r*   r1   )g      ?)rX   
__module____qualname____firstlineno____doc__r   r   r   rJ   rK   r   callabler.   rR   __annotations__r4   r   re   rk   rP   rS   rQ   r   propertyr   __static_attributes____classcell__)rW   s   @r3   r   r   "   s    Tp Xq$v6

 CDJJ
 !kh4?@q$v67t$;'($D & ) )* 5x 6xt-0AFH4T
 ) )r6   r   ))r   rw   rM   numbersr   r   warningsr   numpyrJ   scipy.optimizer   baser   r	   r
   r   decompositionr   
exceptionsr   metricsr   preprocessingr   utils._param_validationr   r   utils.extmathr   utils.fixesr   utils.multiclassr   utils.randomr   utils.validationr   r   r   r   rD   r6   r3   <module>r      s_      "   #    + ( ( : # < ; - J Jt)#%5}t)r6   