
    0Ph@                       d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
 ddl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mZ dd
lmZ ddlmZmZ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(m)Z) ddl*m+Z+m,Z,m-Z- ddl.m/Z/  ej0        ej1                  j2        Z3d Z4d Z5d Z6d5dZ7d Z8d Z9d6dZ:d Z;	 	 	 	 	 	 	 	 	 	 d7dZ<	 	 	 	 d8d Z=	 d9d!Z>	 	 	 	 	 	 	 	 	 d:d#Z? e#d$d%gd$dgd$dgd&gd'd(          	 	 	 d;ddd*d"ddd+d,d+dddd-d.            Z@ G d/ d0eeee          ZA G d1 d2eA          ZB G d3 d4eA          ZCdS )<z"Non-negative matrix factorization.    N)ABC)sqrt)IntegralReal)linalg   )config_context)BaseEstimatorClassNamePrefixFeaturesOutMixinTransformerMixin_fit_context)ConvergenceWarning)check_arraycheck_random_stategen_batchesmetadata_routing)Interval
StrOptionsvalidate_params)"_deprecate_Xt_in_inverse_transform)randomized_svdsafe_sparse_dotsquared_norm)check_is_fittedcheck_non_negativevalidate_data   )_update_cdnmf_fastc                 :    t          t          |                     S )zDot product-based Euclidean norm implementation.

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

    Parameters
    ----------
    x : array-like
        Vector for which to compute the norm.
    )r   r   )xs    Z/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/sklearn/decomposition/_nmf.pynormr"   +   s     Q       c                 t    t          j        |                                 |                                          S )zTrace of np.dot(X, Y.T).

    Parameters
    ----------
    X : array-like
        First matrix.
    Y : array-like
        Second matrix.
    )npdotravel)XYs     r!   	trace_dotr*   8   s&     6!''))QWWYY'''r#   c           	         t          |           } |d         dk    rA| j        d         |d         k    r*t          d| d|d          d| j        d          d          |d         dk    rA| j        d         |d         k    r*t          d| d|d          d| j        d          d          t          | |           t	          j        |           dk    rt          d	| d
          d S )Nr   autoz+Array with wrong first dimension passed to z. Expected z
, but got .r   z,Array with wrong second dimension passed to zArray passed to z is full of zeros.)r   shape
ValueErrorr   r%   max)Ar.   whoms      r!   _check_initr3   E   s1   AAQx6agajE!H44%$ % %5QR8 % %wqz% % %
 
 	
 Qx6agajE!H44%4 % %ERSH % %wqz% % %
 
 	
 q$	vayyA~~DDDDDEEE ~r#   Fc           
      x   t          |          }t          j        |           st          j        |           } t          j        |          }t          j        |          }|dk    rt          j        |           r{t          j        | j        | j                  }t          t          j        	                    |j
        ||g          |          }t          | |j
        z  |          }||z   d|z  z
  dz  }n(t          | t          j        ||          z
            dz  }|rt          j        |dz            S |S t          j        |           rt          |||           j        }	| j        }
n=t          j        ||          }|                                }	|                                 }
|
t          k    }|	|         }	|
|         }
t          |	|	t          k     <   |dk    rt          j        t          j        |d          t          j        |d                    }|
|	z  }t          j        |
t          j        |                    }|||
                                z
  z  }n1|dk    r[|
|	z  }t          j        |          t          j        | j                  z
  t          j        t          j        |                    z
  }nt          j        |           rWd}t)          | j        d                   D ]9}|t          j        t          j        ||dd|f                   |z            z  }:nt          j        ||z            }t          j        |
|	|dz
  z            }|
|z                                  ||z  z
  }|||dz
  z  z  }|||dz
  z  z  }|r't+          |d          }t          j        d|z            S |S )ak  Compute the beta-divergence of X and dot(W, H).

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

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

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

    beta : float or {'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 : bool, 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).
    r          @r   r   axisN)_beta_loss_to_floatspissparser%   
atleast_2dr&   datar*   r   	multi_dotTr   r   _special_sparse_dotr'   EPSILONsumlogprodr.   ranger0   )r(   WHbetasquare_rootnorm_Xnorm_WH
cross_prodresWH_dataX_dataWHindicessum_WHdivsum_WH_betaisum_X_WHs                     r!   _beta_divergencerV   V   ss   6 t$$D ;q>> M!
aA
aA qyy;q>> 	7VAFAF++F	 3 3QS!QK @ @!DDG"AGa00JG#cJ&66#=CCq26!Q<</0036C 	737###J	{1~~ %aA..3VAq\\((** wGgGG_F ")GGg qyyqq)))26!!+<+<+<==wfVRVC[[))v

$$ 
wfSkkBGAG,,,rvbfSkk/B/BB ;q>> 	+ K171:&& B BrvbfQ!!!Q$&8&8D&@AAAB &T**K6&'dQh"788t|  ""TH_4{dQh''ttax   #qkkwq3w
r#   c                 \   t          j        |          r|                                \  }}|j        d         }t	          j        |          }| j        d         }t          |||z            }t          d||          D ]f}	t          |	|	|z             }
t	          j	        | ||
         ddf         |j
        ||
         ddf                                       d          ||
<   gt          j        |||ff|j                  }|                                S t	          j        | |          S )z0Computes np.dot(W, H), only where X is non zero.r   r   Nr6   )r.   )r9   r:   nonzeror.   r%   emptyr0   rD   slicemultiplyr>   rA   
coo_matrixtocsrr&   )rE   rF   r(   iijjn_valsdot_valsn_components
batch_sizestartbatchrO   s               r!   r?   r?      s   	{1~~ B!8F##wqzv'=>>
1fj11 	 	E%!344E k!BuIqqqL/13r%y!!!|;LMMQQ R  HUOO ]Hr2h/qw???xxzzva||r#   c                 L    dddd}t          | t                    r||          } | S )z"Convert string beta_loss to float.r   r   r   )	frobeniuskullback-leibleritakura-saito)
isinstancestr)	beta_lossbeta_loss_maps     r!   r8   r8      s3    "#QOOM)S!! -!),	r#   ư>c           
         t          | d           | j        \  }}|<|dk    r6|t          ||          k    r"t          d                    |                    ||t          ||          k    rd}nd}|dk    rt          j        |                                 |z            }t          |          }||	                    ||f          
                    | j        d          z  }	||	                    ||f          
                    | j        d          z  }
t          j        |	|		           t          j        |
|
	           |
|	fS t          | ||
          \  }}}t          j        |          }
t          j        |          }	t          j        |d                   t          j        |dddf                   z  |
dddf<   t          j        |d                   t          j        |dddf                   z  |	dddf<   t          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%          |          }}||z  ||z  }}||k    r||z  }||z  }|}n||z  }||z  }|}t          j        ||         |z            }||z  |
dd|f<   ||z  |	|ddf<   /d|
|
|k     <   d|	|	|k     <   |dk    rn|dk    r'|                                 }||
|
dk    <   ||	|	dk    <   n|dk    rt          |          }|                                 }t          ||	                    t'          |
|
dk                                 z  dz            |
|
dk    <   t          ||	                    t'          |	|	dk                                 z  dz            |	|	dk    <   nt          d|dd          |
|	fS )aN  Algorithms for NMF initialization.

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

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

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

    init :  {'random', 'nndsvd', 'nndsvda', 'nndsvdar'}, default=None
        Method used to initialize the procedure.
        Valid options:

        - None: 'nndsvda' if n_components <= min(n_samples, n_features),
            otherwise 'random'.

        - '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

        .. versionchanged:: 1.1
            When `init=None` and n_components is less than n_samples and n_features
            defaults to `nndsvda` instead of `nndsvd`.

    eps : float, default=1e-6
        Truncate all values less then this in output to zero.

    random_state : int, RandomState instance or None, default=None
        Used when ``init`` == 'nndsvdar' or 'random'. Pass an int for
        reproducible results across multiple function calls.
        See :term:`Glossary <random_state>`.

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

    H : array-like of 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
    zNMF initializationNrandomzLinit = '{}' can only be used when n_components <= min(n_samples, n_features)nndsvda)sizeF)copyout)random_stater   r   nndsvdnndsvdard   zInvalid init parameter: got z instead of one of )Nrp   rw   rq   rx   )r   r.   minr/   formatr%   r   meanr   standard_normalastypedtypeabsr   
zeros_likerD   maximumminimumr"   len)r(   rb   initepsrv   	n_samples
n_featuresavgrngrF   rE   USVjr    yx_py_px_ny_nx_p_nrmy_p_nrmx_n_nrmy_n_nrmm_pm_nuvsigmalbds                                  r!   _initialize_nmfr      s   | q.///GIz 	H3y*555599?
 
 	

 |3y*5555DDD xgaffhh-.. ..#%%L*+E%FFMMG% N 
 
 
 #%%I|+D%EELLG% M 
 
 
 	qa
qa!t Q<HHHGAq!
aA
aA gadmmbfQqqq!tWoo-AaaadGgadmmbfQq!!!tWoo-AadG1l##  Aw!QQQ$1 :a##RZ1%5%5S6"*Q**++RVBJq!4D4D-E-ES  99d3ii99d3iiW$g&7S 99gAgAEEgAgAEgadUl##'!!!Q$'!QQQ$Aa#gJAa#gJx			ffhh!q&	!q&					 ..ffhhc11s1Q!V9~~1FFFLMM!q&	c11s1Q!V9~~1FFFLMM!q&		jttFFH
 
 	

 a4Kr#   c                    |j         d         }t          j        |j        |          }t	          | |          }	|dk    r|j        dd|dz   xx         |z  cc<   |dk    r|	|z  }	|r|                    |          }
nt          j        |          }
t          j        |
t          j	                  }
t          |||	|
          S )zHelper 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, ...).

    r           Nr   )r.   r%   r&   r>   r   flatpermutationarangeasarrayintpr   )r(   rE   Htl1_regl2_regshufflerv   rb   HHtXHtr   s              r!   _update_coordinate_descentr   y  s     8A;L
&r

C
!R
 
 C }}$$L1$$%%%/%%%}}v ."..|<<i--*[888Kac;777r#   -C6?   Tc                    t          |j        d          }t          | d          } t          |          }t          d|dz             D ]}d}|t	          | ||||||          z  }|	r|t	          | j        ||||||          z  }|dk    r|}|dk    r n6|
rt          d||z             ||z  |k    r|
rt          d	|dz               n||j        |fS )
a	  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 of shape (n_samples, n_features)
        Constant matrix.

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

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

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

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

    l1_reg_W : float, default=0.
        L1 regularization parameter for W.

    l1_reg_H : float, default=0.
        L1 regularization parameter for H.

    l2_reg_W : float, default=0.
        L2 regularization parameter for W.

    l2_reg_H : float, default=0.
        L2 regularization parameter for H.

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

    verbose : int, default=0
        The verbosity level.

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

    random_state : int, RandomState instance or None, default=None
        Used to randomize the coordinates in the CD solver, when
        ``shuffle`` is set to ``True``. Pass an int for reproducible
        results across multiple function calls.
        See :term:`Glossary <random_state>`.

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

    H : ndarray of 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
    ----------
    .. [1] :doi:`"Fast local algorithms for large scale nonnegative matrix and tensor
       factorizations" <10.1587/transfun.E92.A.708>`
       Cichocki, Andrzej, and P. H. A. N. Anh-Huy. IEICE transactions on fundamentals
       of electronics, communications and computer sciences 92.3: 708-721, 2009.
    C)ordercsr)accept_sparser   r   r   z
violation:zConverged at iteration)r   r>   r   rD   r   print)r(   rE   rF   tolmax_iterl1_reg_Wl1_reg_Hl2_reg_Wl2_reg_Hupdate_Hverboser   rv   r   r   n_iter	violationviolation_inits                     r!   _fit_coordinate_descentr     s8   j 
QS	$	$	$BAU+++A
\
*
*C8a<((  	 	/q"h'3
 
 	
	  	3RHh  I Q;;&NQE 	<,	N :;;;~%,, <.
;;;E -
 bdF?r#   c                     |dk    rc|	t          | |j                  }	|
r|	}n|	                                }|t          j        ||j                  }t          j        ||          }nt          |||           }t          j        |           r|j        }| j        }n4|}| }|                                }|dz
  dk     rt          ||t          k     <   |dz
  dk     rt          ||t          k     <   |dk    rt          j
        |||           n#|dk    r|dz  }|dz  }||z  }n||dz
  z  }||z  }t          ||j                  }|dk    r/|t          j        |d	          }|t          j        ddf         }nt          j        |           rt          j        |j                  }t          | j        d                   D ]f}t          j        ||ddf         |          }|dz
  dk     rt          ||t          k     <   ||dz
  z  }t          j        ||j                  ||ddf<   gn"||dz
  z  }t          j        ||j                  }|}|dk    r||z  }|dk    r|||z  z   }t          ||dk    <   ||z  }|}|dk    r||z  }||z  }||||	fS )
z&Update W in Multiplicative Update NMF.r   N      ?r   r5   r   rt   r6   )r   r>   rs   r%   r&   r?   r9   r:   r<   r@   dividerA   newaxisrY   r.   rD   )r(   rE   rF   rl   r   r   gammaH_sumr   r   r   	numeratordenominator	WH_safe_XWH_safe_X_datarN   rO   WHHtrT   WHidelta_Ws                        r!   _multiplicative_update_wr     s    A~~;!!QS))C 	#II 

I ;&AC..CfQnn
 (1a00	;q>> 		+&^NVFF&NF!!B3""#*2<  s?Q7>N>G34>>Ifn.AAAAA!^^ r!Nq Nf$NNy1},Nf$N $Iqs33	 >>}qq)))
AAA.KK {1~~ ' x((qwqz** 2 2A&1aaa4!,,C 1}q((-4C'M*IM)C!#QS!1!1DAAAJJ2 y1}$vb!#K !||x!||!HqL0$+Kq !IG zzELAeS#r#   c
                 T   |dk    r>t          |j        |           }
t          j                            |j        ||g          }nt          |||           }t          j        |           r|j        }| j        }n4|}| }|	                                }|dz
  dk     rt          ||t          k     <   |dz
  dk     rt          ||t          k     <   |dk    rt          j        |||           n#|dk    r|dz  }|dz  }||z  }n||dz
  z  }||z  }t          |j        |          }
|dk    r6t          j        |d          }d||dk    <   |d	d	t          j        f         }nt          j        |           rt          j        |j                  }t!          | j        d                   D ]f}t          j        ||d	d	|f                   }|dz
  dk     rt          ||t          k     <   ||dz
  z  }t          j        |j        |          |d	d	|f<   gn"||dz
  z  }t          j        |j        |          }|}|dk    r||z  }|dk    r|||z  z   }t          ||dk    <   |:|8|dk    r|d|z  z  }|
|z  }
||	z  }||	z  }||
z  }||z  }||z  }|dk    r||z  }n|
}||z  }|dk    r||z  }||z  }|S )
z&update H in Multiplicative Update NMF.r   r   r   r5   r   rt   r   r6   N)r   r>   r%   r   r=   r?   r9   r:   r<   rs   r@   r   rA   r   rY   r.   rD   r&   )r(   rE   rF   rl   r   r   r   r1   Brhor   r   r   r   rN   rO   W_sumWtWHrT   r   delta_Hs                        r!   _multiplicative_update_hr   {  s,    A~~#AC++	i))131+66 (1a00	;q>> 		+&^NVFF&NF!!B3""#*2<  s?Q7>N>G34>>Ifn.AAAAA!^^ r!Nq Nf$NNy1},Nf$N $AC33	 >>F11%%%E #E%1*2:.KK
 {1~~ ' x((qwqz** 2 2A&AaaadG,,C 1}q((-4C'M*IM)C!#S!1!1DAJJ2 y1}$vac2K !||x!||!HqL0$+Kq !}A::!e)OAQ		S	S	Y	[EA::%KA;A::G	WHr#   rg   c                 h   t          j                     }t          |          }|dk     r	dd|z
  z  }n|dk    r	d|dz
  z  }nd}t          | |||d          }|}d\  }}}t          d|dz             D ] }t	          | ||||||||||
          \  }}}}|dk     r*d	||t          j        t
          j                  j        k     <   |
rLt          | |||||	|
          }d\  }}}|dk    r*d	||t          j        t
          j                  j        k     <   |dk    rZ|dz  dk    rQt          | |||d          }|r+t          j                     }t          d|||z
  |fz             ||z
  |z  |k     r n|}|r9|dk    s	|dz  dk    r*t          j                     }t          d|||z
  fz             |||fS )aK	  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 of shape (n_samples, n_features)
        Constant input matrix.

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

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

    beta_loss : float or {'frobenius', 'kullback-leibler',             'itakura-saito'}, 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 : int, default=200
        Number of iterations.

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

    l1_reg_W : float, default=0.
        L1 regularization parameter for W.

    l1_reg_H : float, default=0.
        L1 regularization parameter for H.

    l2_reg_W : float, default=0.
        L2 regularization parameter for W.

    l2_reg_H : float, default=0.
        L2 regularization parameter for H.

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

    verbose : int, default=0
        The verbosity level.

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

    H : ndarray of 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
    ----------
    Lee, D. D., & Seung, H., S. (2001). Algorithms for Non-negative Matrix
    Factorization. Adv. Neural Inform. Process. Syst.. 13.
    Fevotte, C., & Idier, J. (2011). Algorithms for nonnegative matrix
    factorization with the beta-divergence. Neural Computation, 23(9).
    r   r   r5   r   TrH   NNN)rl   r   r   r   r   r   r   r   r   )rl   r   r   r   r   
   z0Epoch %02d reached after %.3f seconds, error: %fz&Epoch %02d reached after %.3f seconds.)timer8   rV   rD   r   r%   finfofloat64r   r   r   )r(   rE   rF   rl   r   r   r   r   r   r   r   r   
start_timer   error_at_initprevious_errorr   r   r   r   error	iter_timeend_times                          r!   _fit_multiplicative_updater     s[   f J#I..I 1}}sY'	Qy3' %Q1iTJJJM"N&OE38a<(( 5# 5# 6
 
 
5#s q==.1Aa"(2:&&**+  	6(#!!  A /OE3 A~~25!bhrz**../ 77v{a''$Q1iTJJJE  IKK	Fy:5u=>  
 &-7#=="N  
C1HHq 0 09;;4:@U7VV	
 	
 	
 a<r#   z
array-likezsparse matrixboolean)r(   rE   rF   r   prefer_skip_nested_validationr,   cdr   same)r   r   solverrl   r   r   alpha_Walpha_Hl1_ratiorv   r   r   c                D   t          ||||||	||
||||          }|                                 t          | dt          j        t          j        g          } t          d          5  |                    | |||          \  }}}ddd           n# 1 swxY w Y   |||fS )a  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:

    .. math::

        L(W, H) &= 0.5 * ||X - WH||_{loss}^2

                &+ alpha\_W * l1\_ratio * n\_features * ||vec(W)||_1

                &+ alpha\_H * l1\_ratio * n\_samples * ||vec(H)||_1

                &+ 0.5 * alpha\_W * (1 - l1\_ratio) * n\_features * ||W||_{Fro}^2

                &+ 0.5 * alpha\_H * (1 - l1\_ratio) * n\_samples * ||H||_{Fro}^2,

    where :math:`||A||_{Fro}^2 = \sum_{i,j} A_{ij}^2` (Frobenius norm) and
    :math:`||vec(A)||_1 = \sum_{i,j} abs(A_{ij})` (Elementwise L1 norm)

    The generic norm :math:`||X - WH||_{loss}^2` may represent
    the Frobenius norm or another supported beta-divergence loss.
    The choice between options is controlled by the `beta_loss` parameter.

    The regularization terms are scaled by `n_features` for `W` and by `n_samples` for
    `H` to keep their impact balanced with respect to one another and to the data fit
    term as independent as possible of the size `n_samples` of the training set.

    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.

    Note that the transformed data is named W and the components matrix is named H. In
    the NMF literature, the naming convention is usually the opposite since the data
    matrix X is transposed.

    Parameters
    ----------
    X : {array-like, sparse matrix} of shape (n_samples, n_features)
        Constant matrix.

    W : array-like of shape (n_samples, n_components), default=None
        If `init='custom'`, it is used as initial guess for the solution.
        If `update_H=False`, it is initialised as an array of zeros, unless
        `solver='mu'`, then it is filled with values calculated by
        `np.sqrt(X.mean() / self._n_components)`.
        If `None`, uses the initialisation method specified in `init`.

    H : array-like of shape (n_components, n_features), default=None
        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.
        If `None`, uses the initialisation method specified in `init`.

    n_components : int or {'auto'} or None, default='auto'
        Number of components. If `None`, all features are kept.
        If `n_components='auto'`, the number of components is automatically inferred
        from `W` or `H` shapes.

        .. versionchanged:: 1.4
            Added `'auto'` value.

        .. versionchanged:: 1.6
            Default value changed from `None` to `'auto'`.

    init : {'random', 'nndsvd', 'nndsvda', 'nndsvdar', 'custom'}, default=None
        Method used to initialize the procedure.

        Valid options:

        - None: 'nndsvda' if n_components < n_features, otherwise 'random'.
        - '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': If `update_H=True`, use custom matrices W and H which must both
          be provided. If `update_H=False`, then only custom matrix H is used.

        .. versionchanged:: 0.23
            The default value of `init` changed from 'random' to None in 0.23.

        .. versionchanged:: 1.1
            When `init=None` and n_components is less than n_samples and n_features
            defaults to `nndsvda` instead of `nndsvd`.

    update_H : bool, 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'}, default='cd'
        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 {'frobenius', 'kullback-leibler',             'itakura-saito'}, default='frobenius'
        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 : int, default=200
        Maximum number of iterations before timing out.

    alpha_W : float, default=0.0
        Constant that multiplies the regularization terms of `W`. Set it to zero
        (default) to have no regularization on `W`.

        .. versionadded:: 1.0

    alpha_H : float or "same", default="same"
        Constant that multiplies the regularization terms of `H`. Set it to zero to
        have no regularization on `H`. If "same" (default), it takes the same value as
        `alpha_W`.

        .. versionadded:: 1.0

    l1_ratio : float, default=0.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.

    random_state : int, RandomState instance or None, default=None
        Used for NMF initialisation (when ``init`` == 'nndsvdar' or
        'random'), and in Coordinate Descent. Pass an int for reproducible
        results across multiple function calls.
        See :term:`Glossary <random_state>`.

    verbose : int, default=0
        The verbosity level.

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

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

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

    n_iter : int
        Actual number of iterations.

    References
    ----------
    .. [1] :doi:`"Fast local algorithms for large scale nonnegative matrix and tensor
       factorizations" <10.1587/transfun.E92.A.708>`
       Cichocki, Andrzej, and P. H. A. N. Anh-Huy. IEICE transactions on fundamentals
       of electronics, communications and computer sciences 92.3: 708-721, 2009.

    .. [2] :doi:`"Algorithms for nonnegative matrix factorization with the
       beta-divergence" <10.1162/NECO_a_00168>`
       Fevotte, C., & Idier, J. (2011). Neural Computation, 23(9).

    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)
    )rb   r   r   rl   r   r   rv   r   r   r   r   r   r   cscr   r   Tassume_finiterE   rF   r   N)NMF_validate_paramsr   r%   r   float32r	   _fit_transform)r(   rE   rF   rb   r   r   r   rl   r   r   r   r   r   rv   r   r   estr   s                     r!   non_negative_factorizationr     s   l !!  C A^BJ
;STTTA	d	+	+	+ J J))!qA)II1fJ J J J J J J J J J J J J J J a<s   (BBBc                       e Zd ZU dZdej        iZ eeddd          d e	dh          g e	h d          dg e	h d	          e
g ee
d
dd          g eeddd          gdg ee
d
dd          g ee
d
dd           e	dh          g ee
d
dd          gdgd
Zeed<   	 dddddddddd
d	dZd Zd Zd Zd dZd dddZed             Z fdZ xZS )!_BaseNMFz$Base class for NMF and MiniBatchNMF.Xtr   Nleftclosedr,   >   customrw   rp   rq   rx   >   rg   ri   rh   r   rv   r   bothr   
rb   r   rl   r   r   rv   r   r   r   r   _parameter_constraintsrg   r   r   r   )	r   rl   r   r   rv   r   r   r   r   c       	             || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        d S Nr  )selfrb   r   rl   r   r   rv   r   r   r   r   s              r!   __init__z_BaseNMF.__init__  sP     )	" ( r#   c                     | j         | _        | j        |j        d         | _        t          | j                  | _        d S )Nr   )rb   _n_componentsr.   r8   rl   
_beta_loss)r  r(   s     r!   _check_paramsz_BaseNMF._check_params  s;    !.%!"D .dn==r#   c                 F   |j         \  }}| j        dk    r|rt          || j        |fd           t          ||| j        fd           | j        dk    r|j         d         | _        |j        |j        k    s|j        |j        k    r-t          d                    |j        |j                            nk|s|t          j        dt                     t          || j        |fd           | j        dk    r|j         d         | _        |j        |j        k    r't          d	                    |j                            | j
        d
k    rRt          j        |                                | j        z            }t          j        || j        f||j                  }nt          j        || j        f|j                  }n`||t          j        dt                     | j        dk    r|j         d         | _        t!          || j        | j        | j                  \  }}||fS )z"Check W and H, or initialize them.r  zNMF (input H)zNMF (input W)r,   r   zKH and W should have the same dtype as X. Got H.dtype = {} and W.dtype = {}.Nz8When update_H=False, the provided initial W is not used.z4H should have the same dtype as X. Got H.dtype = {}.mur   zcWhen init!='custom', provided W or H are ignored. Set  init='custom' to use them as initialization.r   )r   rv   )r.   r   r3   r  r   	TypeErrorr{   warningswarnRuntimeWarningr   r%   r   r|   fullzerosr   rv   )r  r(   rE   rF   r   r   r   r   s           r!   
_check_w_hz_BaseNMF._check_w_h  sB    !	:9  X D.
;_MMMIt'9:OLLL!V++%&WQZ"w!'!!QW%7%755;VAGQW5M5M   &8  (	}N"  
 D.
;_MMM!V++%&WQZ"w!'!!JQQ    {d""gaffhh);;<<GY(:;SPPPHi);<AGLLL }H #   !V++%&WQZ""4%DIDDU  DAq !tr#   c                     |j         \  }}| j        }| j        dk    r| j        n| j        }||z  | j        z  }||z  | j        z  }||z  d| j        z
  z  }||z  d| j        z
  z  }	||||	fS )z$Compute scaled regularization terms.r   r   )r.   r   r   r   )
r  r(   r   r   r   r   r   r   r   r   s
             r!   _compute_regularizationz _BaseNMF._compute_regularization  s     !	:,"&,&"8"8$,,dl'$-7w&6'3+>?w&#*=>8X55r#   c                 "     | j         |fi | | S )aS  Learn a NMF model for the data X.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training vector, 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.

        **params : kwargs
            Parameters (keyword arguments) and values passed to
            the fit_transform instance.

        Returns
        -------
        self : object
            Returns the instance itself.
        )fit_transform)r  r(   r   paramss       r!   fitz_BaseNMF.fit  s$    . 	1'''''r#   )r   c                T    t          ||          }t          |            || j        z  S )a^  Transform data back to its original space.

        .. versionadded:: 0.18

        Parameters
        ----------
        X : {ndarray, sparse matrix} of shape (n_samples, n_components)
            Transformed data matrix.

        Xt : {ndarray, sparse matrix} of shape (n_samples, n_components)
            Transformed data matrix.

            .. deprecated:: 1.5
                `Xt` was deprecated in 1.5 and will be removed in 1.7. Use `X` instead.

        Returns
        -------
        X : ndarray of shape (n_samples, n_features)
            Returns a data matrix of the original shape.
        )r   r   components_)r  r(   r   s      r!   inverse_transformz_BaseNMF.inverse_transform  s/    , /q"554###r#   c                 &    | j         j        d         S )z&Number of transformed output features.r   )r  r.   )r  s    r!   _n_features_outz_BaseNMF._n_features_out.  s     %a((r#   c                     t                                                      }d|j        _        d|j        _        ddg|j        _        |S )NTr   r   )super__sklearn_tags__
input_tagspositive_onlysparsetransformer_tagspreserves_dtype)r  tags	__class__s     r!   r$  z_BaseNMF.__sklearn_tags__3  sA    ww''))(,%!%1:I0F-r#   r,   r  )__name__
__module____qualname____doc__r   UNUSED-_BaseNMF__metadata_request__inverse_transformr   r   r   r   r  dict__annotations__r	  r  r  r  r  r  propertyr!  r$  __classcell__r+  s   @r!   r   r   o  s/        ..
 .23C3J,K) HXq$v666Jx  
 JLLLMM

 JIIIJJ
 q$v6667Xh4???@'(HT1d6:::;HT1d6:::JJx<P<PQXdAq8889;)$ $D   2      2> > >: : :x6 6 6   4$d $ $ $ $ $6 ) ) X)        r#   r   c                        e Zd ZU dZi ej         eddh          gdgdZeed<   	 dddd	d
