o
    Û?Hh¢›  ã                
   @   sŒ  d Z ddlZddlmZmZ ddlmZmZ ddlZ	ddl
mZ ddlmZmZmZmZmZmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZmZ ddl m!Z!m"Z"m#Z# g d¢Z$eedƒkroddl
m%Z& nddl
m&Z& dd„ Z'	d0dd„Z(dd„ Z)d1dd„Z*dd „ Z+d!d"„ Z,d#d$„ Z-G d%d&„ d&eeeeeed'Z.G d(d)„ d)e.ƒZ/G d*d+„ d+e.ƒZ0G d,d-„ d-e.ƒZ1G d.d/„ d/eeeƒZ2dS )2zG
The :mod:`sklearn.pls` module implements Partial Least Squares (PLS).
é    N)ÚABCMetaÚabstractmethod)ÚIntegralÚReal)Úsvdé   )ÚBaseEstimatorÚClassNamePrefixFeaturesOutMixinÚMultiOutputMixinÚRegressorMixinÚTransformerMixinÚ_fit_context)ÚConvergenceWarning)Úcheck_arrayÚcheck_consistent_length)ÚIntervalÚ
StrOptions)Úsvd_flip)Úparse_versionÚ
sp_version)ÚFLOAT_DTYPESÚcheck_is_fittedÚvalidate_data)ÚPLSCanonicalÚPLSRegressionÚPLSSVDz1.7)Úpinv)Úpinv2c              
   C   sš   t | ddd\}}}|jj ¡ }dddœ}t |¡||  t |¡j }t ||k¡}|d d …d |…f }||d |…  }t 	t 
