def make_s_curve(**kargs): n_points = 1000 X, color = datasets.samples_generator.make_s_curve(n_points, random_state=0) # [log] dim > X: (1000, 3), color: (1000,) n_neighbors = 10 n_components = 2 outputdir = kargs.get('outputdir', os.path.join(os.getcwd(), 'plot')) if not os.path.exists(outputdir): os.makedirs(outputdir) # base directory fig = plt.figure(figsize=(15, 8)) plt.suptitle("Manifold Learning with %i points, %i neighbors" % (1000, n_neighbors), fontsize=14) try: # compatibility matplotlib < 1.0 ax = fig.add_subplot(251, projection='3d') ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=plt.cm.Spectral) ax.view_init(4, -72) except: ax = fig.add_subplot(251, projection='3d') plt.scatter(X[:, 0], X[:, 2], c=color, cmap=plt.cm.Spectral) ext = 'tif' fpath = os.path.join(TestDir, name_image_file(descriptor='scurve', **kargs)) plt.savefig(fpath, dpi=300) plt.close() return (X, color)
def isomap(X, **kargs): # import matplotlib.cm as cm n_neighbors = 10 n_components = 2 y = kargs.get('y', None) # ulabels = labels = y = kargs.get('y', None) # n_labels = X.shape[0] # if labels is not None: # ulabels = set(labels) # n_labels = len(ulabels) t0 = time() X_proj = Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X) t1 = time() print("Isomap: %.2g sec" % (t1 - t0)) outputdir = kargs.get('outputdir', os.path.join(os.getcwd(), 'plot')) if not os.path.exists(outputdir): os.makedirs(outputdir) # base directory plot_mode = kargs.get('graph_mode', 'seaborn') fpath = os.path.join(TestDir, name_image_file( descriptor='isomap', **kargs)) # kargs: seq_ptype, d2v_method if plot_mode.startswith('s'): scatter2(X_proj, y) plt.savefig(fpath, dpi=300) else: # [params] colors n_points = X.shape[0] colors = itertools.cycle(cm.rainbow(np.linspace(0, 1, n_points))) # for i, c in enumerate(itertools.cycle(cm.rainbow(np.linspace(0, 1, n_points)))): # for i, c in enumerate(itertools.cycle(["r", "b", "g"])): lcmap = { 0: 'g', 1: 'b', 2: 'r', } colors = (lcmap[l] for l in y) # loop through y fig = plt.figure(figsize=(15, 8)) ax = fig.add_subplot(257) plt.scatter(X_proj[:, 0], X_proj[:, 1], c=next(colors), cmap=plt.cm.Spectral) # plt.title("Isomap (%.2g sec)" % (t1 - t0)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) plt.axis('tight') plt.savefig(fpath, dpi=300) plt.close() return X_proj
def mds(X, **kargs): # import matplotlib.cm as cm # n_neighbors = 10 n_components = 2 y = kargs.get('y', None) t0 = time() mds = manifold.MDS(n_components, max_iter=100, n_init=1) X_proj = Y = mds.fit_transform(X) t1 = time() print("MDS: %.2g sec" % (t1 - t0)) outputdir = kargs.get('outputdir', os.path.join(os.getcwd(), 'plot')) if not os.path.exists(outputdir): os.makedirs(outputdir) # base directory plot_mode = kargs.get('graph_mode', 'seaborn') fpath = os.path.join(TestDir, name_image_file( descriptor='mds', **kargs)) # kargs: seq_ptype, d2v_method if plot_mode.startswith('s'): scatter2(X_proj, y) plt.savefig(fpath, dpi=300) else: # [params] colors n_points = X_proj.shape[0] # colors = itertools.cycle(cm.rainbow(np.linspace(0, 1, n_points))) lcmap = { 0: 'g', 1: 'b', 2: 'r', } colors = (lcmap[l] for l in y) # loop through y fig = plt.figure(figsize=(15, 8)) ax = fig.add_subplot(258) plt.scatter(X_proj[:, 0], X_proj[:, 1], c=next(colors), cmap=plt.cm.Spectral) plt.title("MDS (%.2g sec)" % (t1 - t0)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) plt.axis('tight') plt.savefig(fpath, dpi=300) plt.close() return Y
def locally_linear(X, **kargs): # import matplotlib.cm as cm n_neighbors = 10 n_components = 2 y = kargs.get('y', None) methods = ['standard', 'ltsa', 'hessian', 'modified'] labels = ['LLE', 'LTSA', 'Hessian LLE', 'Modified LLE'] plt.clf() fig = plt.figure(figsize=(15, 8)) n_points = X.shape[0] outputdir = kargs.get('outputdir', os.path.join(os.getcwd(), 'plot')) if not os.path.exists(outputdir): os.makedirs(outputdir) # base directory # configure color(s) colors = itertools.cycle(cm.rainbow(np.linspace(0, 1, n_points))) for i, method in enumerate(methods): t0 = time() Y = manifold.LocallyLinearEmbedding(n_neighbors, n_components, eigen_solver='auto', method=method).fit_transform(X) t1 = time() print("%s: %.2g sec" % (methods[i], t1 - t0)) ax = fig.add_subplot(252 + i) plt.scatter(Y[:, 0], Y[:, 1], c=next(colors), cmap=plt.cm.Spectral) plt.title("%s (%.2g sec)" % (labels[i], t1 - t0)) ax.xaxis.set_major_formatter(NullFormatter()) ax.yaxis.set_major_formatter(NullFormatter()) plt.axis('tight') # scatter2(X_proj, y) fpath = os.path.join( TestDir, name_image_file(descriptor=method, **kargs)) # kargs: seq_ptype, d2v_method plt.savefig(fpath, dpi=300) plt.close() return