dddddddd fdZ	 fdZ
 ed          dd            ZddZd Z xZS )r   a  Non-Negative Matrix Factorization (NMF).

    Find two non-negative matrices, i.e. matrices with all non-negative elements, (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:

    .. math::

        L(W, H) &= 0.5 * ||X - WH||_{loss}^2

                &+ alpha\_W * l1\_ratio * n\_features * ||vec(W)||_1

                &+ alpha\_H * l1\_ratio * n\_samples * ||vec(H)||_1

                &+ 0.5 * alpha\_W * (1 - l1\_ratio) * n\_features * ||W||_{Fro}^2

                &+ 0.5 * alpha\_H * (1 - l1\_ratio) * n\_samples * ||H||_{Fro}^2,

    where :math:`||A||_{Fro}^2 = \sum_{i,j} A_{ij}^2` (Frobenius norm) and
    :math:`||vec(A)||_1 = \sum_{i,j} abs(A_{ij})` (Elementwise L1 norm).

    The generic norm :math:`||X - WH||_{loss}` may represent
    the Frobenius norm or another supported beta-divergence loss.
    The choice between options is controlled by the `beta_loss` parameter.

    The regularization terms are scaled by `n_features` for `W` and by `n_samples` for
    `H` to keep their impact balanced with respect to one another and to the data fit
    term as independent as possible of the size `n_samples` of the training set.

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

    Note that the transformed data is named W and the components matrix is named H. In
    the NMF literature, the naming convention is usually the opposite since the data
    matrix X is transposed.

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

    Parameters
    ----------
    n_components : int or {'auto'} or None, default='auto'
        Number of components. If `None`, all features are kept.
        If `n_components='auto'`, the number of components is automatically inferred
        from W or H shapes.

        .. versionchanged:: 1.4
            Added `'auto'` value.

        .. versionchanged:: 1.6
            Default value changed from `None` to `'auto'`.

    init : {'random', 'nndsvd', 'nndsvda', 'nndsvdar', 'custom'}, default=None
        Method used to initialize the procedure.
        Valid options:

        - `None`: 'nndsvda' if n_components <= min(n_samples, n_features),
          otherwise random.

        - `'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` which must both be provided.

        .. versionchanged:: 1.1
            When `init=None` and n_components is less than n_samples and n_features
            defaults to `nndsvda` instead of `nndsvd`.

    solver : {'cd', 'mu'}, default='cd'
        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 {'frobenius', 'kullback-leibler',             'itakura-saito'}, default='frobenius'
        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 : int, default=200
        Maximum number of iterations before timing out.

    random_state : int, RandomState instance or None, default=None
        Used for initialisation (when ``init`` == 'nndsvdar' or
        'random'), and in Coordinate Descent. Pass an int for reproducible
        results across multiple function calls.
        See :term:`Glossary <random_state>`.

    alpha_W : float, default=0.0
        Constant that multiplies the regularization terms of `W`. Set it to zero
        (default) to have no regularization on `W`.

        .. versionadded:: 1.0

    alpha_H : float or "same", default="same"
        Constant that multiplies the regularization terms of `H`. Set it to zero to
        have no regularization on `H`. If "same" (default), it takes the same value as
        `alpha_W`.

        .. versionadded:: 1.0

    l1_ratio : float, default=0.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 : int, default=0
        Whether to be verbose.

    shuffle : bool, 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_ : ndarray of shape (n_components, n_features)
        Factorization matrix, sometimes called 'dictionary'.

    n_components_ : int
        The number of components. It is same as the `n_components` parameter
        if it was given. Otherwise, it will be same as the number of
        features.

    reconstruction_err_ : float
        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.

    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

    See Also
    --------
    DictionaryLearning : Find a dictionary that sparsely encodes data.
    MiniBatchSparsePCA : Mini-batch Sparse Principal Components Analysis.
    PCA : Principal component analysis.
    SparseCoder : Find a sparse representation of data from a fixed,
        precomputed dictionary.
    SparsePCA : Sparse Principal Components Analysis.
    TruncatedSVD : Dimensionality reduction using truncated SVD.

    References
    ----------
    .. [1] :doi:`"Fast local algorithms for large scale nonnegative matrix and tensor
       factorizations" <10.1587/transfun.E92.A.708>`
       Cichocki, Andrzej, and P. H. A. N. Anh-Huy. IEICE transactions on fundamentals
       of electronics, communications and computer sciences 92.3: 708-721, 2009.

    .. [2] :doi:`"Algorithms for nonnegative matrix factorization with the
       beta-divergence" <10.1162/NECO_a_00168>`
       Fevotte, C., & Idier, J. (2011). Neural Computation, 23(9).

    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_
    r  r   r   )r   r   r  r,   Nrg   r   r   r   r   r   F)r   r   rl   r   r   rv   r   r   r   r   r   c                z    t                                          ||||||||	|