t ||d |… ¡¡¡S )NF)Úfull_matricesÚcheck_finiteg     @@g    €„.A)ÚfÚd)r   ÚdtypeÚcharÚlowerÚnpÚmaxÚfinfoÚepsÚsumÚ	transposeÚ	conjugateÚdot)ÚaÚuÚsÚvhÚtÚfactorÚcondÚrank© r5   ú`/home/air/sanwanet/gpt-api/venv/lib/python3.10/site-packages/sklearn/cross_decomposition/_pls.pyÚ
_pinv2_old)   s   
 r7   ÚAéô  çíµ ÷Æ°>Fc              
      s  t  | j¡j‰ zt‡ fdd„|jD ƒƒ}W n ty& } ztdƒ|‚d}~ww d}|dkr6t| ƒt|ƒ}	}
t|ƒD ]z}|dkrGt  	|	|¡}nt  	| j|¡t  	||¡ }|t  
t  	||¡¡ˆ   }t  	| |¡}|dkrrt  	|
|¡}nt  	|j|¡t  	|j|¡ }|r|t  
t  	||¡¡ˆ   }t  	||¡t  	||¡ˆ   }|| }t  	||¡|k s°|jd dkr² n|}q:|d }||krÃt dt¡ |||fS )	a?  Return the first left and right singular vectors of X'Y.

    Provides an alternative to the svd(X'Y) and uses the power method instead.
    With norm_y_weights to True and in mode A, this corresponds to the
    algorithm section 11.3 of the Wegelin's review, except this starts at the
    "update saliences" part.
    c                 3   s(    | ]}t  t  |¡ˆ k¡r|V  qd S ©N)r%   ÚanyÚabs)Ú.0Úcol©r(   r5   r6   Ú	<genexpr>H   s   €& z;_get_first_singular_vectors_power_method.<locals>.<genexpr>úy residual is constantNéd   ÚBé   z$Maximum number of iterations reached)r%   r'   r"   r(   ÚnextÚTÚStopIterationr7   Úranger,   ÚsqrtÚshapeÚwarningsÚwarnr   )ÚXÚYÚmodeÚmax_iterÚtolÚnorm_y_weightsÚy_scoreÚeÚx_weights_oldÚX_pinvÚY_pinvÚiÚ	x_weightsÚx_scoreÚ	y_weightsÚx_weights_diffÚn_iterr5   r@   r6   Ú(_get_first_singular_vectors_power_method;   s<   
€ÿ
r_   c                 C   s@   t  | j|¡}t|dd\}}}|dd…df |ddd…f fS )zbReturn the first left and right singular vectors of X'Y.

    Here the whole SVD is computed.
    F©r   Nr   )r%   r,   rG   r   )rN   rO   ÚCÚUÚ_ÚVtr5   r5   r6   Ú_get_first_singular_vectors_svdv   s    re   Tc                 C   s¢   | j dd}| |8 } |j dd}||8 }|r9| jddd}d||dk< | | } |jddd}d||dk< || }nt | jd ¡}t |jd ¡}| |||||fS )z{Center X, Y and scale if the scale parameter==True

    Returns
    -------
        X, Y, x_mean, y_mean, x_std, y_std
    r   ©ÚaxisrE   )rg   Úddofg      ð?ç        )ÚmeanÚstdr%   ÚonesrK   )rN   rO   ÚscaleÚx_meanÚy_meanÚx_stdÚy_stdr5   r5   r6   Ú_center_scale_xy€   s   
rr   c                 C   s2   t  t  | ¡¡}t  | | ¡}| |9 } ||9 }dS )z7Same as svd_flip but works on 1d arrays, and is inplaceN)r%   Úargmaxr=   Úsign)r.   ÚvÚbiggest_abs_val_idxrt   r5   r5   r6   Ú_svd_flip_1dš   s   rw   c                 C   s,   |d urt  dt¡ | d urtdƒ‚|S | S )NzE`Y` is deprecated in 1.5 and will be removed in 1.7. Use `y` instead.z?Cannot use both `y` and `Y`. Use only `y` as `Y` is deprecated.)rL   rM   ÚFutureWarningÚ
ValueError©ÚyrO   r5   r5   r6   Ú_deprecate_Y_when_optional¥   s   þÿr|   c                 C   s"   | d u r|d u rt dƒ‚t| |ƒS )Nzy is required.)ry   r|   rz   r5   r5   r6   Ú_deprecate_Y_when_required´   s   
r}   c                       sê   e Zd ZU dZeeddddgdgeddhƒged	d
hƒgeddhƒgeeddddgeeddddgdgdœZe	e
d< e	d$ddd	dddddœdd„ƒZeddd%dd„ƒZd&dd„Zd%dd„Zd'dd„Zd(d d!„Z‡ fd"d#„Z‡  ZS ))Ú_PLSa  Partial Least Squares (PLS)

    This class implements the generic PLS algorithm.

    Main ref: Wegelin, a survey of Partial Least Squares (PLS) methods,
    with emphasis on the two-block case
    https://stat.uw.edu/sites/default/files/files/reports/2000/tr371.pdf
    rE   NÚleft©ÚclosedÚbooleanÚ
regressionÚ	canonicalr8   rD   r   Únipalsr   ©Ún_componentsrm   Údeflation_moderP   Ú	algorithmrQ   rR   ÚcopyÚ_parameter_constraintsr   Tr9   r:   )rm   rˆ   rP   r‰   rQ   rR   rŠ   c          	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S r;   )r‡   rˆ   rP   rm   r‰   rQ   rR   rŠ   )	Úselfr‡   rm   rˆ   rP   r‰   rQ   rR   rŠ   r5   r5   r6   Ú__init__Ö   s   
z_PLS.__init__©Úprefer_skip_nested_validationc                 C   s  t ||ƒ}t||ƒ t| |tjd| jdd}t|dtjd| jdd}|jdkr1d| _| 	dd¡}nd| _|j
d	 }|j
d }|j
d }| j}| jd
krPt||ƒnt|||ƒ}||kretd|› d|› dƒ‚| jdk| _| j}	t||| jƒ\}
}| _| _| _| _t ||f¡| _t ||f¡| _t ||f¡| _t ||f¡| _t ||f¡| _t ||f¡| _g | _t |j¡j }t!|ƒD ]}| j"dkrtj#t $|¡d| k d	d}d|dd…|f< zt%|
|| j&| j'| j(|	d\}}}W n# t)y } zt*|ƒdkrþ‚ t+ ,d|› ¡ W Y d}~ n·d}~ww | j -|¡ n| j"dkr&t.|
|ƒ\}}t/||ƒ t 0|
|¡}|	r7d}nt 0||¡}t 0||¡| }t 0||
¡t 0||¡ }|
t 1||¡8 }
| jdkrst 0||¡t 0||¡ }|t 1||¡8 }| jd
krt 0||¡t 0||¡ }|t 1||¡8 }|| jdd…|f< || jdd…|f< || jdd…|f< || jdd…|f< || jdd…|f< || jdd…|f< q½t 0| jt2t 0| jj3| j¡dd¡| _4t 0| jt2t 0| jj3| j¡dd¡| _5t 0| j4| jj3¡| _6| j6| j j3| j | _6| j| _7| j4j
d | _8| S )ád  Fit model to data.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training vectors, where `n_samples` is the number of samples and
            `n_features` is the number of predictors.

        y : array-like of shape (n_samples,) or (n_samples, n_targets)
            Target vectors, where `n_samples` is the number of samples and
            `n_targets` is the number of response variables.

        Y : array-like of shape (n_samples,) or (n_samples, n_targets)
            Target vectors, where `n_samples` is the number of samples and
            `n_targets` is the number of response variables.

            .. deprecated:: 1.5
               `Y` is deprecated in 1.5 and will be removed in 1.7. Use `y` instead.

        Returns
        -------
        self : object
            Fitted model.
        Tr   ©r"   Úforce_writeablerŠ   Úensure_min_samplesr{   F©Ú
input_namer"   r’   rŠ   Ú	ensure_2drE   éÿÿÿÿr   rƒ   ú`n_components` upper bound is ú. Got ú  instead. Reduce `n_components`.r„   r…   é
   rf   ri   N)rP   rQ   rR   rS   rB   z$y residual is constant at iteration r   )r   )9r}   r   r   r%   Úfloat64rŠ   r   ÚndimÚ_predict_1dÚreshaperK   r‡   rˆ   Úminry   Ú_norm_y_weightsrr   rm   Ú_x_meanÚ_y_meanÚ_x_stdÚ_y_stdÚzerosÚ
x_weights_Ú
y_weights_Ú	_x_scoresÚ	_y_scoresÚx_loadings_Úy_loadings_Ún_iter_r'   r"   r(   rI   r‰   Úallr=   r_   rP   rQ   rR   rH   ÚstrrL   rM   Úappendre   rw   r,   Úouterr   rG   Úx_rotations_Úy_rotations_Úcoef_Ú
intercept_Ú_n_features_out)rŒ   rN   r{   rO   ÚnÚpÚqr‡   Úrank_upper_boundrS   ÚXkÚykÚy_epsÚkÚyk_maskrZ   r\   r­   rU   Úx_scoresÚy_ssÚy_scoresÚ
x_loadingsÚ
y_loadingsr5   r5   r6   Úfitì   sÖ   

úú



ÿÿÿÿúü	€ü
	þþz_PLS.fitc                 C   s¤   t ||ƒ}t| ƒ t| ||tdd}|| j8 }|| j }t || j¡}|durPt	|dd|td}|j
dkr;| dd¡}|| j8 }|| j }t || j¡}||fS |S )a  Apply the dimension reduction.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Samples to transform.

        y : array-like of shape (n_samples, n_targets), default=None
            Target vectors.

        Y : array-like of shape (n_samples, n_targets), default=None
            Target vectors.

            .. deprecated:: 1.5
               `Y` is deprecated in 1.5 and will be removed in 1.7. Use `y` instead.

        copy : bool, default=True
            Whether to copy `X` and `Y`, or perform in-place normalization.

        Returns
        -------
        x_scores, y_scores : array-like or tuple of array-like
            Return `x_scores` if `Y` is not given, `(x_scores, y_scores)` otherwise.
        F©rŠ   r"   ÚresetNr{   )r•   r–   rŠ   r"   rE   r—   )r|   r   r   r   r¢   r¤   r%   r,   r²   r   r   rŸ   r£   r¥   r³   )rŒ   rN   r{   rO   rŠ   rÀ   rÂ   r5   r5   r6   Ú	transform˜  s"   



ÿ


z_PLS.transformc                 C   sŠ   t ||ƒ}t| ƒ t|dtd}t || jj¡}|| j9 }|| j	7 }|durCt|dtd}t || j
j¡}|| j9 }|| j7 }||fS |S )a©  Transform data back to its original space.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_components)
            New data, where `n_samples` is the number of samples
            and `n_components` is the number of pls components.

        y : array-like of shape (n_samples,) or (n_samples, n_components)
            New target, where `n_samples` is the number of samples
            and `n_components` is the number of pls components.

        Y : array-like of shape (n_samples, n_components)
            New target, where `n_samples` is the number of samples
            and `n_components` is the number of pls components.

            .. deprecated:: 1.5
               `Y` is deprecated in 1.5 and will be removed in 1.7. Use `y` instead.

        Returns
        -------
        X_reconstructed : ndarray of shape (n_samples, n_features)
            Return the reconstructed `X` data.

        y_reconstructed : ndarray of shape (n_samples, n_targets)
            Return the reconstructed `X` target. Only returned when `y` is given.

        Notes
        -----
        This transformation will only be exact if `n_components=n_features`.
        rN   )r•   r"   Nr{   )r|   r   r   r   r%   Úmatmulr«   rG   r¤   r¢   r¬   r¥   r£   )rŒ   rN   r{   rO   ÚX_reconstructedÚy_reconstructedr5   r5   r6   Úinverse_transformÇ  s   
 



z_PLS.inverse_transformc                 C   sH   t | ƒ t| ||tdd}|| j8 }|| jj | j }| jr"| ¡ S |S )aU  Predict targets of given samples.

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

        copy : bool, default=True
            Whether to copy `X` and `Y`, or perform in-place normalization.

        Returns
        -------
        y_pred : ndarray of shape (n_samples,) or (n_samples, n_targets)
            Returns predicted values.

        Notes
        -----
        This call requires the estimation of a matrix of shape
        `(n_features, n_targets)`, which may be an issue in high dimensional
        space.
        FrÆ   )	r   r   r   r¢   r´   rG   rµ   rž   Úravel)rŒ   rN   rŠ   ÚYpredr5   r5   r6   Úpredictü  s
   
z_PLS.predictc                 C   ó   |   ||¡ ||¡S )a£  Learn and apply the dimension reduction on the train data.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Training vectors, where `n_samples` is the number of samples and
            `n_features` is the number of predictors.

        y : array-like of shape (n_samples, n_targets), default=None
            Target vectors, where `n_samples` is the number of samples and
            `n_targets` is the number of response variables.

        Returns
        -------
        self : ndarray of shape (n_samples, n_components)
            Return `x_scores` if `Y` is not given, `(x_scores, y_scores)` otherwise.
        ©rÅ   rÈ   ©rŒ   rN   r{   r5   r5   r6   Úfit_transform  ó   z_PLS.fit_transformc                    s   t ƒ  ¡ }d|j_d|j_|S )NTF)ÚsuperÚ__sklearn_tags__Úregressor_tagsÚ
poor_scoreÚtarget_tagsÚrequired)rŒ   Útags©Ú	__class__r5   r6   rÖ   -  s   
z_PLS.__sklearn_tags__©r   ©NN)NNT©Tr;   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r‹   ÚdictÚ__annotations__r   r   r   rÅ   rÈ   rÌ   rÏ   rÓ   rÖ   Ú__classcell__r5   r5   rÜ   r6   r~   º   s<   
 
øþö 
,
/
5
r~   )Ú	metaclassc                       sf   e Zd ZU dZi ej¥Zeed< dD ]Ze 	e¡ q	ddddddœ‡ fd	d
„Z
d‡ fdd„	Z‡  ZS )r   aÏ  PLS regression.

    PLSRegression is also known as PLS2 or PLS1, depending on the number of
    targets.

    For a comparison between other cross decomposition algorithms, see
    :ref:`sphx_glr_auto_examples_cross_decomposition_plot_compare_cross_decomposition.py`.

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

    .. versionadded:: 0.8

    Parameters
    ----------
    n_components : int, default=2
        Number of components to keep. Should be in `[1, n_features]`.

    scale : bool, default=True
        Whether to scale `X` and `Y`.

    max_iter : int, default=500
        The maximum number of iterations of the power method when
        `algorithm='nipals'`. Ignored otherwise.

    tol : float, default=1e-06
        The tolerance used as convergence criteria in the power method: the
        algorithm stops whenever the squared norm of `u_i - u_{i-1}` is less
        than `tol`, where `u` corresponds to the left singular vector.

    copy : bool, default=True
        Whether to copy `X` and `Y` in :term:`fit` before applying centering,
        and potentially scaling. If `False`, these operations will be done
        inplace, modifying both arrays.

    Attributes
    ----------
    x_weights_ : ndarray of shape (n_features, n_components)
        The left singular vectors of the cross-covariance matrices of each
        iteration.

    y_weights_ : ndarray of shape (n_targets, n_components)
        The right singular vectors of the cross-covariance matrices of each
        iteration.

    x_loadings_ : ndarray of shape (n_features, n_components)
        The loadings of `X`.

    y_loadings_ : ndarray of shape (n_targets, n_components)
        The loadings of `Y`.

    x_scores_ : ndarray of shape (n_samples, n_components)
        The transformed training samples.

    y_scores_ : ndarray of shape (n_samples, n_components)
        The transformed training targets.

    x_rotations_ : ndarray of shape (n_features, n_components)
        The projection matrix used to transform `X`.

    y_rotations_ : ndarray of shape (n_targets, n_components)
        The projection matrix used to transform `Y`.

    coef_ : ndarray of shape (n_target, n_features)
        The coefficients of the linear model such that `Y` is approximated as
        `Y = X @ coef_.T + intercept_`.

    intercept_ : ndarray of shape (n_targets,)
        The intercepts of the linear model such that `Y` is approximated as
        `Y = X @ coef_.T + intercept_`.

        .. versionadded:: 1.1

    n_iter_ : list of shape (n_components,)
        Number of iterations of the power method, for each
        component.

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

    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
    --------
    PLSCanonical : Partial Least Squares transformer and regressor.

    Examples
    --------
    >>> from sklearn.cross_decomposition import PLSRegression
    >>> X = [[0., 0., 1.], [1.,0.,0.], [2.,2.,2.], [2.,5.,4.]]
    >>> y = [[0.1, -0.2], [0.9, 1.1], [6.2, 5.9], [11.9, 12.3]]
    >>> pls2 = PLSRegression(n_components=2)
    >>> pls2.fit(X, y)
    PLSRegression()
    >>> Y_pred = pls2.predict(X)

    For a comparison between PLS Regression and :class:`~sklearn.decomposition.PCA`, see
    :ref:`sphx_glr_auto_examples_cross_decomposition_plot_pcr_vs_pls.py`.
    r‹   ©rˆ   rP   r‰   r   Tr9   r:   ©rm   rQ   rR   rŠ   c             
      ó    t ƒ j||ddd|||d d S )Nrƒ   r8   r…   r†   ©rÕ   r   ©rŒ   r‡   rm   rQ   rR   rŠ   rÜ   r5   r6   r   ¦  ó   
øzPLSRegression.__init__Nc                    s,   t ||ƒ}tƒ  ||¡ | j| _| j| _| S )r   )r}   rÕ   rÅ   r©   Ú	x_scores_rª   Ú	y_scores_)rŒ   rN   r{   rO   rÜ   r5   r6   rÅ   ´  s
   
zPLSRegression.fitrÞ   rß   )rá   râ   rã   rä   r~   r‹   rå   ræ   ÚparamÚpopr   rÅ   rç   r5   r5   rÜ   r6   r   4  s   
 g	ÿÿr   c                       sZ   e Zd ZU dZi ej¥Zeed< dD ]Ze 	e¡ q	ddddddd	œ‡ fd
d„Z
‡  ZS )r   a^  Partial Least Squares transformer and regressor.

    For a comparison between other cross decomposition algorithms, see
    :ref:`sphx_glr_auto_examples_cross_decomposition_plot_compare_cross_decomposition.py`.

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

    .. versionadded:: 0.8

    Parameters
    ----------
    n_components : int, default=2
        Number of components to keep. Should be in `[1, min(n_samples,
        n_features, n_targets)]`.

    scale : bool, default=True
        Whether to scale `X` and `Y`.

    algorithm : {'nipals', 'svd'}, default='nipals'
        The algorithm used to estimate the first singular vectors of the
        cross-covariance matrix. 'nipals' uses the power method while 'svd'
        will compute the whole SVD.

    max_iter : int, default=500
        The maximum number of iterations of the power method when
        `algorithm='nipals'`. Ignored otherwise.

    tol : float, default=1e-06
        The tolerance used as convergence criteria in the power method: the
        algorithm stops whenever the squared norm of `u_i - u_{i-1}` is less
        than `tol`, where `u` corresponds to the left singular vector.

    copy : bool, default=True
        Whether to copy `X` and `Y` in fit before applying centering, and
        potentially scaling. If False, these operations will be done inplace,
        modifying both arrays.

    Attributes
    ----------
    x_weights_ : ndarray of shape (n_features, n_components)
        The left singular vectors of the cross-covariance matrices of each
        iteration.

    y_weights_ : ndarray of shape (n_targets, n_components)
        The right singular vectors of the cross-covariance matrices of each
        iteration.

    x_loadings_ : ndarray of shape (n_features, n_components)
        The loadings of `X`.

    y_loadings_ : ndarray of shape (n_targets, n_components)
        The loadings of `Y`.

    x_rotations_ : ndarray of shape (n_features, n_components)
        The projection matrix used to transform `X`.

    y_rotations_ : ndarray of shape (n_targets, n_components)
        The projection matrix used to transform `Y`.

    coef_ : ndarray of shape (n_targets, n_features)
        The coefficients of the linear model such that `Y` is approximated as
        `Y = X @ coef_.T + intercept_`.

    intercept_ : ndarray of shape (n_targets,)
        The intercepts of the linear model such that `Y` is approximated as
        `Y = X @ coef_.T + intercept_`.

        .. versionadded:: 1.1

    n_iter_ : list of shape (n_components,)
        Number of iterations of the power method, for each
        component. Empty if `algorithm='svd'`.

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

    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
    --------
    CCA : Canonical Correlation Analysis.
    PLSSVD : Partial Least Square SVD.

    Examples
    --------
    >>> from sklearn.cross_decomposition import PLSCanonical
    >>> X = [[0., 0., 1.], [1.,0.,0.], [2.,2.,2.], [2.,5.,4.]]
    >>> y = [[0.1, -0.2], [0.9, 1.1], [6.2, 5.9], [11.9, 12.3]]
    >>> plsca = PLSCanonical(n_components=2)
    >>> plsca.fit(X, y)
    PLSCanonical()
    >>> X_c, y_c = plsca.transform(X, y)
    r‹   )rˆ   rP   r   Tr…   r9   r:   )rm   r‰   rQ   rR   rŠ   c             
      s    t ƒ j||dd||||d d S )Nr„   r8   r†   rì   )rŒ   r‡   rm   r‰   rQ   rR   rŠ   rÜ   r5   r6   r   E  s   

øzPLSCanonical.__init__rÞ   ©rá   râ   rã   rä   r~   r‹   rå   ræ   rñ   rò   r   rç   r5   r5   rÜ   r6   r   Ö  s   
 bþør   c                       sX   e Zd ZU dZi ej¥Zeed< dD ]Ze 	e¡ q	ddddddœ‡ fd	d
„Z
‡  ZS )ÚCCAa  Canonical Correlation Analysis, also known as "Mode B" PLS.

    For a comparison between other cross decomposition algorithms, see
    :ref:`sphx_glr_auto_examples_cross_decomposition_plot_compare_cross_decomposition.py`.

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

    Parameters
    ----------
    n_components : int, default=2
        Number of components to keep. Should be in `[1, min(n_samples,
        n_features, n_targets)]`.

    scale : bool, default=True
        Whether to scale `X` and `Y`.

    max_iter : int, default=500
        The maximum number of iterations of the power method.

    tol : float, default=1e-06
        The tolerance used as convergence criteria in the power method: the
        algorithm stops whenever the squared norm of `u_i - u_{i-1}` is less
        than `tol`, where `u` corresponds to the left singular vector.

    copy : bool, default=True
        Whether to copy `X` and `Y` in fit before applying centering, and
        potentially scaling. If False, these operations will be done inplace,
        modifying both arrays.

    Attributes
    ----------
    x_weights_ : ndarray of shape (n_features, n_components)
        The left singular vectors of the cross-covariance matrices of each
        iteration.

    y_weights_ : ndarray of shape (n_targets, n_components)
        The right singular vectors of the cross-covariance matrices of each
        iteration.

    x_loadings_ : ndarray of shape (n_features, n_components)
        The loadings of `X`.

    y_loadings_ : ndarray of shape (n_targets, n_components)
        The loadings of `Y`.

    x_rotations_ : ndarray of shape (n_features, n_components)
        The projection matrix used to transform `X`.

    y_rotations_ : ndarray of shape (n_targets, n_components)
        The projection matrix used to transform `Y`.

    coef_ : ndarray of shape (n_targets, n_features)
        The coefficients of the linear model such that `Y` is approximated as
        `Y = X @ coef_.T + intercept_`.

    intercept_ : ndarray of shape (n_targets,)
        The intercepts of the linear model such that `Y` is approximated as
        `Y = X @ coef_.T + intercept_`.

        .. versionadded:: 1.1

    n_iter_ : list of shape (n_components,)
        Number of iterations of the power method, for each
        component.

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

    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
    --------
    PLSCanonical : Partial Least Squares transformer and regressor.
    PLSSVD : Partial Least Square SVD.

    Examples
    --------
    >>> from sklearn.cross_decomposition import CCA
    >>> X = [[0., 0., 1.], [1.,0.,0.], [2.,2.,2.], [3.,5.,4.]]
    >>> y = [[0.1, -0.2], [0.9, 1.1], [6.2, 5.9], [11.9, 12.3]]
    >>> cca = CCA(n_components=1)
    >>> cca.fit(X, y)
    CCA(n_components=1)
    >>> X_c, Y_c = cca.transform(X, y)
    r‹   ré   r   Tr9   r:   rê   c             
      rë   )Nr„   rD   r…   r†   rì   rí   rÜ   r5   r6   r   º  rî   zCCA.__init__rÞ   ró   r5   r5   rÜ   r6   rô   [  s   
 Zÿÿrô   c                   @   sr   e Zd ZU dZeeddddgdgdgdœZeed< dd
d
dœdd„Z	e
d
dddd„ƒZddd„Zddd„ZdS )r   a¹  Partial Least Square SVD.

    This transformer simply performs a SVD on the cross-covariance matrix
    `X'Y`. It is able to project both the training data `X` and the targets
    `Y`. The training data `X` is projected on the left singular vectors, while
    the targets are projected on the right singular vectors.

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

    .. versionadded:: 0.8

    Parameters
    ----------
    n_components : int, default=2
        The number of components to keep. Should be in `[1,
        min(n_samples, n_features, n_targets)]`.

    scale : bool, default=True
        Whether to scale `X` and `Y`.

    copy : bool, default=True
        Whether to copy `X` and `Y` in fit before applying centering, and
        potentially scaling. If `False`, these operations will be done inplace,
        modifying both arrays.

    Attributes
    ----------
    x_weights_ : ndarray of shape (n_features, n_components)
        The left singular vectors of the SVD of the cross-covariance matrix.
        Used to project `X` in :meth:`transform`.

    y_weights_ : ndarray of (n_targets, n_components)
        The right singular vectors of the SVD of the cross-covariance matrix.
        Used to project `X` in :meth:`transform`.

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

    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
    --------
    PLSCanonical : Partial Least Squares transformer and regressor.
    CCA : Canonical Correlation Analysis.

    Examples
    --------
    >>> import numpy as np
    >>> from sklearn.cross_decomposition import PLSSVD
    >>> X = np.array([[0., 0., 1.],
    ...               [1., 0., 0.],
    ...               [2., 2., 2.],
    ...               [2., 5., 4.]])
    >>> y = np.array([[0.1, -0.2],
    ...               [0.9, 1.1],
    ...               [6.2, 5.9],
    ...               [11.9, 12.3]])
    >>> pls = PLSSVD(n_components=2).fit(X, y)
    >>> X_c, y_c = pls.transform(X, y)
    >>> X_c.shape, y_c.shape
    ((4, 2), (4, 2))
    rE   Nr   r€   r‚   ©r‡   rm   rŠ   r‹   r   T)rm   rŠ   c                C   s   || _ || _|| _d S r;   rõ   )rŒ   r‡   rm   rŠ   r5   r5   r6   r     s   
