#NEWLINE# def _initialize_nmf( X , n_components , init = None , eps = 1e-06 , random_state = None ) : #NEWLINE# #INDENT# "Algorithms for NMF initialization.\n\n    Computes an initial guess for the non-negative\n    rank k matrix approximation for X: X = WH\n\n    Parameters\n    ----------\n    X : array-like, shape (n_samples, n_features)\n        The data matrix to be decomposed.\n\n    n_components : integer\n        The number of components desired in the approximation.\n\n    init :  None | 'random' | 'nndsvd' | 'nndsvda' | 'nndsvdar'\n        Method used to initialize the procedure.\n        Default: 'nndsvdar' if n_components < n_features, otherwise 'random'.\n        Valid options:\n\n        - 'random': non-negative random matrices, scaled with:\n            sqrt(X.mean() / n_components)\n\n        - 'nndsvd': Nonnegative Double Singular Value Decomposition (NNDSVD)\n            initialization (better for sparseness)\n\n        - 'nndsvda': NNDSVD with zeros filled with the average of X\n            (better when sparsity is not desired)\n\n        - 'nndsvdar': NNDSVD with zeros filled with small random values\n            (generally faster, less accurate alternative to NNDSVDa\n            for when sparsity is not desired)\n\n        - 'custom': use custom matrices W and H\n\n    eps : float\n        Truncate all values less then this in output to zero.\n\n    random_state : int seed, RandomState instance, or None (default)\n        Random number generator seed control, used in 'nndsvdar' and\n        'random' modes.\n\n    Returns\n    -------\n    W : array-like, shape (n_samples, n_components)\n        Initial guesses for solving X ~= WH\n\n    H : array-like, shape (n_components, n_features)\n        Initial guesses for solving X ~= WH\n\n    References\n    ----------\n    C. Boutsidis, E. Gallopoulos: SVD based initialization: A head start for\n    nonnegative matrix factorization - Pattern Recognition, 2008\n    http://tinyurl.com/nndsvd\n    " #NEWLINE# check_non_negative ( X , 'NMF initialization' ) #NEWLINE# ( n_samples , n_features ) = X . shape #NEWLINE# if ( init is None ) : #NEWLINE# #INDENT# if ( n_components < n_features ) : #NEWLINE# #INDENT# init = 'nndsvd' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# init = 'random' #NEWLINE# #UNINDENT# #UNINDENT# if ( init == 'random' ) : #NEWLINE# #INDENT# avg = np . sqrt ( ( X . mean ( ) / n_components ) ) #NEWLINE# rng = check_random_state ( random_state ) #NEWLINE# H = ( U * rng . randn ( n_components , n_features ) ) #NEWLINE# W = ( avg * rng . randn ( n_samples , n_components ) ) #NEWLINE# np . abs ( H , H ) #NEWLINE# np . abs ( W , W ) #NEWLINE# return ( W , H ) #NEWLINE# #UNINDENT# ( U , S , V ) = randomized_svd ( X , n_components , random_state = random_state ) #NEWLINE# ( W , H ) = ( np . zeros ( U . shape ) , np . zeros ( V . shape ) ) #NEWLINE# W [ : , 0 ] = ( np . sqrt ( S [ 0 ] ) * np . abs ( U [ : , 0 ] ) ) #NEWLINE# H [ 0 , : ] = ( np . sqrt ( S [ 0 ] ) * np . abs ( V [ 0 , : ] ) ) #NEWLINE# for j in range ( 1 , n_components ) : #NEWLINE# #INDENT# ( x , y ) = ( U [ : , j ] , V [ j , : ] ) #NEWLINE# ( x_p , y_p ) = ( np . maximum ( x , 0 ) , np . maximum ( y , 0 ) ) #NEWLINE# ( x_n , y_n ) = ( np . abs ( np . minimum ( x , 0 ) ) , np . abs ( np . minimum ( y , 0 ) ) ) #NEWLINE# ( x_p_nrm , y_p_nrm ) = ( norm ( x_p ) , norm ( y_p ) ) #NEWLINE# ( x_n_nrm , y_n_nrm ) = ( norm ( x_n ) , norm ( y_n ) ) #NEWLINE# ( m_p , m_n ) = ( ( x_p_nrm * y_p_nrm ) , ( x_n_nrm * y_n_nrm ) ) #NEWLINE# if ( m_p > m_n ) : #NEWLINE# #INDENT# u = ( x_p / x_p_nrm ) #NEWLINE# v = ( y_p / y_p_nrm ) #NEWLINE# sigma = m_p #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# u = ( x_n / x_n_nrm ) #NEWLINE# v = ( y_n / y_n_nrm ) #NEWLINE# sigma = m_n #NEWLINE# #UNINDENT# lbd = np . sqrt ( ( S [ j ] * sigma ) ) #NEWLINE# W [ : , j ] = ( lbd * u ) #NEWLINE# H [ j , : ] = ( lbd * v ) #NEWLINE# #UNINDENT# W [ ( W < eps ) ] = 0 #NEWLINE# H [ ( H < eps ) ] = 0 #NEWLINE# if ( init == 'nndsvd' ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( init == 'nndsvda' ) : #NEWLINE# #INDENT# avg = X . mean ( ) #NEWLINE# W [ ( W == 0 ) ] = avg #NEWLINE# H [ ( H == 0 ) ] = avg #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( init == 'nndsvdar' ) : #NEWLINE# #INDENT# rng = check_random_state ( random_state ) #NEWLINE# avg = X . mean ( ) #NEWLINE# W [ ( W == 0 ) ] = abs ( ( ( avg * rng . randn ( len ( W [ ( W == 0 ) ] ) ) ) / 100 ) ) #NEWLINE# H [ ( H == 0 ) ] = abs ( ( ( avg * rng . randn ( len ( H [ ( H == 0 ) ] ) ) ) / 100 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise ValueError ( ( 'Invalid init parameter: got %r instead of one of %r' % ( init , ( None , 'random' , 'nndsvd' , 'nndsvda' , 'nndsvdar' ) ) ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return ( W , H )