|
  
         || _        || _        d S Nr  )r#  r	  r   r   )r  rb   r   r   rl   r   r   rv   r   r   r   r   r   r+  s                r!   r	  zNMF.__init__  sX      	%% 	 	
 	
 	
 r#   c                    t                                          |           | j        dk    r(| j        dvrt	          d| j        d| j                  | j        dk    r%| j        dk    rt          j        dt                     | S )Nr  )r   rg   z$Invalid beta_loss parameter: solver z does not handle beta_loss = rw   zThe 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.)	r#  r  r   rl   r/   r   r  r  UserWarningr  r(   r+  s     r!   r  zNMF._check_params0  s    a    ;$4>9I#I#I2t{ 2 2#~2 2   ;$49#8#8MM
    r#   Tr   c                 \   t          | |dt          j        t          j        g          }t	          d          5  |                     |||          \  }}}ddd           n# 1 swxY w Y   t          |||| j        d          | _        |j	        d         | _
        || _        || _        |S )	a  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} of shape (n_samples, n_features)
            Training vector, 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.

        W : array-like of shape (n_samples, n_components), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            If `None`, uses the initialisation method specified in `init`.

        H : array-like of shape (n_components, n_features), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            If `None`, uses the initialisation method specified in `init`.

        Returns
        -------
        W : ndarray of shape (n_samples, n_components)
            Transformed data.
        r   r   Tr   rE   rF   Nr   r   )r   r%   r   r   r	   r   rV   r  reconstruction_err_r.   n_components_r  n_iter_)r  r(   r   rE   rF   r   s         r!   r  zNMF.fit_transformG  s    8 !>"*bj9Q
 
 
 $/// 	< 	<..qA.;;LAq&	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< $4q!T_$$
 $
 $
  WQZs   A##A'*A'c                    |                      |           |                                dk    r| j        dk    rt          d          |                     ||||          \  }}|                     |          \  }}}}	| j        dk    r:t          |||| j        | j	        ||||	|| j
        | j        | j                  \  }}}
