a
    h                  
   @   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rd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sumZ	transpose	conjugatedot)ausZvhtfactorZcondZrank r1   \/var/www/html/assistant/venv/lib/python3.9/site-packages/sklearn/cross_decomposition/_pls.py
_pinv2_old)   s    
r3   A  ư>Fc              
      s  t | jj zt fdd|jD }W n. tyX } ztd|W Y d}~n
d}~0 0 d}|dkrxt| t| }	}
t|D ]}|dkrt 	|	|}nt 	| j|t 	|| }|t 
t 	||   }t 	| |}|dkrt 	|
|}nt 	|j|t 	|j| }|r,|t 
t 	||   }t 	||t 	||   }|| }t 	|||k sr|jd dkrx q~|}q|d }||krt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(   r1   r2   	<genexpr>H       z;_get_first_singular_vectors_power_method.<locals>.<genexpr>y residual is constantNd   B   z$Maximum number of iterations reached)r%   r'   r"   r(   nextTStopIterationr3   ranger+   sqrtshapewarningswarnr   )XYmodemax_itertolnorm_y_weightsZy_scoreeZx_weights_oldZX_pinvZY_pinvi	x_weightsZx_score	y_weightsZx_weights_diffZn_iterr1   r<   r2   (_get_first_singular_vectors_power_method;   s8     "
rU   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+   rD   r   )rK   rL   CU_Vtr1   r1   r2   _get_first_singular_vectors_svdv   s    r[   Tc                 C   s   | j dd}| |8 } |j dd}||8 }|rr| 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   axisrB   )r]   Zddofg      ?        )meanZstdr%   ZonesrH   )rK   rL   scaleZx_meanZy_meanZx_stdZy_stdr1   r1   r2   _center_scale_xy   s    
ra   c                 C   s2   t t | }t | | }| |9 } ||9 }dS )z7Same as svd_flip but works on 1d arrays, and is inplaceN)r%   Zargmaxr9   sign)r-   vZbiggest_abs_val_idxrb   r1   r1   r2   _svd_flip_1d   s    rd   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.)rI   rJ   FutureWarning
ValueErroryrL   r1   r1   r2   _deprecate_Y_when_optional   s    ri   c                 C   s"   | d u r|d u rt dt| |S )Nzy is required.)rf   ri   rg   r1   r1   r2   _deprecate_Y_when_required   s    rj   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
    rB   Nleftclosedboolean
regression	canonicalr4   rA   r   nipalsr   n_componentsr`   deflation_moderM   	algorithmrN   rO   copy_parameter_constraintsr   Tr5   r6   )r`   ru   rM   rv   rN   rO   rw   c          	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S r7   )rt   ru   rM   r`   rv   rN   rO   rw   )	selfrt   r`   ru   rM   rv   rN   rO   rw   r1   r1   r2   __init__   s    z_PLS.__init__Z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rbd| _|	dd}nd| _|j
d	 }|j
d }|j
d }| j}| jd
krt||n
t|||}||krt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rBtj#t$|d| k d	d}d|dd|f< z$t%|
|| j&| j'| j(|	d\}}}W nV t)y2 } z<t*|dkr t+,d|  W Y d}~ qW Y d}~n
d}~0 0 | j-| n| j"dkr\t.|
|\}}t/|| t0|
|}|	r~d}nt0||}t0||| }t0||
t0|| }|
t1||8 }
| jdkrt0||t0|| }|t1||8 }| jd
kr*t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< qzt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_writeablerw   Zensure_min_samplesrh   F
input_namer"   r~   rw   	ensure_2drB   r   rp   `n_components` upper bound is . Got   instead. Reduce `n_components`.rq   rr   
   r\   r^   N)rM   rN   rO   rP   r?   z$y residual is constant at iteration r   )r   )9rj   r   r   r%   float64rw   r   ndim_predict_1dreshaperH   rt   ru   minrf   Z_norm_y_weightsra   r`   _x_mean_y_mean_x_std_y_stdZzeros
x_weights_
y_weights_	_x_scores	_y_scoresx_loadings_y_loadings_n_iter_r'   r"   r(   rF   rv   allr9   rU   rM   rN   rO   rE   strrI   rJ   appendr[   rd   r+   outerr   rD   x_rotations_y_rotations_coef_
intercept__n_features_out)ry   rK   rh   rL   npqrt   rank_upper_boundrP   ZXkZykZy_epskZyk_maskrS   rT   r   rQ   x_scoresZy_ssy_scoresZ
x_loadingsZ
y_loadingsr1   r1   r2   fit   s    





&
	z_PLS.fitc                 C   s   t ||}t|  t| ||tdd}|| j8 }|| j }t|| j}|durt	|dd|td}|j
dkrv|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rw   r"   resetNrh   )r   r   rw   r"   rB   r   )ri   r   r   r   r   r   r%   r+   r   r   r   r   r   r   r   )ry   rK   rh   rL   rw   r   r   r1   r1   r2   	transform  s"    






z_PLS.transformc                 C   s   t ||}t|  t|dtd}t|| jj}|| j9 }|| j	7 }|durt|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`.
        rK   )r   r"   Nrh   )ri   r   r   r   r%   matmulr   rD   r   r   r   r   r   )ry   rK   rh   rL   ZX_reconstructedZy_reconstructedr1   r1   r2   inverse_transform  s     




