def nmf_init(data, clusters, k, init='enhanced'):
    """
    runs enhanced NMF initialization from clusterings (Gong 2013)

    There are 3 options for init:
        enhanced - uses EIn-NMF from Gong 2013
        basic - uses means for W, assigns H such that the chosen cluster for a given cell has value 0.75 and all others have 0.25/(k-1).
        nmf - uses means for W, and assigns H using the NMF objective while holding W constant.
    """
    init_w = np.zeros((data.shape[0], k))
    if sparse.issparse(data):
        for i in range(k):
            if data[:,clusters==i].shape[1]==0:
                point = np.random.randint(0, data.shape[1])
                init_w[:,i] = data[:,point].toarray().flatten()
            else:
                init_w[:,i] = np.array(data[:,clusters==i].mean(1)).flatten()
    else:
        for i in range(k):
            if data[:,clusters==i].shape[1]==0:
                point = np.random.randint(0, data.shape[1])
                init_w[:,i] = data[:,point].flatten()
            else:
                init_w[:,i] = data[:,clusters==i].mean(1)
    init_h = np.zeros((k, data.shape[1]))
    if init == 'enhanced':
        distances = np.zeros((k, data.shape[1]))
        for i in range(k):
            for j in range(data.shape[1]):
                distances[i,j] = np.sqrt(((data[:,j] - init_w[:,i])**2).sum())
        for i in range(k):
            for j in range(data.shape[1]):
                init_h[i,j] = 1/((distances[:,j]/distances[i,j])**(-2)).sum()
    elif init == 'basic':
        init_h = initialize_from_assignments(clusters, k)
    elif init == 'nmf':
        init_h_, _, n_iter = non_negative_factorization(data.T, n_components=k, init='custom', update_H=False, H=init_w.T)
        init_h = init_h_.T
    return init_w, init_h
def nmf_init ( data , clusters , k , init = 'enhanced' ) : init_w = np . zeros ( ( data . shape [ 0 ] , k ) ) if sparse . issparse ( data ) : for i in range ( k ) : if data [ : , clusters == i ] . shape [ 1 ] == 0 : point = np . random . randint ( 0 , data . shape [ 1 ] ) init_w [ : , i ] = data [ : , point ] . toarray ( ) . flatten ( ) else : init_w [ : , i ] = np . array ( data [ : , clusters == i ] . mean ( 1 ) ) . flatten ( ) else : for i in range ( k ) : if data [ : , clusters == i ] . shape [ 1 ] == 0 : point = np . random . randint ( 0 , data . shape [ 1 ] ) init_w [ : , i ] = data [ : , point ] . flatten ( ) else : init_w [ : , i ] = data [ : , clusters == i ] . mean ( 1 ) init_h = np . zeros ( ( k , data . shape [ 1 ] ) ) if init == 'enhanced' : distances = np . zeros ( ( k , data . shape [ 1 ] ) ) for i in range ( k ) : for j in range ( data . shape [ 1 ] ) : distances [ i , j ] = np . sqrt ( ( ( data [ : , j ] - init_w [ : , i ] ) ** 2 ) . sum ( ) ) for i in range ( k ) : for j in range ( data . shape [ 1 ] ) : init_h [ i , j ] = 1 / ( ( distances [ : , j ] / distances [ i , j ] ) ** ( - 2 ) ) . sum ( ) elif init == 'basic' : init_h = initialize_from_assignments ( clusters , k ) elif init == 'nmf' : init_h_ , _ , n_iter = non_negative_factorization ( data . T , n_components = k , init = 'custom' , update_H = False , H = init_w . T ) init_h = init_h_ . T return init_w , init_h
runs enhanced NMF initialization from clusterings (Gong 2013)

    There are 3 options for init:
        enhanced - uses EIn-NMF from Gong 2013
        basic - uses means for W, assigns H such that the chosen cluster for a given cell has value 0.75 and all others have 0.25/(k-1).
        nmf - uses means for W, and assigns H using the NMF objective while holding W constant.