
    >[gM                         d Z ddlZddlZddlmZmZ ddlmZ ddlZ	ddl
mZ ddlmZmZmZmZ ddlmZ dd	l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! ddl"m#Z#m$Z$m%Z%  G d deee      Z&y)z!
Neighborhood Component Analysis
    N)IntegralReal)warn)minimize   )BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin_fit_context)PCA)ConvergenceWarning)pairwise_distances)LabelEncoder)Interval
StrOptions)softmax)check_classification_targets)check_random_state)check_arraycheck_is_fittedvalidate_datac            
           e Zd ZU dZ eeddd      dg eh d      ej                  gdg eeddd      g ee	ddd      ge
dgd	gd
gdZeed<   	 ddddddddddZ ed      d        Zd Zd Zd ZddZ fdZed        Z xZS )NeighborhoodComponentsAnalysisa  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                t    || _         || _        || _        || _        || _        || _        || _        || _        y Nr%   )	selfr&   r'   r(   r)   r*   r+   r#   r$   s	            Q/var/www/html/bid-api/venv/lib/python3.12/site-packages/sklearn/neighbors/_nca.py__init__z'NeighborhoodComponentsAnalysis.__init__   s>     )	$  (    T)prefer_skip_nested_validationc           
         t        | ||d      \  }}t        |       t               j                  |      }| j                  E| j                  |j
                  d   kD  r)t        d| j                   d|j
                  d    d      | j                  rkt        | d      r_| j                  j
                  d   |j
                  d   k7  r6t        d	|j
                  d    d
| j                  j
                  d    d      | j                  }t        |t        j                        rt        |      }|j
                  d   |j
                  d   k7  r,t        d|j
                  d    d|j
                  d    d      |j
                  d   |j
                  d   kD  r,t        d|j
                  d    d|j
                  d    d      | j                  E| j                  |j
                  d   k7  r)t        d| j                   d|j
                  d    d      t        | j                         | _        t%        j$                         }|ddt        j&                  f   |t        j&                  ddf   k(  }t        j(                  | j+                  |||            }| j,                  dkD  r| j,                  dz
  nd}d| j.                  ||dfd|| j0                  t3        | j4                  |      | j6                  d}d| _        t;        di |}	|	j<                  j?                  d|j
                  d         | _	        t%        j$                         |z
  }| j,                  rg| j@                  jB                  }
|	jD                  s*tG        djI                  |
|	jJ                        tL               tO        djI                  |
|             | S )ao  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(   hasattrr7   r'   
isinstancenpndarrayr   r   r$   random_state_timenewaxisravel_initializer#   _loss_grad_lbfgsr*   dictr)   	_callbackn_iter_r   xreshape	__class____name__successr   formatmessager   print)r0   Xyr'   t_trainsame_class_masktransformationr:   optimizer_params
opt_resultcls_names              r1   fitz"NeighborhoodComponentsAnalysis.fit   s   $ T1aA>1$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"21a">? $(<<!#3t||a ((. 88DMM=	
 1 01
 &<<//AGGAJ? ))+'<<~~..H %%3:: *"4"4 '	 077'JKr3   c                     t        |        t        | |d      }t        j                  || j                  j
                        S )a  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   rG   dotr7   T)r0   rZ   s     r1   	transformz(NeighborhoodComponentsAnalysis.transformX  s7    & 	$/vva))++,,r3   c                    |}| j                   rt        | d      r| j                  }|S t        |t        j
                        r	 |S |j                  \  }}| j                  xs |}|dk(  rGt        t	        j                  |            }|t        ||dz
        k  rd}n|t        ||      k  rd}nd}|dk(  r%t	        j                  ||j                  d         }|S |dk(  r-| j                  j                  ||j                  d   f      }|S |d	v r6t        j                         }	|dk(  rlt        || j                  
      }
| j                   r+t#        dd       t$        j&                  j)                          |
j+                  |       |
j                  }nv|dk(  rqddlm}  ||      }| j                   r+t#        dd       t$        j&                  j)                          |j+                  ||       |j0                  j2                  d| }| j                   r/t#        dj5                  t        j                         |	z
               |S )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.

        r7   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(   rE   r7   rF   rG   rH   rC   r&   lenuniquemineyerI   standard_normalrJ   r   r#   rY   sysstdoutflushrb   discriminant_analysisrl   	scalings_rf   rW   )r0   rZ   r[   r'   r^   	n_samples
n_featuresr&   	n_classes	init_timer   rl   r   s                r1   rM   z*NeighborhoodComponentsAnalysis._initializep  s   * ??wt];!--NT S bjj)P M %&GG!Iz,,:
Lv~		!-	3z9q=#AA D!C
I$>> D%Dz!!#aggaj!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

((*GGAqM%(]]__]l%CN<<,33DIIK)4KLMr3   c                 ~    | j                   | j                  || j                         | xj                  dz  c_        y)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+   rQ   )r0   r^   s     r1   rP   z(NeighborhoodComponentsAnalysis._callback  s.     ==$MM.$,,7r3   c                    | j                   dk(  r| xj                   dz  c_         | j                  rng d}d} |j                  | }| j                  j                  }t        dj                  |             t        dj                  |||dt        |      z               t        j                         }	|j                  d|j                  d         }t        j                  ||j                        }
t        |
d	
      }t        j                  |t        j                         t!        |       }||z  }t        j"                  |dd	      }t        j"                  |      }|||z  z
  }||j                  z   }t        j                  ||j#                  d              d|
j                  j                  |      j                  |      z  }| j                  rrt        j                         |	z
  }	d}t        |j                  | j                  j                  | j                   ||	             t$        j&                  j)                          ||z  ||j+                         z  fS )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[{}] {}
[{}] {}-r8   T)squared)axiskeepdims)r   r   z[{}] {:>10} {:>20.6e} {:>10.2f})rQ   r#   rW   rT   rU   rY   rm   rJ   rS   rC   rG   re   rf   r   fill_diagonalinfr   sumrr   rs   rt   rL   )r0   r^   rZ   r]   signheader_fields
header_fmtheaderra   	t_funcall
X_embeddedp_ijmasked_p_ijplossweighted_p_ijweighted_p_ij_symgradient
values_fmts                      r1   rN   z/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 $dQh.)MOO;
*]->->A->-F,FGz||''(9:>>qAA <<		i/I:J!!NN++T\\4
 JJd{D8>>#3333r3   c                 F    t         |          }d|j                  _        |S )NT)super__sklearn_tags__target_tagsrequired)r0   tagsrT   s     r1   r   z/NeighborhoodComponentsAnalysis.__sklearn_tags__
  s#    w')$(!r3   c                 4    | j                   j                  d   S )z&Number of transformed output features.r   )r7   rC   )r0   s    r1   _n_features_outz.NeighborhoodComponentsAnalysis._n_features_out  s     %%a((r3   r/   )g      ?)rU   
__module____qualname____doc__r   r   r   rG   rH   r   callabler,   rO   __annotations__r2   r   rb   rg   rM   rP   rN   r   propertyr   __classcell__)rT   s   @r1   r   r   !   s    Tp Xq$v6

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