Ë
    C�Dj  ã                   ó~   — 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  G d	„ d
e
ee	e¬«      Zy)z)Principal Component Analysis Base Classesé    )ÚABCMetaÚabstractmethodN)Úlinalgé   )ÚBaseEstimatorÚClassNamePrefixFeaturesOutMixinÚTransformerMixin)Ú_add_to_diagonalÚdeviceÚget_namespace)Úcheck_is_fittedc                   óR   — e Zd ZdZd„ Zd„ Zed
d„«       Zd„ Zdd„Z	d„ Z
ed	„ «       Zy)Ú_BasePCAzwBase class for PCA methods.

    Warning: This class should not be used directly.
    Use derived classes instead.
    c           
      ó´  — t        | j                  «      \  }}| j                  }| j                  }| j                  r)||j	                  |dd…t
        j                  f   «      z  }|| j                  z
  }|j                  || j                  kD  ||j                  dt        |«      ¬«      «      }|j                  |z  |z  }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   )r   Úcomponents_Úexplained_variance_ÚwhitenÚsqrtÚnpÚnewaxisÚnoise_variance_ÚwhereÚasarrayr   ÚTr
   )ÚselfÚxpÚ_r   Úexp_varÚexp_var_diffÚcovs          ú_C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\sklearn/decomposition/_base.pyÚget_covariancez_BasePCA.get_covariance   sÄ   € ô ˜d×.Ñ.Ó/‰ˆˆAà×&Ñ&ˆØ×*Ñ*ˆØ�;Š;Ø%¨¯©°º¼2¿:¹:¸Ñ0FÓ(GÑGˆKØ ×!5Ñ!5Ñ5ˆØ—x‘xØ�d×*Ñ*Ñ*ØØ�J‰J�s¤6¨'£?ˆJÓ3ó
ˆð
 �}‰}˜|Ñ+¨{Ñ:ˆÜ˜˜d×2Ñ2°BÔ7Øˆ
ó    c           
      ól  — t        | j                  «      \  }}| j                  j                  d   }| j                  dk(  r|j	                  |«      | j
                  z  S |r|j                  j                  }nt        j                  }| j
                  dk(  r || j                  «       «      S | j                  }| j                  }| j                  r)||j                  |dd…t        j                  f   «      z  }|| j
                  z
  }|j                  || j
                  kD  ||j                  dt!        |«      ¬«      «      }||j"                  z  | j
                  z  }t%        |d|z  |«       |j"                   ||«      z  |z  }|| j
                  dz   z  }t%        |d| j
                  z  |«       |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   Nr   g      ð?r   )r   r   ÚshapeÚn_components_Úeyer   r   Úinvr$   r   r   r   r   r   r   r   r   r   r
   )	r   r   Úis_array_api_compliantÚ
n_featuresÚ
linalg_invr   r    r!   Ú	precisions	            r#   Úget_precisionz_BasePCA.get_precision:   s�  € ô &3°4×3CÑ3CÓ%DÑ"ˆÐ"à×%Ñ%×+Ñ+¨AÑ.ˆ
ð ×Ñ Ò"Ø—6‘6˜*Ó%¨×(<Ñ(<Ñ<Ð<á!ØŸ™Ÿ™‰JäŸ™ˆJà×Ñ 3Ò&Ù˜d×1Ñ1Ó3Ó4Ð4ð ×&Ñ&ˆØ×*Ñ*ˆØ�;Š;Ø%¨¯©°º¼2¿:¹:¸Ñ0FÓ(GÑGˆKØ ×!5Ñ!5Ñ5ˆØ—x‘xØ�d×*Ñ*Ñ*ØØ�J‰J�s¤6¨'£?ˆJÓ3ó
ˆð
   +§-¡-Ñ/°$×2FÑ2FÑFˆ	Ü˜ C¨,Ñ$6¸Ô;Ø—M‘M¡J¨yÓ$9Ñ9¸KÑGˆ	Ø�t×+Ñ+¨QÑ.Ð/Ñ/ˆ	Ü˜ C¨$×*>Ñ*>Ñ$>ÀÔCØÐr%   Nc                  ó   — y)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.
        N© )r   ÚXÚys      r#   Úfitz_BasePCA.fitg   s   � r%   c                 óÞ   — t        || j                  | j                  «      \  }}t        | «       | j	                  ||j
                  |j                  gdd¬«      }| j                  ||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.
        )ÚcsrÚcscF)ÚdtypeÚaccept_sparseÚreset)r   Úx_is_centered)r   r   r   r   Ú_validate_dataÚfloat64Úfloat32Ú
_transform)r   r3   r   r   s       r#   Ú	transformz_BasePCA.transformy   sm   € ô$ ˜a ×!1Ñ!1°4×3KÑ3KÓL‰ˆˆAä˜Ôà×ÑØ�b—j‘j "§*¡*Ð-¸^ÐSXð  ó 
ˆð �‰˜q R°uˆÓ=Ð=r%   c                 óZ  — || j                   j                  z  }|s6||j                  | j                  d«      | j                   j                  z  z  }| j                  rM|j                  | j                  «      }|j                  |j                  «      j                  }||||k  <   ||z  }|S )N)r'   éÿÿÿÿ)
r   r   ÚreshapeÚmean_r   r   r   Úfinfor9   Úeps)r   r3   r   r<   ÚX_transformedÚscaleÚ	min_scales          r#   r@   z_BasePCA._transform”   s›   € Ø˜D×,Ñ,×.Ñ.Ñ.ˆÙð ˜RŸZ™Z¨¯
©
°GÓ<¸t×?OÑ?O×?QÑ?QÑQÑQˆMØ�;Š;ð
 —G‘G˜D×4Ñ4Ó5ˆEØŸ™ §¡Ó-×1Ñ1ˆIØ'0ˆE�%˜)Ñ#Ñ$Ø˜UÑ"ˆMØÐr%   c                 ó  — t        |«      \  }}| j                  rO|j                  | j                  dd…t        j
                  f   «      | j                  z  }||z  | j                  z   S || j                  z  | j                  z   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   rE   )r   r3   r   r   Úscaled_componentss        r#   Úinverse_transformz_BasePCA.inverse_transform¨   sy   € ô, ˜aÓ ‰ˆˆAà�;Š;à—‘˜×0Ñ0²´B·J±J°Ñ?Ó@À4×CSÑCSÑSð ð Ð(Ñ(¨4¯:©:Ñ5Ð5à�t×'Ñ'Ñ'¨$¯*©*Ñ4Ð4r%   c                 ó4   — | j                   j                  d   S )z&Number of transformed output features.r   )r   r(   )r   s    r#   Ú_n_features_outz_BasePCA._n_features_outÈ   s   € ð ×Ñ×%Ñ% aÑ(Ð(r%   )N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r$   r0   r   r5   rA   r@   rM   ÚpropertyrO   r2   r%   r#   r   r      sJ   „ ñòò8+ðZ òó ðò">ó6ò(5ð@ ñ)ó ñ)r%   r   )Ú	metaclass)rS   Úabcr   r   Únumpyr   Úscipyr   Úbaser   r   r	   Úutils._array_apir
   r   r   Úutils.validationr   r   r2   r%   r#   ú<module>r\      s9   ðÙ /÷ (ã Ý ç SÑ Sß FÑ FÝ .ôv)Ø#Ð%5°}ÐPWöv)r%   