`L idZddlmZmZddlZddlmZddlm Z m Z m Z ddl m Z mZmZddlmZmZGd d e e e e Zy) z)Principal Component Analysis Base Classes)ABCMetaabstractmethodN)linalg) BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin)_fill_or_add_to_diagonaldevice get_namespace)check_is_fitted validate_datacReZdZdZdZdZed dZdZd dZ dZ 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}|jr)||j |ddt j fz}||jz }|j||jkD||jdt||j}|j|z|z}t||j||S)asCompute 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_whitensqrtnpnewaxisnoise_variance_whereasarrayr rTr )selfxp_rexp_var exp_var_diffcovs a/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/decomposition/_base.pyget_covariancez_BasePCA.get_covariancesd../A&& ** ;;%2:: 0F(GGK!5!55 xx d** *  JJs6'?'--J H }}|+{: d&:&:B? c lt|j\}}|jjd}|jdk(r|j ||j z S|r|j j}nt j}|j dk(r||jS|j}|j}|jr)||j|ddtjfz}||j z }|j||j kD||jdt!|}||j"z|j z }t%|d|z ||j"||z|z}||j dz z}t%|d|j z ||S)a8Compute 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. rrN)r g?r)r rshape n_components_eyerrinvr%rrrrrrrr rr ) rris_array_api_compliant n_features linalg_invrr!r" precisions r$ get_precisionz_BasePCA.get_precision5s&343C3C%D" "%%++A.     "66*%(<(<< < !JJ   3 &d1134 4&& ** ;;%2:: 0F(GGK!5!55 xx d** *  JJs6'?J 3  +--/$2F2FF  C,,>CMMJy$99KG t++Q.//  C$2F2F,FKr&Ncy)aPlaceholder 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)rXys r$fitz _BasePCA.fitbsr&ct||j|j\}}t|t |||j |j gdd}|j||dS)aApply 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. )csrcscF)r accept_sparsereset)r x_is_centered)r rrr rfloat64float32 _transform)rr4rr s r$ transformz_BasePCA.transformtsg$a!1!143K3KLA   ::rzz*(  qRu==r&cZ||jjz}|s6||j|jd|jjzz}|jrM|j |j }|j|jj}||||k<||z}|S)N)r() rrreshapemean_rrrfinforeps)rr4rr< X_transformedscale min_scales r$r?z_BasePCA._transformsD,,...  RZZ G>(5@))r&r) metaclass)rRabcrrnumpyrscipyrbaserrr utils._array_apir r r utils.validationr rrr3r&r$r[s9/ (SSNN=z)#%5}PWz)r&