
     hUh                        d Z ddl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 ddlmZmZmZ dd	lmZ dd
lmZmZmZ ddgZd Zd Zd Zd ZddZd Zd Z 	 dddddddddddddddZ! G d deee
          Z"dS ) 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)IntegralReal)linalg   )BaseEstimatorTransformerMixinClassNamePrefixFeaturesOutMixin)ConvergenceWarning)check_arrayas_float_arraycheck_random_state)check_is_fitted)HiddenInterval
StrOptionsfasticaFastICAc                 z    | t           j                            | |d|         j        |d|         g          z  } | 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_dotT)wWjs      Z/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/sklearn/decomposition/_fastica.py_gs_decorrelationr      s<    * 		a2A2!BQB%0	1	11AH    c                 D   t          j        t          j        | | j                            \  }}t          j        |t          j        | j                  j        d          }t          j         	                    |dt          j
        |          z  z  |j        | g          S )z@Symmetric decorrelation
    i.e. W <- (W * W.T) ^{-1/2} * W
    N)a_mina_max      ?)r   eighr   dotr   clipfinfodtypetinyr   sqrt)r   sus      r   _sym_decorrelationr+   4   s}     ;rva~~&&DAq 	!'**/t<<<A 9S271::%5 6Q?@@@r   c                 X   |j         d         }t          j        ||f| j                  }g }t	          |          D ]]}	||	ddf                                         }
|
t          j        |
dz                                            z  }
t	          |          D ]} |t          j        |
j	        |           |          \  }}| |z  
                    d          |
                                |
z  z
  }t          |||	           |t          j        |dz                                            z  }t          j        t          j        ||
z                                            dz
            }|}