nU| j        dk    r3t          |||| j        | j	        | j        ||||	|| j
                  ^}}}
}nt          d| j        z            |
| j	        k    r-| j        dk    r"t          j        d| j	        z  t                      |||
fS )a4  Learn a NMF model for the data X and returns the transformed data.

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

        y : Ignored

        W : array-like of shape (n_samples, n_components), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            If `update_H=False`, it is initialised as an array of zeros, unless
            `solver='mu'`, then it is filled with values calculated by
            `np.sqrt(X.mean() / self._n_components)`.
            If `None`, uses the initialisation method specified in `init`.

        H : array-like of shape (n_components, n_features), default=None
            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.
            If `None`, uses the initialisation method specified in `init`.

        update_H : bool, default=True
            If True, both W and H will be estimated from initial guesses,
            this corresponds to a call to the 'fit_transform' method.
            If False, only W will be estimated, this corresponds to a call
            to the 'transform' method.

        Returns
        -------
        W : ndarray of shape (n_samples, n_components)
            Transformed data.

        H : ndarray of shape (n_components, n_features)
            Factorization matrix, sometimes called 'dictionary'.

        n_iter_ : int
            Actual number of iterations.
        r   |When beta_loss <= 0 and X contains zeros, the solver may diverge. Please add small values to X, or use a positive beta_loss.r   )r   r   r   rv   r  zInvalid solver parameter '%s'.zLMaximum number of iterations %d reached. Increase it to improve convergence.)r  rz   r  r/   r  r  r   r   r   r   r   r   rv   r   r  r  r   )r  r(   r   rE   rF   r   r   r   r   r   r   _s               r!   r   zNMF._fit_transformt  s   P 	15577a<<DOq005   q!Q111 261M1Ma1P1P.(Hh;$2!!.  LAq&& [D  9   Aq&11 =KLLLT]""tx!||M-/3}="   !V|r#   c                    t          |            t          | |dt          j        t          j        gdd          }t          d          5  |                     || j        d          ^}}ddd           n# 1 swxY w Y   |S )a  Transform the data X according to the fitted NMF model.

        Parameters
        ----------
        X : {array-like, sparse matrix} of shape (n_samples, n_features)
            Training vector, where `n_samples` is the number of samples
            and `n_features` is the number of features.

        Returns
        -------
        W : ndarray of shape (n_samples, n_components)
            Transformed data.
        r   FT)r   r   resetensure_non_negativer   )rF   r   N)r   r   r%   r   r   r	   r   r  )r  r(   rE   rE  s       r!   	transformzNMF.transform  s     	(:rz* $
 
 
 $/// 	O 	O''T-='NNEA	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O 	O s    A77A;>A;r,  r   NNNT)r-  r.  r/  r0  r   r  r   r3  r4  r	  r  r   r  r   rI  r6  r7  s   @r!   r   r   ;  s;        L L\$

)$:tTl++,;$ $ $D           @    . \555* * * 65*X` ` ` `D      r#   r   c                       e Zd ZU dZi ej         eeddd          dg eeddd          g eeddd          gdg eeddd          g eeddd          dgd	Ze	e
d
<   	 d"dddddddddddddddd fdZ fdZd Zd Zd Z ed          d#d            Zd$dZd  Z ed          d#d!            Z xZS )%MiniBatchNMFa;   Mini-Batch Non-Negative Matrix Factorization (NMF).

    .. versionadded:: 1.1

    Find two non-negative matrices, i.e. matrices with all non-negative elements,
    (`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:

    .. math::

        L(W, H) &= 0.5 * ||X - WH||_{loss}^2

                &+ alpha\_W * l1\_ratio * n\_features * ||vec(W)||_1

                &+ alpha\_H * l1\_ratio * n\_samples * ||vec(H)||_1

                &+ 0.5 * alpha\_W * (1 - l1\_ratio) * n\_features * ||W||_{Fro}^2

                &+ 0.5 * alpha\_H * (1 - l1\_ratio) * n\_samples * ||H||_{Fro}^2,

    where :math:`||A||_{Fro}^2 = \sum_{i,j} A_{ij}^2` (Frobenius norm) and
    :math:`||vec(A)||_1 = \sum_{i,j} abs(A_{ij})` (Elementwise L1 norm).

    The generic norm :math:`||X - WH||_{loss}^2` may represent
    the Frobenius norm or another supported beta-divergence loss.
    The choice between options is controlled by the `beta_loss` parameter.

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

    Note that the transformed data is named `W` and the components matrix is
    named `H`. In the NMF literature, the naming convention is usually the opposite
    since the data matrix `X` is transposed.

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

    Parameters
    ----------
    n_components : int or {'auto'} or None, default='auto'
        Number of components. If `None`, all features are kept.
        If `n_components='auto'`, the number of components is automatically inferred
        from W or H shapes.

        .. versionchanged:: 1.4
            Added `'auto'` value.

        .. versionchanged:: 1.6
            Default value changed from `None` to `'auto'`.

    init : {'random', 'nndsvd', 'nndsvda', 'nndsvdar', 'custom'}, default=None
        Method used to initialize the procedure.
        Valid options:

        - `None`: 'nndsvda' if `n_components <= min(n_samples, n_features)`,
          otherwise random.

        - `'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` which must both be provided.

    batch_size : int, default=1024
        Number of samples in each mini-batch. Large batch sizes
        give better long-term convergence at the cost of a slower start.

    beta_loss : float or {'frobenius', 'kullback-leibler',             'itakura-saito'}, default='frobenius'
        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.

    tol : float, default=1e-4
        Control early stopping based on the norm of the differences in `H`
        between 2 steps. To disable early stopping based on changes in `H`, set
        `tol` to 0.0.

    max_no_improvement : int, default=10
        Control early stopping based on the consecutive number of mini batches
        that does not yield an improvement on the smoothed cost function.
        To disable convergence detection based on cost function, set
        `max_no_improvement` to None.

    max_iter : int, default=200
        Maximum number of iterations over the complete dataset before
        timing out.

    alpha_W : float, default=0.0
        Constant that multiplies the regularization terms of `W`. Set it to zero
        (default) to have no regularization on `W`.

    alpha_H : float or "same", default="same"
        Constant that multiplies the regularization terms of `H`. Set it to zero to
        have no regularization on `H`. If "same" (default), it takes the same value as
        `alpha_W`.

    l1_ratio : float, default=0.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.

    forget_factor : float, default=0.7
        Amount of rescaling of past information. Its value could be 1 with
        finite datasets. Choosing values < 1 is recommended with online
        learning as more recent batches will weight more than past batches.

    fresh_restarts : bool, default=False
        Whether to completely solve for W at each step. Doing fresh restarts will likely
        lead to a better solution for a same number of iterations but it is much slower.

    fresh_restarts_max_iter : int, default=30
        Maximum number of iterations when solving for W at each step. Only used when
        doing fresh restarts. These iterations may be stopped early based on a small
        change of W controlled by `tol`.

    transform_max_iter : int, default=None
        Maximum number of iterations when solving for W at transform time.
        If None, it defaults to `max_iter`.

    random_state : int, RandomState instance or None, default=None
        Used for initialisation (when ``init`` == 'nndsvdar' or
        'random'), and in Coordinate Descent. Pass an int for reproducible
        results across multiple function calls.
        See :term:`Glossary <random_state>`.

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

    Attributes
    ----------
    components_ : ndarray of shape (n_components, n_features)
        Factorization matrix, sometimes called 'dictionary'.

    n_components_ : int
        The number of components. It is same as the `n_components` parameter
        if it was given. Otherwise, it will be same as the number of
        features.

    reconstruction_err_ : float
        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 started iterations over the whole dataset.

    n_steps_ : int
        Number of mini-batches processed.

    n_features_in_ : int
        Number of features seen during :term:`fit`.

    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.

    See Also
    --------
    NMF : Non-negative matrix factorization.
    MiniBatchDictionaryLearning : Finds a dictionary that can best be used to represent
        data using a sparse code.

    References
    ----------
    .. [1] :doi:`"Fast local algorithms for large scale nonnegative matrix and tensor
       factorizations" <10.1587/transfun.E92.A.708>`
       Cichocki, Andrzej, and P. H. A. N. Anh-Huy. IEICE transactions on fundamentals
       of electronics, communications and computer sciences 92.3: 708-721, 2009.

    .. [2] :doi:`"Algorithms for nonnegative matrix factorization with the
       beta-divergence" <10.1162/NECO_a_00168>`
       Fevotte, C., & Idier, J. (2011). Neural Computation, 23(9).

    .. [3] :doi:`"Online algorithms for nonnegative matrix factorization with the
       Itakura-Saito divergence" <10.1109/ASPAA.2011.6082314>`
       Lefevre, A., Bach, F., Fevotte, C. (2011). WASPA.

    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 MiniBatchNMF
    >>> model = MiniBatchNMF(n_components=2, init='random', random_state=0)
    >>> W = model.fit_transform(X)
    >>> H = model.components_
    r   Nr   r   r   r  r   )max_no_improvementrc   forget_factorfresh_restartsfresh_restarts_max_itertransform_max_iterr  r,   i   rg   r   r   r   r   r   gffffff?F   )r   rc   rl   r   rM  r   r   r   r   rN  rO  rP  rQ  rv   r   c                    t                                          ||||||||	|
