`L ig4dZddlZddlmZmZddlZddlmZddl m Z m Z m Z m Z ddlmZddlmZmZmZdd lmZmZmZmZdd lmZmZd d gZd ZdZdZdZ d#dZ!dZ"dZ#edgdgdgdgdd d#ddddddddddddd d!Z$Gd"d e e e Z%y)$z Python implementation of the fast ICA algorithms. Reference: Tables 8.3 and 8.4 page 196 in the book: Independent Component Analysis, by Hyvarinen et al. N)IntegralReal)linalg) BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin _fit_context)ConvergenceWarning)as_float_array check_arraycheck_random_state)IntervalOptions StrOptionsvalidate_params)check_is_fitted validate_dataFastICAfasticacp|tjj||d|j|d|gz}|S)a Orthonormalize w wrt the first j rows of W. Parameters ---------- w : ndarray of shape (n,) Array to be orthogonalized W : ndarray of shape (p, n) Null space definition j : int < p The no of (from the first) rows of Null space W wrt which w is orthogonalized. Notes ----- Assumes that W is orthogonal w changed in place N)npr multi_dotT)wWjs d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sklearn/decomposition/_fastica.py_gs_decorrelationrs9*  a2A!BQ%0 11A Hcvtjtj||j\}}tj |tj |jjd}tjj|dtj|z z|j|gS)z@Symmetric decorrelation i.e. W <- (W * W.T) ^{-1/2} * W N)a_mina_max?) reighrdotrclipfinfodtypetinyrsqrt)rsus r_sym_decorrelationr.8s ;;rvva~ &DAq !''*//t} || ddfj } | tj | dzjz} t |D]} |tj| j||\} } || zjd| j| zz }t||| |tj |dzjz}tjtj|| zjdz }|} ||ksn|j dz| || ddf<A|t|fS)zcDeflationary FastICA using fun approx to neg-entropy function Used internally by FastICA. rr)Nraxis)shaperzerosr)rangecopyr+sumr&rmeanrabsappendmax)Xtolgfun_argsmax_iterw_init n_componentsrn_iterrrigwtxg_wtxw1lims r_ica_defrJGsZ <<?L , -QWW=A F<  1a4L    RWWadZZ\ ""x ABFF133NH5KD%d(a(5::r c t|}~t|jd}t|D]}|t j |||\} } tt j | |j |z | ddtjf|zz } ~ ~ tttt jd| |dz } | }| |ksntjdt|dzfS)zCParallel FastICA. Used internally by FastICA --main loop r1Nzij,ij->iz\FastICA did not converge. Consider increasing tolerance or the maximum number of iterations.)r.floatr4r6rr&rnewaxisr<r:einsumwarningswarnr ) r=r>r?r@rArBrp_iirFrGW1rIs r_ica_parrTjs 6"A qwwqz BHo q! h/ e tQSS 1B 6q"**}9MPQ9Q Q R %#c"))JA67!;<=  9    A    b1f9r c|jdd}||z}tj||}tj|jd|j }t |D]!\}}|d|dzz zj||<#||fS)Nalphar$rr0r1r)getrtanhemptyr4r) enumerater9)xr@rVgxg_xrEgx_is r_logcoshr_s LL# &EJA AB ((1771:QWW -CR=041tQw;'--/A0 s7Nr ctj|dz dz }||z}d|dzz |z}||jdfS)Nrr1r2)rexpr9)r[r@rbr\r]s r_exprcsK &&1a41 C SB q!t8s C sxxRx  r c<|dzd|dzzjdfS)Nrrar2)r9)r[r@s r_cuberfs$ a4!ad(b) ))r array-likeboolean)r= return_X_meancompute_sources return_n_iterFprefer_skip_nested_validationparallel unit-variancelogcosh-C6?svdT) algorithmwhitenfunr@rAr>rB whiten_solver random_staterirjrkc Jt||||||||| |  }|j|j|| }|jdvr|j}|j }nd}d}||j |g}| r|j|| r|j|j|S)a#Perform Fast Independent Component Analysis. The implementation is based on [1]_. Read more in the :ref:`User Guide `. Parameters ---------- X : array-like of shape (n_samples, n_features) Training vector, where `n_samples` is the number of samples and `n_features` is the number of features. n_components : int, default=None Number of components to use. If None is passed, all are used. algorithm : {'parallel', 'deflation'}, default='parallel' Specify which algorithm to use for FastICA. whiten : str or bool, default='unit-variance' Specify the whitening strategy to use. - If 'arbitrary-variance', a whitening with variance arbitrary is used. - If 'unit-variance', the whitening matrix is rescaled to ensure that each recovered source has unit variance. - If False, the data is already considered to be whitened, and no whitening is performed. .. versionchanged:: 1.3 The default value of `whiten` changed to 'unit-variance' in 1.3. fun : {'logcosh', 'exp', 'cube'} or callable, default='logcosh' The functional form of the G function used in the approximation to neg-entropy. Could be either 'logcosh', 'exp', or 'cube'. You can also provide your own function. It should return a tuple containing the value of the function, and of its derivative, in the point. The derivative should be averaged along its last dimension. Example:: def my_g(x): return x ** 3, (3 * x ** 2).mean(axis=-1) fun_args : dict, default=None Arguments to send to the functional form. If empty or None and if fun='logcosh', fun_args will take value {'alpha' : 1.0}. max_iter : int, default=200 Maximum number of iterations to perform. tol : float, default=1e-4 A positive scalar giving the tolerance at which the un-mixing matrix is considered to have converged. w_init : ndarray of shape (n_components, n_components), default=None Initial un-mixing array. If `w_init=None`, then an array of values drawn from a normal distribution is used. whiten_solver : {"eigh", "svd"}, default="svd" The solver to use for whitening. - "svd" is more stable numerically if the problem is degenerate, and often faster when `n_samples <= n_features`. - "eigh" is generally more memory efficient when `n_samples >= n_features`, and can be faster when `n_samples >= 50 * n_features`. .. versionadded:: 1.2 random_state : int, RandomState instance or None, default=None Used to initialize ``w_init`` when not specified, with a normal distribution. Pass an int, for reproducible results across multiple function calls. See :term:`Glossary `. return_X_mean : bool, default=False If True, X_mean is returned too. compute_sources : bool, default=True If False, sources are not computed, but only the rotation matrix. This can save memory when working with big data. Defaults to True. return_n_iter : bool, default=False Whether or not to return the number of iterations. Returns ------- K : ndarray of shape (n_components, n_features) or None If whiten is 'True', K is the pre-whitening matrix that projects data onto the first n_components principal components. If whiten is 'False', K is 'None'. W : ndarray of shape (n_components, n_components) The square matrix that unmixes the data after whitening. The mixing matrix is the pseudo-inverse of matrix ``W K`` if K is not None, else it is the inverse of W. S : ndarray of shape (n_samples, n_components) or None Estimated source matrix. X_mean : ndarray of shape (n_features,) The mean over features. Returned only if return_X_mean is True. n_iter : int If the algorithm is "deflation", n_iter is the maximum number of iterations run across all components. Else they are just the number of iterations taken to converge. This is returned only when return_n_iter is set to `True`. Notes ----- The data matrix X is considered to be a linear combination of non-Gaussian (independent) components i.e. X = AS where columns of S contain the independent components and A is a linear mixing matrix. In short ICA attempts to `un-mix' the data by estimating an un-mixing matrix W where ``S = W K X.`` While FastICA was proposed to estimate as many sources as features, it is possible to estimate less by setting n_components < n_features. It this case K is not a square matrix and the estimated A is the pseudo-inverse of ``W K``. This implementation was originally made for data of shape [n_features, n_samples]. Now the input is transposed before the algorithm is applied. This makes it slightly faster for Fortran-ordered input. References ---------- .. [1] A. Hyvarinen and E. Oja, "Fast Independent Component Analysis", Algorithms and Applications, Neural Networks, 13(4-5), 2000, pp. 411-430. Examples -------- >>> from sklearn.datasets import load_digits >>> from sklearn.decomposition import fastica >>> X, _ = load_digits(return_X_y=True) >>> K, W, S = fastica(X, n_components=7, random_state=0, whiten='unit-variance') >>> K.shape (7, 64) >>> W.shape (7, 7) >>> S.shape (1797, 7) rCrtrurvr@rAr>rBrwrxrj)roarbitrary-varianceN) r_validate_params_fit_transformru whitening_mean_ _unmixingr;n_iter_)r=rCrtrurvr@rAr>rBrwrxrirjrkestSKX_meanreturned_valuess rrrsZ !  #! C 1o>A zz<< NN #--+Ov&s{{+ r c ~eZdZUdZeeddddgeddhgedd heed hgehd e ge dgeedddgee d ddgd dgeddhgdgd Z e e d< d!dd dddddddd fd Zd"dZedd!dZedd!dZd#dZd#dZedZfd ZxZS)$raFastICA: a fast algorithm for Independent Component Analysis. The implementation is based on [1]_. Read more in the :ref:`User Guide `. Parameters ---------- n_components : int, default=None Number of components to use. If None is passed, all are used. algorithm : {'parallel', 'deflation'}, default='parallel' Specify which algorithm to use for FastICA. whiten : str or bool, default='unit-variance' Specify the whitening strategy to use. - If 'arbitrary-variance', a whitening with variance arbitrary is used. - If 'unit-variance', the whitening matrix is rescaled to ensure that each recovered source has unit variance. - If False, the data is already considered to be whitened, and no whitening is performed. .. versionchanged:: 1.3 The default value of `whiten` changed to 'unit-variance' in 1.3. fun : {'logcosh', 'exp', 'cube'} or callable, default='logcosh' The functional form of the G function used in the approximation to neg-entropy. Could be either 'logcosh', 'exp', or 'cube'. You can also provide your own function. It should return a tuple containing the value of the function, and of its derivative, in the point. The derivative should be averaged along its last dimension. Example:: def my_g(x): return x ** 3, (3 * x ** 2).mean(axis=-1) fun_args : dict, default=None Arguments to send to the functional form. If empty or None and if fun='logcosh', fun_args will take value {'alpha' : 1.0}. max_iter : int, default=200 Maximum number of iterations during fit. tol : float, default=1e-4 A positive scalar giving the tolerance at which the un-mixing matrix is considered to have converged. w_init : array-like of shape (n_components, n_components), default=None Initial un-mixing array. If `w_init=None`, then an array of values drawn from a normal distribution is used. whiten_solver : {"eigh", "svd"}, default="svd" The solver to use for whitening. - "svd" is more stable numerically if the problem is degenerate, and often faster when `n_samples <= n_features`. - "eigh" is generally more memory efficient when `n_samples >= n_features`, and can be faster when `n_samples >= 50 * n_features`. .. versionadded:: 1.2 random_state : int, RandomState instance or None, default=None Used to initialize ``w_init`` when not specified, with a normal distribution. Pass an int, for reproducible results across multiple function calls. See :term:`Glossary `. Attributes ---------- components_ : ndarray of shape (n_components, n_features) The linear operator to apply to the data to get the independent sources. This is equal to the unmixing matrix when ``whiten`` is False, and equal to ``np.dot(unmixing_matrix, self.whitening_)`` when ``whiten`` is True. mixing_ : ndarray of shape (n_features, n_components) The pseudo-inverse of ``components_``. It is the linear operator that maps independent sources to the data. mean_ : ndarray of shape(n_features,) The mean over features. Only set if `self.whiten` is True. n_features_in_ : int Number of features seen during :term:`fit`. .. versionadded:: 0.24 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 n_iter_ : int If the algorithm is "deflation", n_iter is the maximum number of iterations run across all components. Else they are just the number of iterations taken to converge. whitening_ : ndarray of shape (n_components, n_features) Only set if whiten is 'True'. This is the pre-whitening matrix that projects data onto the first `n_components` principal components. See Also -------- PCA : Principal component analysis (PCA). IncrementalPCA : Incremental principal components analysis (IPCA). KernelPCA : Kernel Principal component analysis (KPCA). MiniBatchSparsePCA : Mini-batch Sparse Principal Components Analysis. SparsePCA : Sparse Principal Components Analysis (SparsePCA). References ---------- .. [1] A. Hyvarinen and E. Oja, Independent Component Analysis: Algorithms and Applications, Neural Networks, 13(4-5), 2000, pp. 411-430. Examples -------- >>> from sklearn.datasets import load_digits >>> from sklearn.decomposition import FastICA >>> X, _ = load_digits(return_X_y=True) >>> transformer = FastICA(n_components=7, ... random_state=0, ... whiten='unit-variance') >>> X_transformed = transformer.fit_transform(X) >>> X_transformed.shape (1797, 7) r1Nleft)closedrn deflationr|roF>rbcuberpgrgr%rsrxrz_parameter_constraintsrprqrr) rtrurvr@rAr>rBrwrxc t |||_||_||_||_||_||_||_||_ | |_ | |_ yN) super__init__rCrtrurvr@rAr>rBrwrx) selfrCrtrurvr@rAr>rBrwrx __class__s rrzFastICA.__init__sY ("      *(r ct t|jtjtjgdj }j in j }tj}|jdd}d|cxkrdkstdtdjdk(rt}nFjd k(rt}n0jd k(rt}ntjrfd }|j \}} j"} js| d} t%j&d | t)| |} | t)| |kDr$t)| |} t%j&d | zjr|j+d} || ddtj,fz}j.dk(rt1j2|j5|\} } tj6| ddd}tj8| j:j<dz}| |k}tj>|rt%j&d|| |<tj@| | | || dd|f} } n-j.dk(rt1jB|dddd\} }  tjD| dz} |  z j d| }~ ~ tj4||}|tj@| z}n tG|d}jH}|4tjJ|jM| | f|j:}n8tjJ|}|j | | fk7rtdd| | fizjN|jP|d}jRdk(rtU|fi|\}}njRdk(rtW|fi|\}}~_,|rZjr-tj0j[|gj }n#tj4|j }nd}jrjd k(rZ|s,tj0j[|gj }tj\|dd!"}||z}|j z}tj4_/ _0|_1n_/t1jdj^d#_3|_4|S)$adFit the model. 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. compute_sources : bool, default=False If False, sources are not computes but only the rotation matrix. This can save memory when working with big data. Defaults to False. Returns ------- S : ndarray of shape (n_samples, n_components) or None Sources matrix. `None` if `compute_sources` is `False`. r)r7r)ensure_min_samplesNrVr$r1zalpha must be in [1,2]rprbrc*j|fi|Sr)rv)r[r@rs rr?z!FastICA._fit_transform..gIstxx.X..r z(Ignoring n_components with whiten=False.z/n_components is too large: it will be set to %srar2r% zfThere are some small singular values, using whiten_solver = 'svd' might lead to more accurate results.)outrsF) full_matrices check_finiter)r7)sizer0z/w_init has invalid shape -- should be %(shape)sr4)r>r?r@rArBrnrroT)r3keepdims)r)5rrurfloat64float32rr@rrxrW ValueErrorrvr_rcrfcallabler4rCrOrPminr9rMrwrr%r&argsortr(r)epsanyr+rssignr rBasarraynormalr>rArtrTrJrrstd components_rrpinvmixing_r)rr=rjXTr@rxrVr? n_features n_samplesrCrdr- sort_indicesrdegenerate_idxrX1rBkwargsrrDrS_stds` rr~zFastICA._fit_transform!s$  ::rzz*  ! .2DMM)$*;*;<  Wc*EQ56 656 6 88y A XX A XX A dhh  /!# I(( {{|7L MMD E  y*5L #i4 4y*5L MMALP  ;;WW"W%F &BJJ' 'B!!V+{{266!9-1!zz!}TrT2 hhqww'++b0!"S66.)MM, %(.!q!!\/(:1##u,zz"ENrPQR1 1 AQ -<(A12B "'')$ $B /B >ZZ##, )E#FbhhF ZZ'F|| l;; E| <=> 88     >>Z ' .v.IAv ^^{ * .v.IAv  {{II''Ar 355FF1bMOOA ;;{{o-& ++Q2J799Aqq48U UWW !vva|D DJDO D {{4#3#3%H r Trlc(|j|dS)a5Fit the model and recover the sources from 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. y : Ignored Not used, present for API consistency by convention. Returns ------- X_new : ndarray of shape (n_samples, n_components) Estimated sources obtained by transforming the data with the estimated unmixing matrix. Tr{r~rr=ys r fit_transformzFastICA.fit_transforms&""1d";;r c,|j|d|S)aFit the model to 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. y : Ignored Not used, present for API consistency by convention. Returns ------- self : object Returns the instance itself. Fr{rrs rfitz FastICA.fits$ Au5 r ct|t|||xr |jtjtj gd}|jr||j z}tj||jjS)a_Recover the sources from X (apply the unmixing matrix). Parameters ---------- X : array-like of shape (n_samples, n_features) Data to transform, where `n_samples` is the number of samples and `n_features` is the number of features. copy : bool, default=True If False, data passed to fit can be overwritten. Defaults to True. Returns ------- X_new : ndarray of shape (n_samples, n_components) Estimated sources obtained by transforming the data with the estimated unmixing matrix. F)r7r)reset) rrrurrrrr&rrrr=r7s r transformzFastICA.transformsn$    &4;;::rzz*   ;; OAvva))++,,r ct|t||xr |jtjtj g}tj ||jj}|jr||jz }|S)a6Transform the sources back to the mixed data (apply mixing matrix). Parameters ---------- X : array-like of shape (n_samples, n_components) Sources, where `n_samples` is the number of samples and `n_components` is the number of components. copy : bool, default=True If False, data passed to fit are overwritten. Defaults to True. Returns ------- X_original : ndarray of shape (n_samples, n_features) Reconstructed data obtained with the mixing matrix. )r7r)) rr rurrrr&rrrrs rinverse_transformzFastICA.inverse_transformsa  !5$++rzz2::>V W FF1dllnn % ;; OAr c4|jjdS)z&Number of transformed output features.r)rr4)rs r_n_features_outzFastICA._n_features_outs%%a((r cJt|}ddg|j_|S)Nrr)r__sklearn_tags__transformer_tagspreserves_dtype)rtagsrs rrzFastICA.__sklearn_tags__!s(w')1:I0F- r r)F)T)__name__ __module__ __qualname____doc__rrrrboolrdictrr__annotations__rr~r rrrrpropertyrr __classcell__)rs@rrrosAEP"(AtFCTJ *k!:;< ,o> ? D5' " 56A4Lh4?@sD89&$fe_56'($D$)  )4Vp5<6<(56(-@2))r r)&rrOnumbersrrnumpyrscipyrbaserrr r exceptionsr utilsr r rutils._param_validationrrrrutils.validationrr__all__rr.rJrTr_rcrfrrr rrs" ,CCTT= i  2 A FD !*^#%;#  #(@    @@Fu-/?ur