text_plain_output_2.png text_plain_output_1.png image_output_1.png
import umap
from sklearn import manifold
from sklearn.decomposition import PCA
from sklearn.decomposition import FactorAnalysis
from sklearn.decomposition import NMF
from sklearn.decomposition import FastICA
from sklearn.decomposition import FactorAnalysis
from sklearn.decomposition import LatentDirichletAllocation
from sklearn.ensemble import RandomTreesEmbedding
from sklearn.random_projection import SparseRandomProjection
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.pipeline import make_pipeline
from sklearn.decomposition import TruncatedSVD
from collections import OrderedDict
from functools import partial
from matplotlib.ticker import NullFormatter
n_neighbors = 10
n_components = 2
LLE = partial(manifold.LocallyLinearEmbedding, n_neighbors, n_components, eigen_solver='auto')
methods = OrderedDict()
methods['PCA'] = PCA()
methods['umap'] = umap.UMAP(n_components=n_components)
methods['t-SNE'] = manifold.TSNE(n_components=n_components, init='pca', random_state=0)
methods['ICA'] = FastICA(n_components=n_components, random_state=0)
methods['FA'] = FactorAnalysis(n_components=n_components, random_state=0)
methods['MDS'] = manifold.MDS(n_components, max_iter=100, n_init=1)
methods['SE'] = manifold.SpectralEmbedding(n_components=n_components, n_neighbors=n_neighbors)
methods['NMF'] = NMF(n_components=n_components, init='random', random_state=0)
methods['RandProj'] = SparseRandomProjection(n_components=n_components, random_state=42)
rand_trees_embed = make_pipeline(RandomTreesEmbedding(n_estimators=200, random_state=0, max_depth=5), TruncatedSVD(n_components=n_components))
methods['RandTrees'] = rand_trees_embed
methods['LatDirAll'] = LatentDirichletAllocation(n_components=n_components, random_state=0)
list_fast_methods = ['PCA', 'umap', 'FA', 'NMF', 'RandProj', 'RandTrees']
list_slow_methods = ['t-SNE', 'LLE', 'Modified LLE', 'Isomap', 'MDS', 'SE', 'LatDirAll', 'LTSA', 'Hessian LLE']
fig = plt.figure(figsize=(25, 16))
plt.suptitle(str_data_inf + ' n_samples=' + str(len(X)), fontsize=20)
c = 0
for i, (label, method) in enumerate(methods.items()):
    if label not in list_slow_methods:
        continue
    t0 = time.time()
    try:
        if isinstance(X, pd.DataFrame):
            r = method.fit_transform(X.iloc[:N, :])
        else:
            r = method.fit_transform(X[:N, :])
    except:
        print('Got Exception', label)
        continue
    t1 = time.time()
    print('%s: %.2g sec' % (label, t1 - t0))
    c += 1
    fig.add_subplot(2, 3, c)
    if isinstance(v4color, pd.Series):
        sns.scatterplot(x=r[:, 0], y=r[:, 1], hue=v4color.iloc[:N], palette=palette1, marker=marker1, alpha=alpha1)
    else:
        sns.scatterplot(x=r[:, 0], y=r[:, 1], hue=v4color[:N], palette=palette1, marker=marker1, alpha=alpha1)
    plt.title(label, fontsize=20)
    if c == 1:
        plt.setp(ax.get_legend().get_texts(), fontsize=12)
        plt.setp(ax.get_legend().get_title(), fontsize=12)
        plt.legend(bbox_to_anchor=(1.15, 1.0), loc='upper right')
    else:
        plt.legend('')
plt.show()