ó
‡ˆ\c           @   sX  d  Z  d d l Z d d l Z d d l m Z d d l m Z m Z d d l	 m
 Z
 d d l m Z d d l 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 d d g Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d d e  d d d d d d e! e  e! d „ Z" d e e f d „  ƒ  YZ# d S(   s   
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.
iÿÿÿÿN(   t   linalgi   (   t   BaseEstimatort   TransformerMixin(   t   ConvergenceWarning(   t   six(   t   moves(   t   string_types(   t   check_arrayt   as_float_arrayt   check_random_state(   t   check_is_fitted(   t   FLOAT_DTYPESt   fasticat   FastICAc         C   s1   |  t  j t  j |  | |  j ƒ | |  ƒ 8}  |  S(   s  
    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
    (   t   npt   dott   T(   t   wt   Wt   j(    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _gs_decorrelation   s    -c         C   sT   t  j t j |  |  j ƒ ƒ \ } } t j t j | d t j | ƒ | j ƒ |  ƒ S(   sA    Symmetric decorrelation
    i.e. W <- (W * W.T) ^{-1/2} * W
    g      ð?(   R    t   eighR   R   R   t   sqrt(   R   t   st   u(    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _sym_decorrelation6   s    $c         C   sƒ  | j  d } t j | | f d |  j ƒ} g  } x?t | ƒ D]1}	 | |	 d d … f j ƒ  }
 |
 t j |
 d j ƒ  ƒ :}
 xÈ t j	 | ƒ D]· } | t j
 |
 j |  ƒ | ƒ \ } } |  | j d d ƒ | j ƒ  |
 } t | | |	 ƒ | t j | d j ƒ  ƒ :} t j t j | |
 j ƒ  ƒ d ƒ } | }
 | | k  r Pq q W| j | d ƒ |
 | |	 d d … f <q> W| t | ƒ f S(   sc   Deflationary FastICA using fun approx to neg-entropy function

    Used internally by FastICA.
    i    t   dtypeNi   t   axisi   (   t   shapeR   t   zerosR   t   ranget   copyR   t   sumR   t   xrangeR   R   t   meanR   t   abst   appendt   max(   t   Xt   tolt   gt   fun_argst   max_itert   w_initt   n_componentsR   t   n_iterR   R   t   it   gwtxt   g_wtxt   w1t   lim(    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _ica_def@   s$    $$&c         C   s  t  | ƒ } ~ t |  j d ƒ } xÒ t j | ƒ D]± } | t j | |  ƒ | ƒ \ }	 }
 t  t j |	 |  j ƒ | |
 d d … t j f | ƒ } ~	 ~
 t	 t
 t
 t j t j | | j ƒ ƒ ƒ d ƒ ƒ } | } | | k  r2 Pq2 q2 Wt j d t ƒ | | d f S(   sC   Parallel FastICA.

    Used internally by FastICA --main loop

    i   Ns\   FastICA did not converge. Consider increasing tolerance or the maximum number of iterations.(   R   t   floatR   R   R!   R   R   R   t   newaxisR%   R#   t   diagt   warningst   warnR   (   R&   R'   R(   R)   R*   R+   R   t   p_t   iiR/   R0   t   W1R2   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _ica_parc   s    !!4	c         C   s‡   | j  d d ƒ } |  | 9}  t j |  |  ƒ } t j |  j d ƒ } x6 t | ƒ D]( \ } } | d | d j ƒ  | | <qQ W| | f S(   Nt   alphag      ð?i    i   i   (   t   getR   t   tanht   emptyR   t	   enumerateR"   (   t   xR)   R=   t   gxt   g_xR.   t   gx_i(    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _logcosh€   s    
 c         C   sJ   t  j |  d d ƒ } |  | } d |  d | } | | j d d ƒ f S(   Ni   i   R   iÿÿÿÿ(   R   t   expR"   (   RB   R)   RG   RC   RD   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _expŒ   s    
c         C   s"   |  d d |  d j  d d ƒ f S(   Ni   i   R   iÿÿÿÿ(   R"   (   RB   R)   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _cube“   s    t   parallelt   logcoshiÈ   g-Cëâ6?c            sJ  t  |	 ƒ }	 | d k r i  n | } t |  d | d t d d ƒj }  | j d d ƒ } d | k on d k n s‚ t d ƒ ‚ n  ˆ  d	 k r— t } nv ˆ  d
 k r¬ t } na ˆ  d k rÁ t	 } nL t
 ˆ  ƒ rß ‡  f d †  } n. t ˆ  t j ƒ r÷ t n t } | d ˆ  ƒ ‚ |  j \ } } | rE| d k	 rEd } t j d ƒ n  | d k rct | | ƒ } n  | t | | ƒ k r›t | | ƒ } t j d | ƒ n  | r0|  j d d ƒ } |  | d d … t j f 8}  t j |  d t ƒ\ } } } ~ | | j |  } ~ ~ t j | |  ƒ } | t j | ƒ 9} n t |  d t ƒ} | d k r{t j |	 j d | | f ƒ d | j ƒ} nD t j | ƒ } | j | | f k r¿t d i | | f d 6ƒ ‚ n  i | d 6| d 6| d 6| d 6| d 6} | d k rt | |  \ } } n0 | d k r0t  | |  \ } } n t d ƒ ‚ ~ | rÍ| rot j t j | | ƒ |  ƒ j } n d } |
 r§| r”| | | | | f S| | | | f SqF| r½| | | | f S| | | f Sny | rët j | |  ƒ j } n d } |
 r#| rd | | d | f Sd | | d f Sn# | r9d | | | f Sd | | f Sd S(   sþ  Perform Fast Independent Component Analysis.

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

    Parameters
    ----------
    X : array-like, 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, optional
        Number of components to extract. If None no dimension reduction
        is performed.

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

    whiten : boolean, optional
        If True perform an initial whitening of the data.
        If False, the data is assumed to have already been
        preprocessed: it should be centered, normed and white.
        Otherwise you will get incorrect results.
        In this case the parameter n_components will be ignored.

    fun : string or function, optional. 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 : dictionary, optional
        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, optional
        Maximum number of iterations to perform.

    tol : float, optional
        A positive scalar giving the tolerance at which the
        un-mixing matrix is considered to have converged.

    w_init : (n_components, n_components) array, optional
        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, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    return_X_mean : bool, optional
        If True, X_mean is returned too.

    compute_sources : bool, optional
        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, optional
        Whether or not to return the number of iterations.

    Returns
    -------
    K : array, shape (n_components, n_features) | 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 : array, shape (n_components, n_components)
        Estimated un-mixing matrix.
        The mixing matrix can be obtained by::

            w = np.dot(W, K.T)
            A = w.T * (w * w.T).I

    S : array, shape (n_samples, n_components) | None
        Estimated source matrix

    X_mean : array, 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.``

    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.

    Implemented using FastICA:
    `A. Hyvarinen and E. Oja, Independent Component Analysis:
    Algorithms and Applications, Neural Networks, 13(4-5), 2000,
    pp. 411-430`

    R   R   t   ensure_min_samplesi   R=   g      ð?i   s   alpha must be in [1,2]RK   RG   t   cubec            s   ˆ  |  |  S(   N(    (   RB   R)   (   t   fun(    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyR(     s    sJ   Unknown function %r; should be one of 'logcosh', 'exp', 'cube' or callables(   Ignoring n_components with whiten=False.s/   n_components is too large: it will be set to %sR   iÿÿÿÿNt   full_matricest   sizes/   w_init has invalid shape -- should be %(shape)sR   R'   R(   R)   R*   R+   RJ   t	   deflations<   Invalid algorithm: must be either `parallel` or `deflation`.(!   R	   t   NoneR   R   R   R>   t
   ValueErrorRF   RH   RI   t   callablet
   isinstanceR   R   t	   TypeErrorR   R7   R8   t   minR"   R   R5   R    t   svdt   FalseR   R   R   t   asarrayt   normalR   R<   R3   (   R&   R,   t	   algorithmt   whitenRN   R)   R*   R'   R+   t   random_statet   return_X_meant   compute_sourcest   return_n_iterR=   R(   t   exct   nt   pt   X_meanR   t   dt   _t   Kt   X1t   kwargsR   R-   t   S(    (   RN   s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyR   —   s˜    v			


$c        
   B   sq   e  Z d  Z d d e d d d d d d d „	 Z e d „ Z d d „ Z d d „ Z	 d	 e d
 „ Z
 e d „ Z RS(   sŠ
  FastICA: a fast algorithm for Independent Component Analysis.

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

    Parameters
    ----------
    n_components : int, optional
        Number of components to use. If none is passed, all are used.

    algorithm : {'parallel', 'deflation'}
        Apply parallel or deflational algorithm for FastICA.

    whiten : boolean, optional
        If whiten is false, the data is already considered to be
        whitened, and no whitening is performed.

    fun : string or function, optional. 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 : dictionary, optional
        Arguments to send to the functional form.
        If empty and if fun='logcosh', fun_args will take value
        {'alpha' : 1.0}.

    max_iter : int, optional
        Maximum number of iterations during fit.

    tol : float, optional
        Tolerance on update at each iteration.

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

    random_state : int, RandomState instance or None, optional (default=None)
        If int, random_state is the seed used by the random number generator;
        If RandomState instance, random_state is the random number generator;
        If None, the random number generator is the RandomState instance used
        by `np.random`.

    Attributes
    ----------
    components_ : 2D array, shape (n_components, n_features)
        The unmixing matrix.

    mixing_ : array, shape (n_features, n_components)
        The mixing matrix.

    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.

    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)
    >>> X_transformed = transformer.fit_transform(X)
    >>> X_transformed.shape
    (1797, 7)

    Notes
    -----
    Implementation based on
    `A. Hyvarinen and E. Oja, Independent Component Analysis:
    Algorithms and Applications, Neural Networks, 13(4-5), 2000,
    pp. 411-430`

    RJ   RK   iÈ   g-Cëâ6?c
   
      C   sŒ   t  t |  ƒ j ƒ  | d k  r7 t d j | ƒ ƒ ‚ n  | |  _ | |  _ | |  _ | |  _ | |  _	 | |  _
 | |  _ | |  _ |	 |  _ d  S(   Ni   s4   max_iter should be greater than 1, got (max_iter={})(   t   superR   t   __init__RS   t   formatR,   R\   R]   RN   R)   R*   R'   R+   R^   (
   t   selfR,   R\   R]   RN   R)   R*   R'   R+   R^   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyRm   Ñ  s    									c         C   s  |  j  d k r i  n |  j  } t d | d |  j d |  j d |  j d |  j d | d |  j d |  j d	 |  j	 d
 |  j
 d t d | d t ƒ \ } } } } |  _ |  j rÒ t j | | ƒ |  _ | |  _ | |  _ n	 | |  _ t j |  j ƒ |  _ | r| |  _ n  | S(   s  Fit the model

        Parameters
        ----------
        X : array-like, 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
            If False, sources are not computes but only the rotation matrix.
            This can save memory when working with big data. Defaults to False.

        Returns
        -------
            X_new : array-like, shape (n_samples, n_components)
        R&   R,   R\   R]   RN   R)   R*   R'   R+   R^   R_   R`   Ra   N(   R)   RR   R   R,   R\   R]   RN   R*   R'   R+   R^   t   Truet   n_iter_R   R   t   components_t   mean_t
   whitening_R    t   pinvt   mixing_t   _FastICA__sources(   Ro   R&   R`   R)   t	   whiteningt   unmixingt   sourcesRe   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   _fitâ  s     !			c         C   s   |  j  | d t ƒS(   s  Fit the model and recover the sources from X.

        Parameters
        ----------
        X : array-like, 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

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
        R`   (   R{   Rp   (   Ro   R&   t   y(    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   fit_transform	  s    c         C   s   |  j  | d t ƒ|  S(   s6  Fit the model to X.

        Parameters
        ----------
        X : array-like, 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

        Returns
        -------
        self
        R`   (   R{   RY   (   Ro   R&   R|   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   fit  s    t
   deprecatedc         C   sƒ   t  | t ƒ s | d k r/ t j d t ƒ n  t |  d ƒ t | d | d t ƒ} |  j rm | |  j	 8} n  t
 j | |  j j ƒ S(   sQ  Recover the sources from X (apply the unmixing matrix).

        Parameters
        ----------
        X : array-like, shape (n_samples, n_features)
            Data to transform, where n_samples is the number of samples
            and n_features is the number of features.
        y : (ignored)
            .. deprecated:: 0.19
               This parameter will be removed in 0.21.
        copy : bool (optional)
            If False, data passed to fit are overwritten. Defaults to True.

        Returns
        -------
        X_new : array-like, shape (n_samples, n_components)
        R   sS   The parameter y on transform() is deprecated since 0.19 and will be removed in 0.21Rv   R   R   (   RU   R   R7   R8   t   DeprecationWarningR
   R   R   R]   Rs   R   R   Rr   R   (   Ro   R&   R|   R   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt	   transform,  s    	
	c         C   sc   t  |  d ƒ t | d | o" |  j d t ƒ} t j | |  j j ƒ } |  j r_ | |  j 7} n  | S(   sé  Transform the sources back to the mixed data (apply mixing matrix).

        Parameters
        ----------
        X : array-like, shape (n_samples, n_components)
            Sources, where n_samples is the number of samples
            and n_components is the number of components.
        copy : bool (optional)
            If False, data passed to fit are overwritten. Defaults to True.

        Returns
        -------
        X_new : array-like, shape (n_samples, n_features)
        Rv   R   R   (	   R
   R   R]   R   R   R   Rv   R   Rs   (   Ro   R&   R   (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   inverse_transformK  s    !	N(   t   __name__t
   __module__t   __doc__RR   Rp   Rm   RY   R{   R}   R~   R   R‚   (    (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyR     s   O	'($   R…   R7   t   numpyR   t   scipyR    t   baseR   R   t
   exceptionsR   t	   externalsR   t   externals.sixR   R   t   utilsR   R   R	   t   utils.validationR
   R   t   __all__R   R   R3   R<   RR   RF   RH   RI   Rp   RY   R   R   (    (    (    s=   lib/python2.7/site-packages/sklearn/decomposition/fastica_.pyt   <module>   s0   		
	#					ç