|
  
         || _        || _        || _        || _        || _        || _        d S r:  )r#  r	  rM  rc   rN  rO  rP  rQ  )r  rb   r   rc   rl   r   rM  r   r   r   r   rN  rO  rP  rQ  rv   r   r+  s                    r!   r	  zMiniBatchNMF.__init__  s{    ( 	%% 	 	
 	
 	
 #5$*,'>$"4r#   c                    t                                          |           t          | j        |j        d                   | _        | j        | j        |j        d         z  z  | _        | j        dk     rdd| j        z
  z  | _	        n%| j        dk    rd| j        dz
  z  | _	        nd| _	        | j
        | j        n| j
        | _        | S )Nr   r   r   r5   r   )r#  r  rz   rc   r.   _batch_sizerN  _rhor  _gammarQ  r   _transform_max_iterr=  s     r!   r  zMiniBatchNMF._check_params  s    a    t
;; &4+;agaj+HI	 ?Qt!67DKK_q  3!67DKKDK
 &. MM( 	  r#   c           
         t          j        |                                | j        z            }t          j        |j        d         | j        f||j                  }|                                }|                     |          \  }}}	}t          |          D ]n}t          |||| j        ||	| j                  ^}}t          j        ||z
            t          j        |          z  }
| j        dk    r|
| j        k    r n||dd<   o|S )zMinimize the objective function w.r.t W.

        Update W with H being fixed, until convergence. This is the heart
        of `transform` but it's also used during `fit` when doing fresh restarts.
        r   r   N)r%   r   r|   r  r  r.   r   rs   r  rD   r   r  rW  r   r"   r   )r  r(   rF   r   r   rE   W_bufferr   rE  r   W_diffs              r!   _solve_WzMiniBatchNMF._solve_W  s    gaffhh!3344GQWQZ!34cIII6688 $(#?#?#B#B !Xqx 		 		A,1a(Hdk EA [X..Q?Fx!||$( 2 2HQQQKKr#   c                 (   |j         d         }|                     |          \  }}}}	| j        s||                     ||| j                  }n!t          |||| j        ||| j                  ^}}