||k     r n|                    |dz              |
||	ddf<   _|t          |          fS )zcDeflationary FastICA using fun approx to neg-entropy function

    Used internally by FastICA.
    r   r&   Nr      axis)shaper   zerosr&   rangecopyr(   sumr#   r   meanr   absappendmax)Xtolgfun_argsmax_iterw_initn_componentsr   n_iterr   r   igwtxg_wtxw1lims                   r   _ica_defrG   C   s    <?L
,-QW===AF <    1aaa4L	RWadZZ\\"""x 	 	A!BF13NNH55KD%d(a((5::<<!+;;Bb!Q'''"'2q5++--(((B&a//!344CASyy  	a!e!QQQ$c&kk>r   c                     t          |          }~t          | j        d                   }t          |          D ]} |t	          j        ||           |          \  }	}
t          t	          j        |	| j                  |z  |
ddt          j        f         |z  z
            }~	~
t          t          t          t	          j
        d||                    dz
                      }|}||k     r nt          j        dt                     ||dz   fS )zCParallel FastICA.

    Used internally by FastICA --main loop

    r.   Nzij,ij->iz\FastICA did not converge. Consider increasing tolerance or the maximum number of iterations.)r+   floatr1   r3   r   r#   r   newaxisr9   r7   einsumwarningswarnr
   )r:   r;   r<   r=   r>   r?   r   p_iirC   rD   W1rF   s                r   _ica_parrQ   f   s    	6""A	qwqz		BHoo 
 
aq!h//etQS 1 1B 6qqq"*}9MPQ9Q QRR% #c")JA6677!;<<==99E  	=	
 	
 	
 b1f9r   c                     |                     dd          }| |z  } t          j        | |           }t          j        | j        d         | j                  }t          |          D ]%\  }}|d|dz  z
  z                                  ||<   &||fS )Nalphar!   r   r-   r.   r   )getr   tanhemptyr1   r&   	enumerater6   )xr=   rS   gxg_xrB   gx_is          r   _logcoshr\      s    LL#&&EJA	AB
(171:QW
-
-
-CR== 0 041tQw;'--//As7Nr   c                     t          j        | dz   dz            }| |z  }d| dz  z
  |z  }||                    d          fS )Nr   r.   r/   )r   expr6   )rX   r=   r_   rY   rZ   s        r   _expr`      sO    
&1a41

C	
SBq!t8s
CsxxRx    r   c                 D    | dz  d| dz  z                       d          fS )N   r   r^   r/   )r6   )rX   r=   s     r   _cuberc      s'    a4!ad(b))))r   parallelrM   logcosh   -C6?svdFT)	algorithmwhitenfunr=   r>   r;   r?   whiten_solverrandom_statereturn_X_meancompute_sourcesreturn_n_iterc                   t          |||||||||	|

  
        }|                    | |          }|j        dv r|j        }|j        }nd}d}||j        |g}|r|                    |           |r|                    |j                   |S )aW  Perform Fast Independent Component Analysis.

    The implementation is based on [1]_.

    Read more in the :ref:`User Guide <ICA>`.

    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="warn"
        Specify the whitening strategy to use.

        - If 'arbitrary-variance' (default), 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.

        .. deprecated:: 1.1
            Starting in v1.3, `whiten='unit-variance'` will be used by default.
            `whiten=True` is deprecated from 1.1 and will raise ValueError in 1.3.
            Use `whiten=arbitrary-variance` instead.

    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 <random_state>`.

    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.
    
r@   ri   rj   rk   r=   r>   r;   r?   rl   rm   ro   )unit-variancearbitrary-varianceN)r   _fit_transform_whiten
whitening_mean_	_unmixingr8   n_iter_)r:   r@   ri   rj   rk   r=   r>   r;   r?   rl   rm   rn   ro   rp   estSKX_meanreturned_valuess                      r   r   r      s    r !#!  C 	1o>>A
{===N#-+O 'v&&& ,s{+++r   c                       e Zd ZU dZ eeddd          dg eddh          g e edh                     ed	d
h          dg eh d          ege	dg eeddd          g ee
ddd          gddg eddh          gdgd
Ze	ed<   	 d"dddddddddd	 fdZd#dZd"dZd"dZd$dZd$dZed              Zd! Z xZS )%r   a}  FastICA: a fast algorithm for Independent Component Analysis.

    The implementation is based on [1]_.

    Read more in the :ref:`User Guide <ICA>`.

    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="warn"
        Specify the whitening strategy to use.

        - If 'arbitrary-variance' (default), 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.

        .. deprecated:: 1.1
            Starting in v1.3, `whiten='unit-variance'` will be used by default.
            `whiten=True` is deprecated from 1.1 and will raise ValueError in 1.3.
            Use `whiten=arbitrary-variance` instead.

    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 <random_state>`.

    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)
    r.   Nleft)closedrd   	deflationrM   ru   rt   boolean>   r_   cubere   g        z
array-liker"   rh   rm   rr   _parameter_constraintsre   rf   rg   )	ri   rj   rk   r=   r>   r;   r?   rl   rm   c       	             t                                                       || _        || _        || _        || _        || _        || _        || _        || _	        |	| _
        |
| _        d S N)super__init__r@   ri   rj   rk   r=   r>   r;   r?   rl   rm   )selfr@   ri   rj   rk   r=   r>   r;   r?   rl   rm   	__class__s              r   r   zFastICA.__init__  sj     	("  *(r   Fc                 d
     j          _         j        dk    r!t          j        dt                     d _         j        du r#t          j        dt          d           d _                             | j        t          j        t          j        gd          j	        } j
        i n j
        }t           j                  }|                    d
d          }d|cxk    rdk    sn t          d           j        dk    rt           }n? j        dk    rt"          }n, j        dk    rt$          }nt'           j                  r fd}|j        \  }}	 j        }
 j        s|
d	}
t          j        d           |
t-          |	|          }
|
t-          |	|          k    r't-          |	|          }
t          j        d|
z              j        r|                    d          }||d	d	t          j        f         z  } j        dk    rt5          j        |                    |                    \  }}t          j        |          d	d	d         }t          j        |j                  j         }||k     }t          j!        |          rt          j        d           |||<   t          j"        ||           ||         |d	d	|f         }}n- j        dk    r"t5          j#        |dd          d	d         \  }}|t          j$        |d                   z  }||z  j	        d	|
         }~~t          j        ||          }|t          j"        |	          z  }ntK          |d          } j&        }|2t          j'        |(                    |
|
f          |j                  }n7t          j'        |          }|j        |
|
fk    rt          d d!|
|
fiz             j)        || j*        |d"} j+        d#k    rtY          |fi |\  }}n j+        d$k    rt[          |fi |\  }}~| _.        |rJ j        r(t          j        /                    |||g          j	        }nt          j        ||          j	        }nd	} j        r j        d%k    rO|s't          j        /                    |||g          j	        }t          j0        |dd&          }||z  }||j	        z  }t          j        ||           _1        | _2        | _3        n| _1        t5          j4         j1        d'           _5        | _6        |S )(ad  Fit 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`.
        rM   zAStarting in v1.3, whiten='unit-variance' will be used by default.ru   TzStarting in v1.3, whiten=True should be specified as whiten='arbitrary-variance' (its current behaviour). This behavior is deprecated in 1.1 and will raise ValueError in 1.3.r   )
stacklevel)r4   r&   ensure_min_samplesNrS   r!   r.   zalpha must be in [1,2]re   r_   r   c                       j         | fi |S r   )rk   )rX   r=   r   s     r   r<   z!FastICA._fit_transform.<locals>.g@  s    tx..X...r   z(Ignoring n_components with whiten=False.z/n_components is too large: it will be set to %sr^   r/   r"   zfThere are some small singular values, using whiten_solver = 'svd' might lead to more accurate results.)outrh   F)full_matricescheck_finiter   )r4   )sizer-   z/w_init has invalid shape -- should be %(shape)sr1   )r;   r<   r=   r>   r?   rd   r   rt   )r0   keepdims)r   )7rj   rw   rL   rM   FutureWarning_validate_datar   float64float32r   r=   r   rm   rT   
ValueErrorrk   r\   r`   rc   callabler1   r@   minr6   rJ   rl   r   r"   r#   argsortr%   r&   epsanyr(   rh   signr   r?   asarraynormalr;   r>   ri   rQ   rG   r{   r   stdcomponents_ry   rx   pinvmixing_rz   )r   r:   ro   XTr=   rm   rS   r<   
n_features	n_samplesr@   r   dr*   sort_indicesr   degenerate_idxr~   X1r?   kwargsr   rA   r}   S_stds   `                        r   rv   zFastICA._fit_transform	  s   $ {<6!!MS   0DL<4MR     0DL  DLRZ(@UV ! 
 

 	 .22DM)$*;<<Wc**EQ56668y  AAXAAXAAdh 	// / / / / !#
I(| 	F 8LMDEEEy*55L#i4444y*55LMALP   < $	0WW"W%%F&BJ''B !V++{266!99--1!z!}}TTrT2hqw''+!"S6.)) M,  
 %(.!q!!!!!!!\/(:1#u,,z"ENNNrPQrR1 1AQ	-<-(A12B "')$$$BB  ///B>Z##,)E#FFbh  FF
 Z''F|l;;; E| <=>   8 
 
 >Z'' ..v..IAvv^{** ..v..IAv 	| $I''Ar
335F1bMMOA< 	!|..& :	++Q2J779Aqq4888U
UW!va||DDJDOO D{4#3%HHHr   c                 X    |                                   |                     |d          S )a5  Fit 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.
        Trs   _validate_paramsrv   r   r:   ys      r   fit_transformzFastICA.fit_transform  s/    $ 	""1d";;;r   c                 \    |                                   |                     |d           | S )a  Fit 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.
        Frs   r   r   s      r   fitzFastICA.fit  s4    " 	Au555r   Tc                     t          |            |                     ||o| j        t          j        t          j        gd          }| j        r
|| j        z  }t          j        || j        j	                  S )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)r4   r&   reset)
r   r   rw   r   r   r   ry   r#   r   r   r   r:   r4   s      r   	transformzFastICA.transform  su    $ 	T*dlBJ
3KSX   
 
 < 	OAva)+,,,r   c                     t          |            t          ||o| j        t          j        t          j        g          }t          j        || j        j                  }| j        r
|| j	        z  }|S )a1  Transform 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_new : ndarray of shape (n_samples, n_features)
            Reconstructed data obtained with the mixing matrix.
        )r4   r&   )
r   r   rw   r   r   r   r#   r   r   ry   r   s      r   inverse_transformzFastICA.inverse_transform  se      	!6$,
BJ?WXXXF1dln%%< 	OAr   c                 &    | j         j        d         S )z&Number of transformed output features.r   )r   r1   r   s    r   _n_features_outzFastICA._n_features_out  s     %a((r   c                 6    dt           j        t           j        giS )Npreserves_dtype)r   r   r   r   s    r   
_more_tagszFastICA._more_tags  s    !BJ
#;<<r   r   )F)T)__name__
__module____qualname____doc__r   r   r   r   r   dictr   r   __annotations__r   rv   r   r   r   r   propertyr   r   __classcell__)r   s   @r   r   r   T  s        G GT "(AtFCCCTJ j*k!:;;<F::vh''((J,o>??

 
55566A4LXh4???@sD8889&$*fe_556'($ $D   & ) ) ) ) ) ) ) )4e e e eN< < < <,   ,- - - -8   2 ) ) X)= = = = = = =r   r   )#r   rL   numbersr   r   numpyr   scipyr   baser   r   r	   
exceptionsr
   utilsr   r   r   utils.validationr   utils._param_validationr   r   r   __all__r   r+   rG   rQ   r\   r`   rc   r   r    r   r   <module>r      s     " " " " " " " "           S S S S S S S S S S + + + + + + C C C C C C C C C C . . . . . . B B B B B B B B B Bi
   2A A A     F  @	 	 	 	! ! !* * * t t t t t tnC= C= C= C= C=-/? C= C= C= C= C=r   