o
    tBh^                     @   s   d Z ddl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
gZdd Zdd Zdd Zdd Zd#ddZdd Zdd Z	d#dddddddddddd d!d	ZG d"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)linalg   )BaseEstimatorTransformerMixin _ClassNamePrefixFeaturesOutMixin)ConvergenceWarning)check_arrayas_float_arraycheck_random_state)check_is_fittedfasticaFastICAc                 C   s,   | t j| |d| j|d| g8 } | 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j r   u/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/sklearn/decomposition/_fastica.py_gs_decorrelation   s   (r   c                 C   sT   t t| | j\}}tj|t| jjdd}tj 	|dt
|  |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ur   r   r   _sym_decorrelation3   s   "r#   c                 C   s  |j d }tj||f| jd}g }t|D ]n}	||	ddf  }
|
t|
d   }
t|D ]C}|t|
j	| |\}}| | j
dd|
 |
  }t|||	 |t|d   }tt||
  d }|}
||k rs nq0||d  |
||	ddf< q|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limr   r   r   _ica_defB   s&   
r>   c              
   C   s   t |}~t| jd }t|D ]?}|t|| |\}	}
t t|	| j| |
ddtjf |  }~	~
tt	t	t
t||jd }|}||k rO nqtdt ||d fS )zCParallel FastICA.

    Used internally by FastICA --main loop

    r%   Nz\FastICA did not converge. Consider increasing tolerance or the maximum number of iterations.)r#   floatr(   r*   r   r   r   newaxisr0   r.   diagwarningswarnr   )r1   r2   r3   r4   r5   r6   r   p_iir:   r;   W1r=   r   r   r   _ica_pare   s"   ,$rG   c                 C   sh   | dd}| |9 } t| | }tj| jd | jd}t|D ]\}}|d|d    ||< q||fS )Nalphar   r   r$   r%   r   )getr   tanhemptyr(   r   	enumerater-   )xr4   rH   gxg_xr9   gx_ir   r   r   _logcosh   s   rQ   c                 C   s<   t | d  d }| | }d| d  | }||jddfS )Nr   r%   r&   )r   expr-   )rM   r4   rS   rN   rO   r   r   r   _exp   s   rT   c                 C   s   | d d| d  j ddfS )N   r   rR   r&   )r-   rM   r4   r   r   r   _cube   s   rW   parallelrC   logcosh   -C6?FT)	algorithmwhitenfunr4   r5   r2   r6   random_statereturn_X_meancompute_sourcesreturn_n_iterc                C   sv   t |||||||||	d	}|j| |d}|jdv r |j}|j}nd}d}||j|g}|
r1|| |r9||j |S )a  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 extract. If None no dimension reduction
        is performed.

    algorithm : {'parallel', 'deflation'}, default='parallel'
        Apply a parallel or deflational FASTICA algorithm.

    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
            From version 1.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, np.mean(3 * x ** 2, 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-04
        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 of dimension (n.comp,n.comp).
        If None (default) then an array of normal r.v.'s is used.

    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.
    )	r7   r\   r]   r^   r4   r5   r2   r6   r_   ra   )unit-variancearbitrary-varianceN)r   _fit_whiten
whitening_mean_	_unmixingr/   n_iter_)r1   r7   r\   r]   r^   r4   r5   r2   r6   r_   r`   ra   rb   estSKX_meanreturned_valuesr   r   r   r      s0    

c                
       s~   e Zd ZdZ	dddddddddd fd	d
ZdddZdddZdddZdddZdddZ	e
dd Zdd Z  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'
        Apply parallel or deflational algorithm 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
            From version 1.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. 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 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
        Tolerance on update at each iteration.

    w_init : ndarray of shape (n_components, n_components), default=None
        The mixing matrix to be used to initialize the algorithm.

    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)
    NrX   rC   rY   rZ   r[   )r\   r]   r^   r4   r5   r2   r6   r_   c          
         sD   t    || _|| _|| _|| _|| _|| _|| _|| _	|	| _
d S N)super__init__r7   r\   r]   r^   r4   r5   r2   r6   r_   )
selfr7   r\   r]   r^   r4   r5   r2   r6   r_   	__class__r   r   rs     s   

zFastICA.__init__Fc                    s   j  _ jdkrtdt d _ jdu r"tjdtdd d _ j| jtjtjgddj	} j
d	u r8i n j
}t j}|d
d}d|  krSdksXtd td jdkr`t}n- jdkrht}n% jdkrpt}nt jr| fdd}nt jtrtnt}|d j |j\}	}
 j} js|d	urd	}td |d	u rt|