| j        dk     r*d||t          j	        t          j
                  j        k     <   t          |||| j                  ||                                z  z   ||                                z  z   ||dz                                  z  z   |	|dz                                  z  z   |z  }|rlt          |||| j        ||	| j        | j        | j        | j        
  
        |dd<   | j        dk    r*d||t          j	        t          j
                  j        k     <   |S )z0Perform the update of W and H for one minibatch.r   Nr   r   r   )rl   r   r   r   r1   r   r   )r.   r  rO  r\  rP  r   r  rW  r%   r   r   r   rV   rA   r   _components_numerator_components_denominatorrV  )r  r(   rE   rF   r   rc   r   r   r   r   rE  
batch_costs               r!   _minibatch_stepzMiniBatchNMF._minibatch_step'  s   WQZ
 261M1Ma1P1P.(Hh  	!)aD$@AAAA,1a(Hdk EA
 ?Q.1Aa"(2:&&**+ Q1do66 ! ! !Q$%& !Q$%	&
 
  	6+/!!k,.I  AaaaD !##25!bhrz**../r#   c           
         |j         d         }|dz   }|dk    r!| j        rt          d| d| d|            dS | j        || _        n0||dz   z  }	t	          |	d          }	| j        d|	z
  z  ||	z  z   | _        | j        r t          d| d| d| d| j                    t          j        ||z
            t          j        |          z  }