z_PLS.inverse_transformc                 C   sH   t |  t| ||tdd}|| j8 }|| jj | j }| jrD| 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   rD   r   r   Zravel)ry   rK   rw   ZYpredr1   r1   r2   predict  s
    
z_PLS.predictc                 C   s   |  ||||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   ry   rK   rh   r1   r1   r2   fit_transform  s    z_PLS.fit_transformc                    s   t   }d|j_d|j_|S )NTF)super__sklearn_tags__Zregressor_tagsZ
poor_scoreZtarget_tagsrequired)ry   tags	__class__r1   r2   r   -  s    
z_PLS.__sklearn_tags__)r   )NN)NNT)NN)T)N)__name__
__module____qualname____doc__r   r   r   r   rx   dict__annotations__r   rz   r   r   r   r   r   r   r   __classcell__r1   r1   r   r2   rk      s:   

  ,
/
5

rk   )	metaclassc                       sd   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`.
    rx   ru   rM   rv   r   Tr5   r6   r`   rN   rO   rw   c             
      s    t  j||ddd|||d d S )Nrp   r4   rr   rs   r   rz   ry   rt   r`   rN   rO   rw   r   r1   r2   rz     s    zPLSRegression.__init__Nc                    s,   t ||}t || | j| _| j| _| S )r|   )rj   r   r   r   Z	x_scores_r   Z	y_scores_)ry   rK   rh   rL   r   r1   r2   r     s
    
zPLSRegression.fit)r   )NN)r   r   r   r   rk   rx   r   r   parampoprz   r   r   r1   r1   r   r2   r   4  s   
g	 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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)
    rx   )ru   rM   r   Trr   r5   r6   )r`   rv   rN   rO   rw   c             
      s    t  j||dd||||d d S )Nrq   r4   rs   r   )ry   rt   r`   rv   rN   rO   rw   r   r1   r2   rz   E  s    
zPLSCanonical.__init__)r   r   r   r   r   rk   rx   r   r   r   r   rz   r   r1   r1   r   r2   r     s   
b r   c                       sV   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)
    rx   r   r   Tr5   r6   r   c             
      s    t  j||ddd|||d d S )Nrq   rA   rr   rs   r   r   r   r1   r2   rz     s    zCCA.__init__)r   r   r1   r1   r   r2   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))
    rB   Nrl   rm   ro   rt   r`   rw   rx   r   T)r`   rw   c                C   s   || _ || _|| _d S r7   r   )ry   rt   r`   rw   r1   r1   r2   rz     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rZ|dd}| j	}t
|jd	 |jd |jd }||krt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}   rh   Fr   rB   r   r   r   r   r   rV   N)rj   r   r   r%   r   rw   r   r   r   rt   r   rH   rf   ra   r`   r   r   r   r   r+   rD   r   r   r   r   r   )ry   rK   rh   rL   rt   r   rW   rX   r.   rZ   Vr1   r1   r2   r     sR    


z
PLSSVD.fitc                 C   s   t ||}t|  t| |tjdd}|| j | j }t|| j}|durt	|ddtjd}|j
dkrr|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   Nrh   )r   r   r"   rB   r   )ri   r   r   r%   r   r   r   r+   r   r   r   r   r   r   r   )ry   rK   rh   rL   ZXrr   yrr   r1   r1   r2   r   `  s    

zPLSSVD.transformc                 C   s   |  ||||S )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   r1   r1   r2   r     s    zPLSSVD.fit_transform)r   )NN)NN)N)r   r   r   r   r   r   rx   r   r   rz   r   r   r   r   r1   r1   r1   r2   r     s   
DG
(r   )r4   r5   r6   F)T)3r   rI   abcr   r   numbersr   r   numpyr%   Zscipy.linalgr   baser   r	   r
   r   r   r   
exceptionsr   utilsr   r   Zutils._param_validationr   r   Zutils.extmathr   Zutils.fixesr   r   Zutils.validationr   r   r   __all__r   r   r3   rU   r[   ra   rd   ri   rj   rk   r   r   r   r   r1   r1   r1   r2   <module>   sP     
;

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