zPLSSVD.__init__rŽ   c                 C   s0  t ||ƒ}t||ƒ t| |tjd| jdd}t|dtjd| jdd}|jdkr-| dd¡}| j	}t
|jd	 |jd |jd ƒ}||krNtd
|› d|› dƒ‚t||| jƒ\}}| _| _| _| _t |j|¡}t|dd\}}}	|dd…d|…f }|	d|… }	t||	ƒ\}}	|	j}
|| _|
| _| jjd | _| S )a  Fit model to data.

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

        y : array-like of shape (n_samples,) or (n_samples, n_targets)
            Targets.

        Y : array-like of shape (n_samples,) or (n_samples, n_targets)
            Targets.

            .. deprecated:: 1.5
               `Y` is deprecated in 1.5 and will be removed in 1.7. Use `y` instead.

        Returns
        -------
        self : object
            Fitted estimator.
        Tr   r‘   r{   Fr”   rE   r—   r   r˜   r™   rš   r`   N)r}   r   r   r%   rœ   rŠ   r   r   rŸ   r‡   r    rK   ry   rr   rm   r¢   r£   r¤   r¥   r,   rG   r   r   r§   r¨   r¶   )rŒ   rN   r{   rO   r‡   rº   ra   rb   r/   rd   ÚVr5   r5   r6   rÅ     sR   

úú
ÿÿÿz
PLSSVD.fitc                 C   sœ   t ||ƒ}t| ƒ t| |tjdd}|| j | j }t || j¡}|durLt	|ddtjd}|j
dkr9| dd¡}|| j | j }t || j¡}||fS |S )aú  
        Apply the dimensionality reduction.

        Parameters
        ----------
        X : array-like of shape (n_samples, n_features)
            Samples to be transformed.

        y : array-like of shape (n_samples,) or (n_samples, n_targets),                 default=None
            Targets.

        Y : array-like of shape (n_samples,) or (n_samples, n_targets),                 default=None
            Targets.

            .. deprecated:: 1.5
               `Y` is deprecated in 1.5 and will be removed in 1.7. Use `y` instead.

        Returns
        -------
        x_scores : array-like or tuple of array-like
            The transformed data `X_transformed` if `Y is not None`,
            `(X_transformed, Y_transformed)` otherwise.
        F)r"   rÇ   Nr{   )r•   r–   r"   rE   r—   )r|   r   r   r%   rœ   r¢   r¤   r,   r§   r   r   rŸ   r£   r¥   r¨   )rŒ   rN   r{   rO   ÚXrrÀ   ÚyrrÂ   r5   r5   r6   rÈ   `  s   

zPLSSVD.transformc                 C   rÐ   )aü  Learn and apply the dimensionality reduction.

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

        y : array-like of shape (n_samples,) or (n_samples, n_targets),                 default=None
            Targets.

        Returns
        -------
        out : array-like or tuple of array-like
            The transformed data `X_transformed` if `Y is not None`,
            `(X_transformed, Y_transformed)` otherwise.
        rÑ   rÒ   r5   r5   r6   rÓ   ˆ  rÔ   zPLSSVD.fit_transformrÞ   rß   r;   )rá   râ   rã   rä   r   r   r‹   rå   ræ   r   r   rÅ   rÈ   rÓ   r5   r5   r5   r6   r   É  s   
 Dý
G(r   )r8   r9   r:   Frà   )3rä   rL   Úabcr   r   Únumbersr   r   Únumpyr%   Úscipy.linalgr   Úbaser   r	   r
   r   r   r   Ú
exceptionsr   Úutilsr   r   Úutils._param_validationr   r   Úutils.extmathr   Úutils.fixesr   r   Úutils.validationr   r   r   Ú__all__r   r   r7   r_   re   rr   rw   r|   r}   r~   r   r   rô   r   r5   r5   r5   r6   Ú<module>   sR     
ÿ;


ú  | # n