a
    �ù»j³  ã                   @   s|   d 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 ddlmZmZmZ ddlmZmZ G d	d
„ d
e
ee	ed�ZdS )z)Principal Component Analysis Base Classesé    )ÚABCMetaÚabstractmethodN)Úlinalgé   )ÚBaseEstimatorÚClassNamePrefixFeaturesOutMixinÚTransformerMixin)Ú_fill_or_add_to_diagonalÚdeviceÚget_namespace)Úcheck_is_fittedÚvalidate_datac                   @   sT   e Zd ZdZdd„ Zdd„ Zeddd„ƒZd	d
„ Zddd„Z	dd„ Z
edd„ ƒZdS )Ú_BasePCAzwBase class for PCA methods.

    Warning: This class should not be used directly.
    Use derived classes instead.
    c              	   C   sŒ   t | jƒ\}}| j}| j}| jr<|| |dd…tjf ¡ }|| j }| || jk||j	dt
|ƒ|jd�¡}|j| | }t|| j|ƒ |S )as  Compute data covariance with the generative model.

        ``cov = components_.T * S**2 * components_ + sigma2 * eye(n_features)``
        where S**2 contains the explained variances, and sigma2 contains the
        noise variances.

        Returns
        -------
        cov : array of shape=(n_features, n_features)
            Estimated covariance of data.
        Nç        )r
   Údtype)r   Úcomponents_Úexplained_variance_ÚwhitenÚsqrtÚnpÚnewaxisÚnoise_variance_ÚwhereÚasarrayr
   r   ÚTr	   )ÚselfÚxpÚ_r   Úexp_varÚexp_var_diffZcov© r    úAE:\soccer\ml_env\Lib\site-packages\sklearn/decomposition/_base.pyÚget_covariance   s    
ýz_BasePCA.get_covariancec           	      C   s  t | jƒ\}}| jjd }| jdkr4| |¡| j S |rB|jj}ntj}| jdkr^||  ¡ ƒS | j}| j	}| j
rŒ|| |dd…tjf ¡ }|| j }| || jk||jdt|ƒd�¡}||j | j }t|d| |ƒ |j||ƒ | }|| jd   }t|d| j |ƒ |S )a8  Compute data precision matrix with the generative model.

        Equals the inverse of the covariance but computed with
        the matrix inversion lemma for efficiency.

        Returns
        -------
        precision : array, shape=(n_features, n_features)
            Estimated precision of data.
        é   r   r   N)r
   g      ð?r   )r   r   ÚshapeZn_components_Úeyer   r   Úinvr"   r   r   r   r   r   r   r   r
   r   r	   )	r   r   Zis_array_api_compliantZ
n_featuresZ
linalg_invr   r   r   Ú	precisionr    r    r!   Úget_precision5   s2    



ýz_BasePCA.get_precisionNc                 C   s   dS )a¢  Placeholder for fit. Subclasses should implement this method!

        Fit the model with X.

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

        Returns
        -------
        self : object
            Returns the instance itself.
        Nr    )r   ÚXÚyr    r    r!   Úfitb   s    z_BasePCA.fitc                 C   sF   t || j| jƒ\}}t| ƒ t| ||j|jgddd�}| j||dd�S )a‹  Apply dimensionality reduction to X.

        X is projected on the first principal components previously extracted
        from a training set.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            New data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        Returns
        -------
        X_new : array-like of shape (n_samples, n_components)
            Projection of X in the first principal components, where `n_samples`
            is the number of samples and `n_components` is the number of the components.
        )ZcsrZcscF)r   Zaccept_sparseÚreset)r   Úx_is_centered)r   r   r   r   r   Zfloat64Zfloat32Ú
_transform)r   r)   r   r   r    r    r!   Ú	transformt   s    
ûz_BasePCA.transformFc                 C   sb   || j j }|s*|| | jd¡| j j 8 }| jr^| | j¡}| |j¡j	}||||k < || }|S )N)r#   éÿÿÿÿ)
r   r   ZreshapeÚmean_r   r   r   Zfinfor   Úeps)r   r)   r   r-   ZX_transformedÚscaleZ	min_scaler    r    r!   r.   “   s    z_BasePCA._transformc                 C   sT   t |ƒ\}}| jr@| | jdd…tjf ¡| j }|| | j S || j | j S dS )aç  Transform data back to its original space.

        In other words, return an input `X_original` whose transform would be X.

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

        Returns
        -------
        X_original array-like of shape (n_samples, n_features)
            Original data, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        Notes
        -----
        If whitening is enabled, inverse_transform will compute the
        exact inverse operation, which includes reversing whitening.
        N)r   r   r   r   r   r   r   r1   )r   r)   r   r   Zscaled_componentsr    r    r!   Úinverse_transform§   s    ÿz_BasePCA.inverse_transformc                 C   s   | j jd S )z&Number of transformed output features.r   )r   r$   )r   r    r    r!   Ú_n_features_outÇ   s    z_BasePCA._n_features_out)N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r"   r(   r   r+   r/   r.   r4   Úpropertyr5   r    r    r    r!   r      s   -
 r   )Ú	metaclass)r9   Úabcr   r   Únumpyr   Zscipyr   Úbaser   r   r   Zutils._array_apir	   r
   r   Zutils.validationr   r   r   r    r    r    r!   Ú<module>   s   
ÿ