|	}|t|
|	krt|
|	}td|   jr|jdd}||d	d	tjf 8 }tj|ddd\}}}~|| j	d	| }~~t||}|t|
9 }nt |dd} j!}|d	u rtj"|j#||fd|j$d}nt"|}|j||fkr,tdd||fi  j%dk r:td& j% j'|| j%|d } j(d!krUt)|fi |\}}n j(d"krft*|fi |\}}ntd#~| _+|r jrtj,|||gj	}n
t||j	}nd	} jr jd$kr|stj,|||gj	}tj-|d%dd&}|| }||j	 }t|| _.| _/| _0n| _.tj1 j.dd' _2| _3|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`.
        rC   z@From version 1.3 whiten='unit-variance' will be used by default.re   TzFrom version 1.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)r+   r   ensure_min_samplesNrH   r   r%   zalpha must be in [1,2]rY   rS   cubec                    s    j | fi |S rq   )r^   rV   rt   r   r   r3   
  s   zFastICA._fit.<locals>.gzJUnknown function %r; should be one of 'logcosh', 'exp', 'cube' or callablez(Ignoring n_components with whiten=False.z/n_components is too large: it will be set to %srR   r&   F)full_matricescheck_finite)r+   )sizer$   z/w_init has invalid shape -- should be %(shape)sr(   z4max_iter should be greater than 1, got (max_iter={}))r2   r3   r4   r5   r6   rX   	deflationz<Invalid algorithm: must be either `parallel` or `deflation`.rd   r   )r'   keepdims)r|   )4r]   rg   rB   rC   FutureWarning_validate_datar   float64float32r   r4   r
   r_   rI   
ValueErrorr^   rQ   rT   rW   callable
isinstancestr	TypeErrorr(   r7   minr-   r@   r   svdr   r    r	   r6   asarraynormalr   r5   formatr2   r\   rG   r>   rk   r   stdcomponents_ri   rh   pinvmixing_rj   )rt   r1   ra   XTr4   r_   rH   r3   exc
n_features	n_samplesr7   ro   r"   d_rn   X1r6   kwargsr   r8   rm   S_stdr   rz   r   rf     s   














zFastICA._fitc                 C   s   | j |d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.
        Trc   rf   rt   r1   yr   r   r   fit_transform|  s   zFastICA.fit_transformc                 C   s   | j |d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.
        Frc   r   r   r   r   r   fit  s   zFastICA.fitTc                 C   sH   t |  | j||o| jtjtjgdd}| jr|| j8 }t|| 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)r+   r   reset)
r   r   rg   r   r   r   ri   r   r   r   rt   r1   r+   r   r   r   	transform  s   
zFastICA.transformc                 C   sH   t |  t||o
| jtjtjgd}t|| jj}| jr"|| j	7 }|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.
        )r+   r   )
r   r   rg   r   r   r   r   r   r   ri   r   r   r   r   inverse_transform  s   
zFastICA.inverse_transformc                 C   s   | j jd S )z&Number of transformed output features.r   )r   r(   rz   r   r   r   _n_features_out  s   zFastICA._n_features_outc                 C   s   dt jt jgiS )Npreserves_dtype)r   r   r   rz   r   r   r   
_more_tags  s   zFastICA._more_tagsrq   )F)T)__name__
__module____qualname____doc__rs   rf   r   r   r   r   propertyr   r   __classcell__r   r   ru   r   r   B  s,    z
 
*



rq   )r   rB   numpyr   scipyr   baser   r   r   
exceptionsr   utilsr   r	   r
   utils.validationr   __all__r   r#   r>   rG   rQ   rT   rW   r   r   r   r   r   r   <module>   s@    #
 )