| j        dk    r)|
| j        k    r| j        rt          d	| d|            d
S | j        | j        | j        k     rd| _	        | j        | _        n| xj	        dz  c_	        | j
        .| j	        | j
        k    r| j        rt          d| d|            d
S dS )z7Helper function to encapsulate the early stopping logicr   r   zMinibatch step /z: mean batch cost: FNz, ewa cost: z#Converged (small H change) at step Tz>Converged (lack of improvement in objective function) at step )r.   r   r   	_ewa_costrz   r   r"   r   _ewa_cost_min_no_improvementrM  )r  r(   r`  rF   H_bufferr   stepn_stepsrc   alphaH_diffs              r!   _minibatch_convergencez#MiniBatchNMF._minibatch_convergenceX  s    WQZ
 ax 199| YWWWwWW:WWXXX5
 >!'DNN)a-0EqMME!^q5y9J<NNDN < 	<$ < < < << <+/>< <   Q\**V[^^;8a<<Fdh..| NLDLL7LLMMM4 %$:L)L)L#$D !%D  A%   #/$(???| 0#0 0&-0 0   4ur#   Tr   c                 l   t          | |dt          j        t          j        g          }t	          d          5  |                     |||          \  }}}}ddd           n# 1 swxY w Y   t          |||| j        d          | _        |j	        d         | _
        || _        || _        || _        |S )	a  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} of shape (n_samples, n_features)
            Data matrix to be decomposed.

        y : Ignored
            Not used, present here for API consistency by convention.

        W : array-like of shape (n_samples, n_components), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            If `None`, uses the initialisation method specified in `init`.

        H : array-like of shape (n_components, n_features), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            If `None`, uses the initialisation method specified in `init`.

        Returns
        -------
        W : ndarray of shape (n_samples, n_components)
            Transformed data.
        r   r   Tr   r?  Nr   r   )r   r%   r   r   r	   r   rV   r  r@  r.   rA  r  rB  n_steps_)r  r(   r   rE   rF   r   ri  s          r!   r  zMiniBatchNMF.fit_transform  s   6 !>"*bj9Q
 
 
 $/// 	E 	E$($7$7Q!$7$D$D!Aq&'	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E 	E $4q!T_$$
 $
 $
  WQZs   A$$A(+A(c           
         t          |d           |                     |           |                                dk    r| j        dk    rt	          d          |j        d         }|                     ||||          \  }}|                                }|                                | _        t          j
        |j        |j                  | _        d| _        d| _        d| _        t!          || j                  }t%          j        |          }t)          t          j        || j        z                      }| j        |z  }	t/          t1          |	          |          D ]U\  }
}|                     ||         ||         ||          }|r#|                     ||         |||||
|	          r n||dd<   V| j        r|                     ||| j                  }|
dz   }	t)          t          j        |	|z                      }|| j        k    r.| j        dk    r#t?          j         d| j         dtB                     ||||	fS )	a  Learn a NMF model for the data X and returns the transformed data.

        Parameters
        ----------
        X : {ndarray, sparse matrix} of shape (n_samples, n_features)
            Data matrix to be decomposed.

        W : array-like of shape (n_samples, n_components), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            If `update_H=False`, it is initialised as an array of zeros, unless
            `solver='mu'`, then it is filled with values calculated by
            `np.sqrt(X.mean() / self._n_components)`.
            If `None`, uses the initialisation method specified in `init`.

        H : array-like of shape (n_components, n_features), default=None
            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.
            If `None`, uses the initialisation method specified in `init`.

        update_H : bool, default=True
            If True, both W and H will be estimated from initial guesses,
            this corresponds to a call to the `fit_transform` method.
            If False, only W will be estimated, this corresponds to a call
            to the `transform` method.

        Returns
        -------
        W : ndarray of shape (n_samples, n_components)
            Transformed data.

        H : ndarray of shape (n_components, n_features)
            Factorization matrix, sometimes called 'dictionary'.

        n_iter : int
            Actual number of started iterations over the whole dataset.

        n_steps : int
            Number of mini-batches processed.
        zMiniBatchNMF (input X)r   rD  r   Nr   zMaximum number of iterations z- reached. Increase it to improve convergence.)"r   r  rz   r  r/   r.   r  rs   r^  r%   onesr   r_  rd  re  rf  r   rU  	itertoolscycleintceilr   ziprD   ra  rl  rO  r\  rX  r   r  r  r   )r  r(   rE   rF   r   r   rg  batchesn_steps_per_iterri  rT   re   r`  r   s                 r!   r   zMiniBatchNMF._fit_transform  sW   P 	1677715577a<<DOq005   GAJ	 q!Q1116688 &'VVXX"')wqwag'F'F'F$ ! i)9::/'**rwy43C'CDDEE-"22E'NNG44 	 	HAu--ah%!XNNJ D77%*a9a   HQQQKK 	>aD$<==Aa%RWW'778899T]""tx!||M:DM : : : #   !VW$$r#   c                     t          |            t          | |dt          j        t          j        gd          }|                     || j        | j                  }|S )ag  Transform the data X according to the fitted MiniBatchNMF model.

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

        Returns
        -------
        W : ndarray of shape (n_samples, n_components)
            Transformed data.
        r   Fr   r   rG  )r   r   r%   r   r   r\  r  rX  )r  r(   rE   s      r!   rI  zMiniBatchNMF.transform"	  s`     	(:rz*
 
 
 MM!T-t/GHHr#   c                     t          | d          }t          | |dt          j        t          j        g|           }|sw|                     |           |                     |||d          \  }}|                                | _        t          j	        |j
        |j                  | _        d| _        n| j        }|                     |d|d	           |j
        d         | _        || _        | xj        d
