U
    9¼|eUh  ã                   @   s  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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$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)ÚIntegralÚReal)Úlinalgé   )ÚBaseEstimatorÚTransformerMixinÚClassNamePrefixFeaturesOutMixin)ÚConvergenceWarning)Úcheck_arrayÚas_float_arrayÚcheck_random_state)Úcheck_is_fitted)ÚHiddenÚIntervalÚ
StrOptionsÚ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   ú[/var/www/website-v5/atlas_env/lib/python3.8/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_decorrelation4   s    r(   c                 C   s  |j d }tj||f| jd�}g }t|ƒD ]Ü}	||	dd…f  ¡ }
|
t |
d  ¡ ¡ }
t|ƒD ]†}|t |
j	| ¡|ƒ\}}| | j
dd�| 
¡ |
  }t|||	ƒ |t |d  ¡ ¡ }t t ||
  ¡ ¡d ¡}|}
||k r` qèq`| |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_defC   s$    
rC   c              	   C   s²   t |ƒ}~t| jd ƒ}t|ƒD ]x}|t || ¡|ƒ\}	}
t t |	| j¡| |
dd…tjf |  ƒ}~	~
tt	t	t 
d||¡ƒd ƒƒ}|}||k r  q¦q t dt¡ ||d 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(   Úfloatr-   r/   r   r    r   Únewaxisr5   r3   ÚeinsumÚwarningsÚwarnr	   )r6   r7   r8   r9   r:   r;   r   Úp_Úiir?   r@   ÚW1rB   r   r   r   Ú_ica_parf   s     ,ýrL   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#   Ú	enumerater2   )Úxr9   rM   ÚgxÚg_xr>   Zgx_ir   r   r   Ú_logcosh†   s    rU   c                 C   s<   t  | d  d ¡}| | }d| d  | }||jdd�fS )Nr   r*   éÿÿÿÿr+   )r   Úexpr2   )rR   r9   rW   rS   rT   r   r   r   Ú_exp’   s    rX   c                 C   s   | d d| d  j dd�fS )Né   r   rV   r+   )r2   ©rR   r9   r   r   r   Ú_cube™   s    r[   ÚparallelrH   ÚlogcoshéÈ   ç-Cëâ6?ÚsvdFT)Ú	algorithmÚwhitenÚfunr9   r:   r7   r;   Úwhiten_solverÚrandom_stateÚreturn_X_meanÚcompute_sourcesÚreturn_n_iterc                C   sx   t |||||||||	|
d�
}|j| |d�}|jdkrB|j}|j}nd}d}||j|g}|rd| |¡ |rt| |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<   ra   rb   rc   r9   r:   r7   r;   rd   re   ©rg   )úunit-varianceúarbitrary-varianceN)r   Ú_fit_transformÚ_whitenÚ
whitening_Úmean_Ú	_unmixingr4   Ún_iter_)r6   r<   ra   rb   rc   r9   r:   r7   r;   rd   re   rf   rg   rh   ÚestÚSÚKÚX_meanZreturned_valuesr   r   r   r   �   s2     ö

