
\c           @  sB  d  Z  d d l m Z m Z d d l m Z d d l Z d d l Z d d l Z d d l	 Z
 d d l j Z d d l m Z m Z d d l m Z m Z d d l m Z m Z m Z d d	 l m Z d d
 l m Z m Z d d l m Z d d l m Z e
 j  e
 j!  j" Z# e j$ e
 j% f Z& d   Z' d   Z( d   Z) e* d  Z+ d   Z, d   Z- d   Z. d   Z/ d d d d  Z1 d   Z2 d d d d d d e3 d e* d d 
 Z4 d d d e3 d  Z5 d   Z6 d d d d d d d e3 d d  	 Z7 d d d d! e3 d" d d d d# d# d d d e* d$  Z8 d% e e f d&     YZ9 d S('   s#    Non-negative matrix factorization
i(   t   divisiont   print_function(   t   sqrtNi   (   t   BaseEstimatort   TransformerMixin(   t   check_random_statet   check_array(   t   randomized_svdt   safe_sparse_dott   squared_norm(   t   safe_min(   t   check_is_fittedt   check_non_negative(   t   ConvergenceWarningi   (   t   _update_cdnmf_fastc         C  s   t  t |    S(   s   Dot product-based Euclidean norm implementation

    See: http://fseoane.net/blog/2011/computing-the-vector-norm/

    Parameters
    ----------
    x : array-like
        Vector for which to compute the norm
    (   R   R	   (   t   x(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   norm!   s    
c         C  s   t  j |  j   | j    S(   s   Trace of np.dot(X, Y.T).

    Parameters
    ----------
    X : array-like
        First matrix
    Y : array-like
        Second matrix
    (   t   npt   dott   ravel(   t   Xt   Y(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt	   trace_dot.   s    
c         C  s   t  |   }  t j |   | k rF t d | | t j |   f   n  t |  |  t j |   d k r{ t d |   n  d  S(   Ns=   Array with wrong shape passed to %s. Expected %s, but got %s i    s$   Array passed to %s is full of zeros.(   R   R   t   shapet
   ValueErrorR   t   max(   t   AR   t   whom(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _check_init;   s    c         C  sV  t  |  } t j |   s- t j |   }  n  t j |  } t j |  } | d k rt j |   r t j |  j |  j  } t t j t j | j |  |  |  } t |  | j |  } | | d | d } n  t	 |  t j | |   d } | rt j
 | d  S| Sn  t j |   rEt | | |   j }	 |  j }
 n* t j | |  } | j   }	 |  j   }
 |
 t k } |	 | }	 |
 | }
 t |	 |	 d k <| d k rt j t j | d d t j | d d  } |
 |	 } t j |
 t j |   } | | |
 j   7} n | d k re|
 |	 } t j |  t j |  j  t j t j |   } n t j |   rd } xd t |  j d  D]9 } | t j t j | | d d  | f  |  7} qWn t j | |  } t j |
 |	 | d  } |
 | j   | | } | | | d 7} | | | d } | rNt j
 d |  S| Sd S(   s{  Compute the beta-divergence of X and dot(W, H).

    Parameters
    ----------
    X : float or array-like, shape (n_samples, n_features)

    W : float or dense array-like, shape (n_samples, n_components)

    H : float or dense array-like, shape (n_components, n_features)

    beta : float, string in {'frobenius', 'kullback-leibler', 'itakura-saito'}
        Parameter of the beta-divergence.
        If beta == 2, this is half the Frobenius *squared* norm.
        If beta == 1, this is the generalized Kullback-Leibler divergence.
        If beta == 0, this is the Itakura-Saito divergence.
        Else, this is the general beta-divergence.

    square_root : boolean, default False
        If True, return np.sqrt(2 * res)
        For beta == 2, it corresponds to the Frobenius norm.

    Returns
    -------
        res : float
            Beta divergence of X and np.dot(X, H)
    i   g       @i    i   t   axisN(   t   _beta_loss_to_floatt   spt   issparseR   t
   atleast_2dR   t   dataR   t   TR	   R   t   _special_sparse_dotR   t   EPSILONt   sumt   logt   productR   t   range(   R   t   Wt   Ht   betat   square_roott   norm_Xt   norm_WHt
   cross_prodt   rest   WH_datat   X_datat   WHt   indicest   sum_WHt   divt   sum_WH_betat   it   sum_X_WH(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _beta_divergenceE   sZ    * 

0

8:c         C  s   t  j |  r | j   \ } } t j |  | d d  f | j | d d  f  j d d  } t  j | | | f f d | j } | j	   St j
 |  |  Sd S(   s0   Computes np.dot(W, H), only where X is non zero.NR   i   R   (   R   R    t   nonzeroR   t   multiplyR#   R&   t
   coo_matrixR   t   tocsrR   (   R*   R+   R   t   iit   jjt   dot_valsR4   (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyR$      s    A$
c   	      C  s   d } d } | d k r' t  |   } n  | d k rB t  |   } n  | | } | | } | d | } | d | } | | | | f S(   s9   Compute L1 and L2 regularization coefficients for W and Hg        t   botht
   componentst   transformationg      ?(   RC   RD   (   RC   RE   (   t   float(	   t   alphat   l1_ratiot   regularizationt   alpha_Ht   alpha_Wt   l1_reg_Wt   l1_reg_Ht   l2_reg_Wt   l2_reg_H(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _compute_regularization   s    

c         C  s   d } |  | k r+ t  d |  | f   n  d } | | k rV t  d | | f   n  |  d k r | d k r t  d
 |  | f   n  |  d k r | d k r t j d t  n  t |  } | S(   Nt   cdt   mus5   Invalid solver parameter: got %r instead of one of %rRC   RD   RE   s=   Invalid regularization parameter: got %r instead of one of %ri   t	   frobeniussE   Invalid beta_loss parameter: solver %r does not handle beta_loss = %rt   nndsvds   The multiplicative update ('mu') solver cannot update zeros present in the initialization, and so leads to poorer results when used jointly with init='nndsvd'. You may try init='nndsvda' or init='nndsvdar' instead.(   RQ   RR   (   RC   RD   RE   N(   i   RS   (   R   t   Nonet   warningst   warnt   UserWarningR   (   t   solverRI   t	   beta_losst   initt   allowed_solvert   allowed_regularization(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _check_string_param   s&    	
c         C  sx   i d d 6d d 6d d 6} t  |  t  rC |  | k rC | |  }  n  t  |  t j  st t d |  | j   f   n  |  S(   s!   Convert string beta_loss to floati   RS   i   s   kullback-leibleri    s   itakura-saitosE   Invalid beta_loss parameter: got %r instead of one of %r, or a float.(   t
   isinstancet   strt   numberst   NumberR   t   keys(   RZ   t   allowed_beta_loss(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyR      s    

gư>c         C  s'  t  |  d  |  j \ } } | d k rF | | k  r= d } qF d } n  | d k r t j |  j   |  } t |  } | | j | |  }	 | | j | |  }
 t j |	 |	  t j |
 |
  |
 |	 f St	 |  | d | \ } } } t j
 | j  t j
 | j  }
 }	 t j | d  t j | d d  d f  |
 d d  d f <t j | d  t j | d d d  f  |	 d d d  f <xut d |  D]d} | d d  | f | | d d  f } } t j | d  t j | d  } } t j t j | d   t j t j | d   } } t |  t |  } } t |  t |  } } | | | | } } | | k r| | } | | } | } n | | } | | } | } t j | | |  } | | |
 d d  | f <| | |	 | d d  f <qWd |
 |
 | k  <d |	 |	 | k  <| d k r7n | d k rr|  j   } | |
 |
 d k <| |	 |	 d k <n | d	 k rt |  } |  j   } t | | j t |
 |
 d k   d
  |
 |
 d k <t | | j t |	 |	 d k   d
  |	 |	 d k <n t d | d f   |
 |	 f S(   s  Algorithms for NMF initialization.

    Computes an initial guess for the non-negative
    rank k matrix approximation for X: X = WH

    Parameters
    ----------
    X : array-like, shape (n_samples, n_features)
        The data matrix to be decomposed.

    n_components : integer
        The number of components desired in the approximation.

    init :  None | 'random' | 'nndsvd' | 'nndsvda' | 'nndsvdar'
        Method used to initialize the procedure.
        Default: 'nndsvd' if n_components < n_features, otherwise 'random'.
        Valid options:

        - 'random': non-negative random matrices, scaled with:
            sqrt(X.mean() / n_components)

        - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD)
            initialization (better for sparseness)

        - 'nndsvda': NNDSVD with zeros filled with the average of X
            (better when sparsity is not desired)

        - 'nndsvdar': NNDSVD with zeros filled with small random values
            (generally faster, less accurate alternative to NNDSVDa
            for when sparsity is not desired)

        - 'custom': use custom matrices W and H

    eps : float
        Truncate all values less then this in output to zero.

    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`. Used when ``random`` == 'nndsvdar' or 'random'.

    Returns
    -------
    W : array-like, shape (n_samples, n_components)
        Initial guesses for solving X ~= WH

    H : array-like, shape (n_components, n_features)
        Initial guesses for solving X ~= WH

    References
    ----------
    C. Boutsidis, E. Gallopoulos: SVD based initialization: A head start for
    nonnegative matrix factorization - Pattern Recognition, 2008
    http://tinyurl.com/nndsvd
    s   NMF initializationRT   t   randomt   random_statei    Ni   t   nndsvdat   nndsvdarid   s3   Invalid init parameter: got %r instead of one of %r(   NRe   RT   Rg   Rh   (   R   R   RU   R   R   t   meanR   t   randnt   absR   t   zerosR)   t   maximumt   minimumR   t   lenR   (   R   t   n_componentsR[   t   epsRf   t	   n_samplest
   n_featurest   avgt   rngR+   R*   t   Ut   St   Vt   jR   t   yt   x_pt   y_pt   x_nt   y_nt   x_p_nrmt   y_p_nrmt   x_n_nrmt   y_n_nrmt   m_pt   m_nt   ut   vt   sigmat   lbd(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _initialize_nmf   sh    :		
%@@-%7

	

7:c         C  s   | j  d } t j | j |  } t |  |  }	 | d k r` | j d d | d  c | 7<n  | d k ry |	 | 8}	 n  | r | j |  }
 n t j |  }
 t j |
 d t j	 }
 t
 | | |	 |
  S(   s   Helper function for _fit_coordinate_descent

    Update W to minimize the objective function, iterating once over all
    coordinates. By symmetry, to update H, one can call
    _update_coordinate_descent(X.T, Ht, W, ...)

    i   g        Nt   dtype(   R   R   R   R#   R   t   flatt   permutationt   aranget   asarrayt   intpR   (   R   R*   t   Htt   l1_regt   l2_regt   shuffleRf   Rp   t   HHtt   XHtR   (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _update_coordinate_descent  s    	#g-C6?i   i    c      
   C  s  t  | j d d } t  |  d d }  t |  } x t |  D] } d } | t |  | | | | | |  7} |	 r | t |  j | | | | | |  7} n  | d k r | } n  | d k r Pn  |
 r t d | |  n  | | | k r@ |
 rt d | d	  n  Pq@ q@ W| | j | f S(
   s	  Compute Non-negative Matrix Factorization (NMF) with Coordinate Descent

    The objective function is minimized with an alternating minimization of W
    and H. Each minimization is done with a cyclic (up to a permutation of the
    features) Coordinate Descent.

    Parameters
    ----------
    X : array-like, shape (n_samples, n_features)
        Constant matrix.

    W : array-like, shape (n_samples, n_components)
        Initial guess for the solution.

    H : array-like, shape (n_components, n_features)
        Initial guess for the solution.

    tol : float, default: 1e-4
        Tolerance of the stopping condition.

    max_iter : integer, default: 200
        Maximum number of iterations before timing out.

    l1_reg_W : double, default: 0.
        L1 regularization parameter for W.

    l1_reg_H : double, default: 0.
        L1 regularization parameter for H.

    l2_reg_W : double, default: 0.
        L2 regularization parameter for W.

    l2_reg_H : double, default: 0.
        L2 regularization parameter for H.

    update_H : boolean, default: True
        Set to True, both W and H will be estimated from initial guesses.
        Set to False, only W will be estimated.

    verbose : integer, default: 0
        The verbosity level.

    shuffle : boolean, default: False
        If true, randomize the order of coordinates in the CD solver.

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

    Returns
    -------
    W : array-like, shape (n_samples, n_components)
        Solution to the non-negative least squares problem.

    H : array-like, shape (n_components, n_features)
        Solution to the non-negative least squares problem.

    n_iter : int
        The number of iterations done by the algorithm.

    References
    ----------
    Cichocki, Andrzej, and Phan, Anh-Huy. "Fast local algorithms for
    large scale nonnegative matrix and tensor factorizations."
    IEICE transactions on fundamentals of electronics, communications and
    computer sciences 92.3: 708-721, 2009.
    t   ordert   Ct   accept_sparset   csrg        i    s
   violation:s   Converged at iterationi   (   R   R#   R   R)   R   t   print(   R   R*   R+   t   tolt   max_iterRL   RM   RN   RO   t   update_Ht   verboseR   Rf   R   Ru   t   n_itert	   violationt   violation_init(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _fit_coordinate_descent  s*    I	c         C  s?  | d k r |	 d	 k r- t |  | j  }	 n  |
 r< |	 } n |	 j   } | d	 k rl t j | | j  } n  t j | |  } n?t | | |   } t j |   r | j	 } |  j	 } n; | } |  } | j   } | d d k  r t
 | | d k <n  | d d k  rt
 | | d k <n  | d k r:t j | | d | nE | d k rg| d C} | d C} | | 9} n | | d C} | | 9} t | | j  } | d k r| d	 k rt j | d d } n  | t j d	 d	  f } n t j |   rt j | j  } x t |  j d  D]~ } t j | | d	 d	  f |  } | d d k  r]t
 | | d k <n  | | d C} t j | | j  | | d	 d	  f <qWn# | | d C} t j | | j  } | } | d k r| | 7} n  | d k r| | | } n  t
 | | d k <| | } | } | d k r/| | C} n  | | | |	 f S(
   s%   update W in Multiplicative Update NMFi   g      ?i    g       @i   t   outiR   N(   RU   R   R#   t   copyR   R   R$   R   R    R"   R%   t   divideR&   t   newaxist   emptyR   R)   (   R   R*   R+   RZ   RL   RN   t   gammat   H_sumR   R   R   t	   numeratort   denominatort	   WH_safe_Xt   WH_safe_X_dataR3   R4   t   WHHtR9   t   WHit   delta_W(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _multiplicative_update_w
  sl    		


",
c         C  s  | d k rB t  | j |   } t j t j | j |  |  } n@t | | |   }	 t j |   rx |	 j }
 |  j } n; |	 }
 |  } |	 j   } | d d k  r t	 | | d k <n  | d d k  r t	 |
 |
 d k <n  | d k r t j
 | |
 d |
 nE | d k r(|
 d C}
 |
 d C}
 |
 | 9}
 n |
 | d C}
 |
 | 9}
 t  | j |	  } | d k rt j | d d } d | | d k <| d	 d	  t j f } n t j |   rYt j | j  } x t |  j d  D]~ } t j | | d	 d	  | f  } | d d k  rt	 | | d k <n  | | d C} t j | j |  | d	 d	  | f <qWn# | | d C} t j | j |  } | } | d k r| | 7} n  | d k r| | | } n  t	 | | d k <| | } | } | d k r| | C} n  | S(
   s%   update H in Multiplicative Update NMFi   g      ?i    g       @i   R   iR   N(   R   R#   R   R   R$   R   R    R"   R   R%   R   R&   R   R   R   R)   (   R   R*   R+   RZ   RM   RO   R   R   R   R   R   R3   R4   t   W_sumt   WtWHR9   R   t   delta_H(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _multiplicative_update_hi  s`    $	


",
RS   c         C  s`  t  j    } t |  } | d k  r5 d d | } n# | d k rR d | d } n d } t |  | | | d t } | } d \ } } } xt d | d  D]j} t |  | | | | | | | | | |
  \ } } } } | | 9} | d k  rd | | t j t j	  j
 k  <n  |
 r~t |  | | | | |	 |  } | | 9} d \ } } } | d k r~d | | t j t j	  j
 k  <q~n  | d k r | d d k r t |  | | | d t } | rt  j    } t d	 | | | | f  n  | | | | k  rPn  | } q q W| rS| d k s,| d d k rSt  j    } t d
 | | | f  n  | | | f S(   s  Compute Non-negative Matrix Factorization with Multiplicative Update

    The objective function is _beta_divergence(X, WH) and is minimized with an
    alternating minimization of W and H. Each minimization is done with a
    Multiplicative Update.

    Parameters
    ----------
    X : array-like, shape (n_samples, n_features)
        Constant input matrix.

    W : array-like, shape (n_samples, n_components)
        Initial guess for the solution.

    H : array-like, shape (n_components, n_features)
        Initial guess for the solution.

    beta_loss : float or string, default 'frobenius'
        String must be in {'frobenius', 'kullback-leibler', 'itakura-saito'}.
        Beta divergence to be minimized, measuring the distance between X
        and the dot product WH. Note that values different from 'frobenius'
        (or 2) and 'kullback-leibler' (or 1) lead to significantly slower
        fits. Note that for beta_loss <= 0 (or 'itakura-saito'), the input
        matrix X cannot contain zeros.

    max_iter : integer, default: 200
        Number of iterations.

    tol : float, default: 1e-4
        Tolerance of the stopping condition.

    l1_reg_W : double, default: 0.
        L1 regularization parameter for W.

    l1_reg_H : double, default: 0.
        L1 regularization parameter for H.

    l2_reg_W : double, default: 0.
        L2 regularization parameter for W.

    l2_reg_H : double, default: 0.
        L2 regularization parameter for H.

    update_H : boolean, default: True
        Set to True, both W and H will be estimated from initial guesses.
        Set to False, only W will be estimated.

    verbose : integer, default: 0
        The verbosity level.

    Returns
    -------
    W : array, shape (n_samples, n_components)
        Solution to the non-negative least squares problem.

    H : array, shape (n_components, n_features)
        Solution to the non-negative least squares problem.

    n_iter : int
        The number of iterations done by the algorithm.

    References
    ----------
    Fevotte, C., & Idier, J. (2011). Algorithms for nonnegative matrix
    factorization with the beta-divergence. Neural Computation, 23(9).
    i   g      ?g       @i   R-   g        i    i
   s0   Epoch %02d reached after %.3f seconds, error: %fs&   Epoch %02d reached after %.3f seconds.N(   NNN(   NNN(   t   timeR   R;   t   TrueRU   R)   R   R   t   finfot   float64Rq   R   R   (   R   R*   R+   RZ   R   R   RL   RM   RN   RO   R   R   t
   start_timeR   t   error_at_initt   previous_errorR   R   R   R   R   R   t   errort	   iter_timet   end_time(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   _fit_multiplicative_update  sL    F
"
%"Re   RQ   g        c         C  s  t  |  d d d t }  t |  d  t | | | |  } t |   d k rg | d k rg t d   n  |  j \ } } | d k r | } n  t | t	  s | d k r t d |   n  t |	 t	  s |	 d k  r t d	 |	   n  t | t
 j  s| d k  rt d
 |   n  | d k r\| r\t | | | f d  t | | | f d  n | st | | | f d  | d k rt j |  j   |  } t j | | f |  } qt j | | f  } n! t |  | d | d | \ } } t |
 | |  \ } } } } | d k rct |  | | | |	 | | | | d | d | d | d | 	\ } } } nU | d k rt |  | | | |	 | | | | | | |  \ } } } n t d |   | |	 k r| d k rt j d |	 t  n  | | | f S(   sO  Compute Non-negative Matrix Factorization (NMF)

    Find two non-negative matrices (W, H) whose product approximates the non-
    negative matrix X. This factorization can be used for example for
    dimensionality reduction, source separation or topic extraction.

    The objective function is::

        0.5 * ||X - WH||_Fro^2
        + alpha * l1_ratio * ||vec(W)||_1
        + alpha * l1_ratio * ||vec(H)||_1
        + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2
        + 0.5 * alpha * (1 - l1_ratio) * ||H||_Fro^2

    Where::

        ||A||_Fro^2 = \sum_{i,j} A_{ij}^2 (Frobenius norm)
        ||vec(A)||_1 = \sum_{i,j} abs(A_{ij}) (Elementwise L1 norm)

    For multiplicative-update ('mu') solver, the Frobenius norm
    (0.5 * ||X - WH||_Fro^2) can be changed into another beta-divergence loss,
    by changing the beta_loss parameter.

    The objective function is minimized with an alternating minimization of W
    and H. If H is given and update_H=False, it solves for W only.

    Parameters
    ----------
    X : array-like, shape (n_samples, n_features)
        Constant matrix.

    W : array-like, shape (n_samples, n_components)
        If init='custom', it is used as initial guess for the solution.

    H : array-like, shape (n_components, n_features)
        If init='custom', it is used as initial guess for the solution.
        If update_H=False, it is used as a constant, to solve for W only.

    n_components : integer
        Number of components, if n_components is not set all features
        are kept.

    init :  None | 'random' | 'nndsvd' | 'nndsvda' | 'nndsvdar' | 'custom'
        Method used to initialize the procedure.
        Default: 'random'.
        Valid options:

        - 'random': non-negative random matrices, scaled with:
            sqrt(X.mean() / n_components)

        - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD)
            initialization (better for sparseness)

        - 'nndsvda': NNDSVD with zeros filled with the average of X
            (better when sparsity is not desired)

        - 'nndsvdar': NNDSVD with zeros filled with small random values
            (generally faster, less accurate alternative to NNDSVDa
            for when sparsity is not desired)

        - 'custom': use custom matrices W and H

    update_H : boolean, default: True
        Set to True, both W and H will be estimated from initial guesses.
        Set to False, only W will be estimated.

    solver : 'cd' | 'mu'
        Numerical solver to use:
        'cd' is a Coordinate Descent solver that uses Fast Hierarchical
            Alternating Least Squares (Fast HALS).
        'mu' is a Multiplicative Update solver.

        .. versionadded:: 0.17
           Coordinate Descent solver.

        .. versionadded:: 0.19
           Multiplicative Update solver.

    beta_loss : float or string, default 'frobenius'
        String must be in {'frobenius', 'kullback-leibler', 'itakura-saito'}.
        Beta divergence to be minimized, measuring the distance between X
        and the dot product WH. Note that values different from 'frobenius'
        (or 2) and 'kullback-leibler' (or 1) lead to significantly slower
        fits. Note that for beta_loss <= 0 (or 'itakura-saito'), the input
        matrix X cannot contain zeros. Used only in 'mu' solver.

        .. versionadded:: 0.19

    tol : float, default: 1e-4
        Tolerance of the stopping condition.

    max_iter : integer, default: 200
        Maximum number of iterations before timing out.

    alpha : double, default: 0.
        Constant that multiplies the regularization terms.

    l1_ratio : double, default: 0.
        The regularization mixing parameter, with 0 <= l1_ratio <= 1.
        For l1_ratio = 0 the penalty is an elementwise L2 penalty
        (aka Frobenius Norm).
        For l1_ratio = 1 it is an elementwise L1 penalty.
        For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.

    regularization : 'both' | 'components' | 'transformation' | None
        Select whether the regularization affects the components (H), the
        transformation (W), both or none of them.

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

    verbose : integer, default: 0
        The verbosity level.

    shuffle : boolean, default: False
        If true, randomize the order of coordinates in the CD solver.

    Returns
    -------
    W : array-like, shape (n_samples, n_components)
        Solution to the non-negative least squares problem.

    H : array-like, shape (n_components, n_features)
        Solution to the non-negative least squares problem.

    n_iter : int
        Actual number of iterations.

    Examples
    --------
    >>> import numpy as np
    >>> X = np.array([[1,1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]])
    >>> from sklearn.decomposition import non_negative_factorization
    >>> W, H, n_iter = non_negative_factorization(X, n_components=2,
    ... init='random', random_state=0)

    References
    ----------
    Cichocki, Andrzej, and P. H. A. N. Anh-Huy. "Fast local algorithms for
    large scale nonnegative matrix and tensor factorizations."
    IEICE transactions on fundamentals of electronics, communications and
    computer sciences 92.3: 708-721, 2009.

    Fevotte, C., & Idier, J. (2011). Algorithms for nonnegative matrix
    factorization with the beta-divergence. Neural Computation, 23(9).
    R   R   t   cscR   s   NMF (input X)i    s|   When beta_loss <= 0 and X contains zeros, the solver may diverge. Please add small values to X, or use a positive beta_loss.sF   Number of components must be a positive integer; got (n_components=%r)sJ   Maximum number of iterations must be a positive integer; got (max_iter=%r)s>   Tolerance for stopping criteria must be positive; got (tol=%r)t   customs   NMF (input H)s   NMF (input W)RR   R[   Rf   RQ   R   R   R   s   Invalid solver parameter '%s'.sK   Maximum number of iteration %d reached. Increase it to improve convergence.(   R   R   N(   R   RF   R   R^   R
   R   R   RU   R_   t   INTEGER_TYPESRa   Rb   R   R   R   Ri   t   fullRl   R   RP   R   R   RV   RW   R   (   R   R*   R+   Rp   R[   R   RY   RZ   R   R   RG   RH   RI   Rf   R   R   Rr   Rs   Rt   RL   RM   RN   RO   R   (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   non_negative_factorizationA  s`    					t   NMFc           B  sh   e  Z d  Z d d d d d d d d d d e d  Z d d d d  Z d d	  Z d
   Z d   Z	 RS(   s  Non-Negative Matrix Factorization (NMF)

    Find two non-negative matrices (W, H) whose product approximates the non-
    negative matrix X. This factorization can be used for example for
    dimensionality reduction, source separation or topic extraction.

    The objective function is::

        0.5 * ||X - WH||_Fro^2
        + alpha * l1_ratio * ||vec(W)||_1
        + alpha * l1_ratio * ||vec(H)||_1
        + 0.5 * alpha * (1 - l1_ratio) * ||W||_Fro^2
        + 0.5 * alpha * (1 - l1_ratio) * ||H||_Fro^2

    Where::

        ||A||_Fro^2 = \sum_{i,j} A_{ij}^2 (Frobenius norm)
        ||vec(A)||_1 = \sum_{i,j} abs(A_{ij}) (Elementwise L1 norm)

    For multiplicative-update ('mu') solver, the Frobenius norm
    (0.5 * ||X - WH||_Fro^2) can be changed into another beta-divergence loss,
    by changing the beta_loss parameter.

    The objective function is minimized with an alternating minimization of W
    and H.

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

    Parameters
    ----------
    n_components : int or None
        Number of components, if n_components is not set all features
        are kept.

    init :  'random' | 'nndsvd' |  'nndsvda' | 'nndsvdar' | 'custom'
        Method used to initialize the procedure.
        Default: 'nndsvd' if n_components < n_features, otherwise random.
        Valid options:

        - 'random': non-negative random matrices, scaled with:
            sqrt(X.mean() / n_components)

        - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD)
            initialization (better for sparseness)

        - 'nndsvda': NNDSVD with zeros filled with the average of X
            (better when sparsity is not desired)

        - 'nndsvdar': NNDSVD with zeros filled with small random values
            (generally faster, less accurate alternative to NNDSVDa
            for when sparsity is not desired)

        - 'custom': use custom matrices W and H

    solver : 'cd' | 'mu'
        Numerical solver to use:
        'cd' is a Coordinate Descent solver.
        'mu' is a Multiplicative Update solver.

        .. versionadded:: 0.17
           Coordinate Descent solver.

        .. versionadded:: 0.19
           Multiplicative Update solver.

    beta_loss : float or string, default 'frobenius'
        String must be in {'frobenius', 'kullback-leibler', 'itakura-saito'}.
        Beta divergence to be minimized, measuring the distance between X
        and the dot product WH. Note that values different from 'frobenius'
        (or 2) and 'kullback-leibler' (or 1) lead to significantly slower
        fits. Note that for beta_loss <= 0 (or 'itakura-saito'), the input
        matrix X cannot contain zeros. Used only in 'mu' solver.

        .. versionadded:: 0.19

    tol : float, default: 1e-4
        Tolerance of the stopping condition.

    max_iter : integer, default: 200
        Maximum number of iterations before timing out.

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

    alpha : double, default: 0.
        Constant that multiplies the regularization terms. Set it to zero to
        have no regularization.

        .. versionadded:: 0.17
           *alpha* used in the Coordinate Descent solver.

    l1_ratio : double, default: 0.
        The regularization mixing parameter, with 0 <= l1_ratio <= 1.
        For l1_ratio = 0 the penalty is an elementwise L2 penalty
        (aka Frobenius Norm).
        For l1_ratio = 1 it is an elementwise L1 penalty.
        For 0 < l1_ratio < 1, the penalty is a combination of L1 and L2.

        .. versionadded:: 0.17
           Regularization parameter *l1_ratio* used in the Coordinate Descent
           solver.

    verbose : bool, default=False
        Whether to be verbose.

    shuffle : boolean, default: False
        If true, randomize the order of coordinates in the CD solver.

        .. versionadded:: 0.17
           *shuffle* parameter used in the Coordinate Descent solver.

    Attributes
    ----------
    components_ : array, [n_components, n_features]
        Factorization matrix, sometimes called 'dictionary'.

    reconstruction_err_ : number
        Frobenius norm of the matrix difference, or beta-divergence, between
        the training data ``X`` and the reconstructed data ``WH`` from
        the fitted model.

    n_iter_ : int
        Actual number of iterations.

    Examples
    --------
    >>> import numpy as np
    >>> X = np.array([[1, 1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]])
    >>> from sklearn.decomposition import NMF
    >>> model = NMF(n_components=2, init='random', random_state=0)
    >>> W = model.fit_transform(X)
    >>> H = model.components_

    References
    ----------
    Cichocki, Andrzej, and P. H. A. N. Anh-Huy. "Fast local algorithms for
    large scale nonnegative matrix and tensor factorizations."
    IEICE transactions on fundamentals of electronics, communications and
    computer sciences 92.3: 708-721, 2009.

    Fevotte, C., & Idier, J. (2011). Algorithms for nonnegative matrix
    factorization with the beta-divergence. Neural Computation, 23(9).
    RQ   RS   g-C6?i   g        i    c         C  sg   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ | |  _ |	 |  _ |
 |  _	 | |  _
 d  S(   N(   Rp   R[   RY   RZ   R   R   Rf   RG   RH   R   R   (   t   selfRp   R[   RY   RZ   R   R   Rf   RG   RH   R   R   (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   __init__  s    										c      !   C  s   t  | d d d t } t d | d | d | d |  j d	 |  j d
 t d |  j d |  j d |  j d |  j	 d |  j
 d |  j d d d |  j d |  j d |  j  \ } } } t | | | |  j d t |  _ | j d |  _ | |  _ | |  _ | S(   s  Learn a NMF model for the data X and returns the transformed data.

        This is more efficient than calling fit followed by transform.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Data matrix to be decomposed

        y : Ignored

        W : array-like, shape (n_samples, n_components)
            If init='custom', it is used as initial guess for the solution.

        H : array-like, shape (n_components, n_features)
            If init='custom', it is used as initial guess for the solution.

        Returns
        -------
        W : array, shape (n_samples, n_components)
            Transformed data.
        R   R   R   R   R   R*   R+   Rp   R[   R   RY   RZ   R   R   RG   RH   RI   RC   Rf   R   R   R-   i    (   R   R   (   R   RF   R   Rp   R[   R   RY   RZ   R   R   RG   RH   Rf   R   R   R;   t   reconstruction_err_R   t   n_components_t   components_t   n_iter_(   R   R   Rz   R*   R+   R   (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   fit_transform  s    $		c         K  s   |  j  | |  |  S(   s  Learn a NMF model for the data X.

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Data matrix to be decomposed

        y : Ignored

        Returns
        -------
        self
        (   R   (   R   R   Rz   t   params(    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   fit  s    c      !   C  s   t  |  d  t d | d d d |  j d |  j d |  j d t d |  j d	 |  j d
 |  j	 d |  j
 d |  j d |  j d d d |  j d |  j d |  j  \ } } } | S(   sU  Transform the data X according to the fitted NMF model

        Parameters
        ----------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Data matrix to be transformed by the model

        Returns
        -------
        W : array, shape (n_samples, n_components)
            Transformed data
        R   R   R*   R+   Rp   R[   R   RY   RZ   R   R   RG   RH   RI   RC   Rf   R   R   N(   R   R   RU   R   R   R[   t   FalseRY   RZ   R   R   RG   RH   Rf   R   R   (   R   R   R*   t   _R   (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt	   transform  s    c         C  s    t  |  d  t j | |  j  S(   sy  Transform data back to its original space.

        Parameters
        ----------
        W : {array-like, sparse matrix}, shape (n_samples, n_components)
            Transformed data matrix

        Returns
        -------
        X : {array-like, sparse matrix}, shape (n_samples, n_features)
            Data matrix of original shape

        .. versionadded:: 0.18
        R   (   R   R   R   R   (   R   R*   (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   inverse_transform  s    N(
   t   __name__t
   __module__t   __doc__RU   R   R   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyR     s   		*	(:   R   t
   __future__R    R   t   mathR   RV   Ra   R   t   numpyR   t   scipy.sparset   sparseR   t   baseR   R   t   utilsR   R   t   utils.extmathR   R   R	   R
   t   utils.validationR   R   t
   exceptionsR   t
   cdnmf_fastR   R   t   float32Rq   R%   t   Integralt   integerR   R   R   R   R   R;   R$   RP   R^   R   RU   R   R   R   R   R   R   R   R   R   (    (    (    s8   lib/python2.7/site-packages/sklearn/decomposition/nmf.pyt   <module>   sV   			
j						i^	R			