z  c_        | S )af  Update the model using the data in `X` as a mini-batch.

        This method is expected to be called several times consecutively
        on different chunks of a dataset so as to implement out-of-core
        or online learning.

        This is especially useful when the whole dataset is too big to fit in
        memory at once (see :ref:`scaling_strategies`).

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

        y : Ignored
            Not used, present here for API consistency by convention.

        W : array-like of shape (n_samples, n_components), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            Only used for the first call to `partial_fit`.

        H : array-like of shape (n_components, n_features), default=None
            If `init='custom'`, it is used as initial guess for the solution.
            Only used for the first call to `partial_fit`.

        Returns
        -------
        self
            Returns the instance itself.
        r  r   ry  Tr   r   r   N)r   r   )hasattrr   r%   r   r   r  r  rs   r^  rp  r.   r   r_  rn  r  ra  rA  )r  r(   r   rE   rF   has_componentsrE  s          r!   partial_fitzMiniBatchNMF.partial_fit<	  s   @ !}55(:rz*$$
 
 
  		!q!!!??1Q?>>DAq)*D&+-717!'+J+J+JD(DMM AQa$777WQZr#   r,  r   )NNT)r-  r.  r/  r0  r   r  r   r   r   r3  r4  r	  r  r\  ra  rl  r   r  r   rI  r}  r6  r7  s   @r!   rL  rL    s        I IV$

)$'x!T&III4Px!T&AAAB"(4Af===>$+$,HXq$v$N$N$N#O'x!T&III4P$ $ $D    &5  "%&5 &5 &5 &5 &5 &5 &5P    4  6/ / /b: : :x \555* * * 65*X_% _% _% _%B  4 \555: : : 65: : : : :r#   rL  )F)Nrn   N)
r   r   r   r   r   r   Tr   FNrJ  r   )	rg   r   r   r   r   r   r   Tr   )NNr,   )Dr0  rq  r   r  abcr   mathr   numbersr   r   numpyr%   scipy.sparser'  r9   scipyr   _configr	   baser
   r   r   r   
exceptionsr   utilsr   r   r   r   utils._param_validationr   r   r   utils.deprecationr   utils.extmathr   r   r   utils.validationr   r   r   _cdnmf_fastr   r   r   r   r@   r"   r*   r3   rV   r?   r8   r   r   r   r   r   r   r   r   r   rL   r#   r!   <module>r     ss   ( (
                   " " " " " " " "                 $ $ $ $ $ $            , + + + + + R R R R R R R R R R R R         
 C B B B B B I I I I I I I I I I         
 , + + + + +
"(2:


"
! 
! 
!
( 
( 
(F F F"h h h hV  *  X X X Xv8 8 8D 	u u u u@ i i i iZ HL^ ^ ^ ^J b b b bJ O,D!D!K	  #(   
	b 
#b b b b bJI I I I I.0@-QT I I IXv v v v v( v v vrC
 C
 C
 C
 C
8 C
 C
 C
 C
 C
r#   