c                       s  e Zd ZU dZeedddd�dgeddhƒgeedhƒƒed	d
hƒdgedddhƒege	dgeedddd�gee
d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d„Zd,dd„Zd-dd„Zd.d d!„Zd/d#d$„Zd0d%d&„Zed'd(„ ƒZd)d*„ Z‡  ZS )1r   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)Úclosedr\   Ú	deflationrH   rl   rk   Úbooleanr]   rW   Úcubeg        z
array-liker   r`   re   ri   Ú_parameter_constraintsr^   r_   )	ra   rb   rc   r9   r:   r7   r;   rd   re   c       	            sJ   t ƒ  ¡  || _|| _|| _|| _|| _|| _|| _|| _	|	| _
|
| _d S ©N)ÚsuperÚ__init__r<   ra   rb   rc   r9   r:   r7   r;   rd   re   )Úselfr<   ra   rb   rc   r9   r:   r7   r;   rd   re   ©Ú	__class__r   r   r   ï  s    
zFastICA.__init__Fc                    s  ˆ j ˆ _ˆ jdkr$t dt¡ dˆ _ˆ jdkrDtjdtdd� dˆ _ˆ j|ˆ jtjtjgdd�j	}ˆ j
d	krpi nˆ j
}tˆ jƒ}| d
d¡}d|  kr dksªn tdƒ‚ˆ jdkrºt}n6ˆ jdkrÊt}n&ˆ jdkrÚt}ntˆ jƒrð‡ fdd„}|j\}}	ˆ j}
ˆ j�s |
d	k	�r d	}
t d¡ |
d	k�r4t|	|ƒ}
|
t|	|ƒk�r\t|	|ƒ}
t d|
 ¡ ˆ j�r„|jdd�}||d	d	…tjf 8 }ˆ jdk�rt | |¡¡\}}t |¡d	d	d… }t |j¡j }||k }t !|¡�ræt d¡ |||< tj"||d� || |d	d	…|f  }}n(ˆ jdk�r@tj#|ddd�d	d… \}}|t $|d ¡9 }|| j	d	|
… }~~t ||¡}|t "|	¡9 }nt%|dd�}ˆ j&}|d	k�r¾tj'|j(|
|
fd�|jd �}n.t '|¡}|j|
|
fk�rìtd!d"|
|
fi ƒ‚ˆ j)||ˆ j*|d#œ}ˆ j+d$k�rt,|f|Ž\}}nˆ j+d%k�r:t-|f|Ž\}}~|ˆ _.|�rvˆ j�rftj /|||g¡j	}nt ||¡j	}nd	}ˆ j�ræˆ jd&k�rÊ|�s¨tj /|||g¡j	}tj0|ddd'�}|| }||j	 }t ||¡ˆ _1|ˆ _2|ˆ _3n|ˆ _1tj4ˆ j1d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`.
        rH   zAStarting in v1.3, whiten='unit-variance' will be used by default.rl   Tz®Starting 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)r0   r#   Úensure_min_samplesNrM   r   r*   zalpha must be in [1,2]r]   rW   r{   c                    s   ˆ j | f|ŽS r}   )rc   rZ   ©r€   r   r   r8   @  s    z!FastICA._fit_transform.<locals>.gz(Ignoring n_components with whiten=False.z/n_components is too large: it will be set to %srV   r+   r   zfThere are some small singular values, using whiten_solver = 'svd' might lead to more accurate results.)Úoutr`   F)Úfull_matricesÚcheck_finiter   )r0   )Úsizer)   z/w_init has invalid shape -- should be %(shape)sr-   )r7   r8   r9   r:   r;   r\   ry   rk   )r,   Úkeepdims)rˆ   )7rb   rn   rG   rH   ÚFutureWarningÚ_validate_datar   Úfloat64Úfloat32r   r9   r   re   rN   Ú
ValueErrorrc   rU   rX   r[   Úcallabler-   r<   Úminr2   rE   rd   r   r   r    Úargsortr"   r#   ÚepsÚanyr%   r`   Úsignr   r;   ÚasarrayÚnormalr7   r:   ra   rL   rC   rr   r   ÚstdÚcomponents_rp   ro   ÚpinvÚmixing_rq   )r€   r6   rg   ZXTr9   re   rM   r8   Ú
n_featuresÚ	n_samplesr<   rv   Údr'   Úsort_indicesr“   Zdegenerate_idxru   ÚX1r;   Úkwargsr   r=   rt   ZS_stdr   r…   r   rm   	  sà    
þ
û  
 ÿ









ÿÿ
 ÿ

ÿÿû
zFastICA._fit_transformc                 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.
        Trj   ©Ú_validate_paramsrm   ©r€   r6   Ú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.
        Frj   r¢   r¤   r   r   r   ÚfitÆ  s    zFastICA.fitTc                 C   sH   t | ƒ | j||o| jtjtjgdd�}| jr8|| 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)r0   r#   Úreset)
r   rŒ   rn   r   r�   rŽ   rp   r    r™   r   ©r€   r6   r0   r   r   r   Ú	transformÜ  s      
 ÿ
zFastICA.transformc                 C   sH   t | ƒ t||o| jtjtjgd�}t || jj¡}| jrD|| 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.
        )r0   r#   )
r   r
   rn   r   r�   rŽ   r    r›   r   rp   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-   r…   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�   r…   r   r   r   Ú
_more_tags  s    zFastICA._more_tags)N)F)N)N)T)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r�   Údictr   r|   Ú__annotations__r   rm   r¦   r§   rª   r«   Úpropertyr¬   r®   Ú__classcell__r   r   r�   r   r   T  sJ   
 
ýò þô
 (




)N)N)#r²   rG   Únumbersr   r   Únumpyr   Úscipyr   Úbaser   r   r   Ú
exceptionsr	   Úutilsr
   r   r   Úutils.validationr   Úutils._param_validationr   r   r   Ú__all__r   r(   rC   rL   rU   rX   r[   r   r   r   r   r   r   Ú<module>   sD   # 
 þñ 8