def nmf_tsne(data, k, n_runs=10, init='enhanced', **params):
    """
    runs tsne-consensus-NMF

    1. run a bunch of NMFs, get W and H
    2. run tsne + km on all WH matrices
    3. run consensus clustering on all km results
    4. use consensus clustering as initialization for a new run of NMF
    5. return the W and H from the resulting NMF run
    """
    clusters = []
    nmf = NMF(k)
    tsne = TSNE(2)
    km = KMeans(k)
    for i in range(n_runs):
        w = nmf.fit_transform(data)
        h = nmf.components_
        tsne_wh = tsne.fit_transform(w.dot(h).T)
        clust = km.fit_predict(tsne_wh)
        clusters.append(clust)
    clusterings = np.vstack(clusters)
    consensus = CE.cluster_ensembles(clusterings, verbose=False, N_clusters_max=k)
    nmf_new = NMF(k, init='custom')
    # TODO: find an initialization for the consensus W and H
    init_w, init_h = nmf_init(data, consensus, k, init)
    W = nmf_new.fit_transform(data, W=init_w, H=init_h)
    H = nmf_new.components_
    return W, H
def nmf_tsne ( data , k , n_runs = 10 , init = 'enhanced' , ** params ) : clusters = [ ] nmf = NMF ( k ) tsne = TSNE ( 2 ) km = KMeans ( k ) for i in range ( n_runs ) : w = nmf . fit_transform ( data ) h = nmf . components_ tsne_wh = tsne . fit_transform ( w . dot ( h ) . T ) clust = km . fit_predict ( tsne_wh ) clusters . append ( clust ) clusterings = np . vstack ( clusters ) consensus = CE . cluster_ensembles ( clusterings , verbose = False , N_clusters_max = k ) nmf_new = NMF ( k , init = 'custom' ) init_w , init_h = nmf_init ( data , consensus , k , init ) W = nmf_new . fit_transform ( data , W = init_w , H = init_h ) H = nmf_new . components_ return W , H
runs tsne-consensus-NMF

    1. run a bunch of NMFs, get W and H
    2. run tsne + km on all WH matrices
    3. run consensus clustering on all km results
    4. use consensus clustering as initialization for a new run of NMF
    5. return the W and H from the resulting NMF run