parameters: (X, n_components, init=None, eps=1e-6,
                      random_state=None)
return_statement: return W, H
docstring: 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: 'nndsvdar' 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 seed, RandomState instance, or None (default)
          Random number generator seed control, used in 'nndsvdar' and
          'random' modes.
  
      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
docstring_summary: Algorithms for NMF initialization.
docstring_tokens: Algorithms for NMF initialization .
function: def _initialize_nmf(X, n_components, init=None, eps=1e-6,
                      random_state=None):
      """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: 'nndsvdar' 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 seed, RandomState instance, or None (default)
          Random number generator seed control, used in 'nndsvdar' and
          'random' modes.
  
      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
      """
      check_non_negative(X, "NMF initialization")
      n_samples, n_features = X.shape
  
      if init is None:
          if n_components < n_features:
              init = 'nndsvd'
          else:
              init = 'random'
  
      # Random initialization
      if init == 'random':
          avg = np.sqrt(X.mean() / n_components)
          rng = check_random_state(random_state)
          H = avg * rng.randn(n_components, n_features)
          W = avg * rng.randn(n_samples, n_components)
          # we do not write np.abs(H, out=H) to stay compatible with
          # numpy 1.5 and earlier where the 'out' keyword is not
          # supported as a kwarg on ufuncs
          np.abs(H, H)
          np.abs(W, W)
          return W, H
  
      # NNDSVD initialization
      U, S, V = randomized_svd(X, n_components, random_state=random_state)
      W, H = np.zeros(U.shape), np.zeros(V.shape)
  
      # The leading singular triplet is non-negative
      # so it can be used as is for initialization.
      W[:, 0] = np.sqrt(S[0]) * np.abs(U[:, 0])
      H[0, :] = np.sqrt(S[0]) * np.abs(V[0, :])
  
      for j in range(1, n_components):
          x, y = U[:, j], V[j, :]
  
          # extract positive and negative parts of column vectors
          x_p, y_p = np.maximum(x, 0), np.maximum(y, 0)
          x_n, y_n = np.abs(np.minimum(x, 0)), np.abs(np.minimum(y, 0))
  
          # and their norms
          x_p_nrm, y_p_nrm = norm(x_p), norm(y_p)
          x_n_nrm, y_n_nrm = norm(x_n), norm(y_n)
  
          m_p, m_n = x_p_nrm * y_p_nrm, x_n_nrm * y_n_nrm
  
          # choose update
          if m_p > m_n:
              u = x_p / x_p_nrm
              v = y_p / y_p_nrm
              sigma = m_p
          else:
              u = x_n / x_n_nrm
              v = y_n / y_n_nrm
              sigma = m_n
  
          lbd = np.sqrt(S[j] * sigma)
          W[:, j] = lbd * u
          H[j, :] = lbd * v
  
      W[W < eps] = 0
      H[H < eps] = 0
  
      if init == "nndsvd":
          pass
      elif init == "nndsvda":
          avg = X.mean()
          W[W == 0] = avg
          H[H == 0] = avg
      elif init == "nndsvdar":
          rng = check_random_state(random_state)
          avg = X.mean()
          W[W == 0] = abs(avg * rng.randn(len(W[W == 0])) / 100)
          H[H == 0] = abs(avg * rng.randn(len(H[H == 0])) / 100)
      else:
          raise ValueError(
              'Invalid init parameter: got %r instead of one of %r' %
              (init, (None, 'random', 'nndsvd', 'nndsvda', 'nndsvdar')))
  
      return W, H
function_tokens: def _initialize_nmf ( X , n_components , init = None , eps = 1e-6 , random_state = None ) : check_non_negative ( X , "NMF initialization" ) n_samples , n_features = X . shape if init is None : if n_components < n_features : init = 'nndsvd' else : init = 'random' # Random initialization if init == 'random' : avg = np . sqrt ( X . mean ( ) / n_components ) rng = check_random_state ( random_state ) H = avg * rng . randn ( n_components , n_features ) W = avg * rng . randn ( n_samples , n_components ) # we do not write np.abs(H, out=H) to stay compatible with # numpy 1.5 and earlier where the 'out' keyword is not # supported as a kwarg on ufuncs np . abs ( H , H ) np . abs ( W , W ) return W , H # NNDSVD initialization U , S , V = randomized_svd ( X , n_components , random_state = random_state ) W , H = np . zeros ( U . shape ) , np . zeros ( V . shape ) # The leading singular triplet is non-negative # so it can be used as is for initialization. W [ : , 0 ] = np . sqrt ( S [ 0 ] ) * np . abs ( U [ : , 0 ] ) H [ 0 , : ] = np . sqrt ( S [ 0 ] ) * np . abs ( V [ 0 , : ] ) for j in range ( 1 , n_components ) : x , y = U [ : , j ] , V [ j , : ] # extract positive and negative parts of column vectors x_p , y_p = np . maximum ( x , 0 ) , np . maximum ( y , 0 ) x_n , y_n = np . abs ( np . minimum ( x , 0 ) ) , np . abs ( np . minimum ( y , 0 ) ) # and their norms x_p_nrm , y_p_nrm = norm ( x_p ) , norm ( y_p ) x_n_nrm , y_n_nrm = norm ( x_n ) , norm ( y_n ) m_p , m_n = x_p_nrm * y_p_nrm , x_n_nrm * y_n_nrm # choose update if m_p > m_n : u = x_p / x_p_nrm v = y_p / y_p_nrm sigma = m_p else : u = x_n / x_n_nrm v = y_n / y_n_nrm sigma = m_n lbd = np . sqrt ( S [ j ] * sigma ) W [ : , j ] = lbd * u H [ j , : ] = lbd * v W [ W < eps ] = 0 H [ H < eps ] = 0 if init == "nndsvd" : pass elif init == "nndsvda" : avg = X . mean ( ) W [ W == 0 ] = avg H [ H == 0 ] = avg elif init == "nndsvdar" : rng = check_random_state ( random_state ) avg = X . mean ( ) W [ W == 0 ] = abs ( avg * rng . randn ( len ( W [ W == 0 ] ) ) / 100 ) H [ H == 0 ] = abs ( avg * rng . randn ( len ( H [ H == 0 ] ) ) / 100 ) else : raise ValueError ( 'Invalid init parameter: got %r instead of one of %r' % ( init , ( None , 'random' , 'nndsvd' , 'nndsvda' , 'nndsvdar' ) ) ) return W , H