def swiss_roll_test(): import matplotlib.pyplot as plt plt.style.use('ggplot') from time import time from sklearn import manifold, datasets from sklearn.manifold import SpectralEmbedding from lpproj import LocalityPreservingProjection n_points = 1000 X, color = datasets.samples_generator.make_s_curve(n_points, random_state=0) n_neighbors = 20 n_components = 2 # original lE algorithm t0 = time() ml_model = SpectralEmbedding(n_neighbors=n_neighbors, n_components=n_components) Y = ml_model.fit_transform(X) t1 = time() # 2d projection fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(5, 10)) ax[0].scatter(Y[:, 0], Y[:, 1], c=color, label='scikit') ax[0].set_title('Sklearn-LE: {t:.2g}'.format(t=t1 - t0)) # Jakes LPP Algorithm t0 = time() ml_model = LocalityPreservingProjection(n_components=n_components) ml_model.fit(X) Y = ml_model.transform(X) t1 = time() ax[1].scatter(Y[:, 0], Y[:, 1], c=color, label='Jakes Algorithm') ax[1].set_title('Jakes LPP: {t:.2g}'.format(t=t1 - t0)) # my SSSE algorith, t0 = time() ml_model = LocalityPreservingProjections(weight='angle', n_components=n_components, n_neighbors=n_neighbors, sparse=True, eig_solver='dense') ml_model.fit(X) Y = ml_model.transform(X) t1 = time() ax[2].scatter(Y[:, 0], Y[:, 1], c=color, label='My LPP Algorithm') ax[2].set_title('My LPP: {t:.2g}'.format(t=t1 - t0)) plt.show()
def swiss_roll_test(): n_points = 1000 X, color = datasets.samples_generator.make_s_curve(n_points, random_state=0) n_neighbors=20 n_components=2 # original lE algorithm t0 = time() ml_model = SpectralEmbedding(n_neighbors=n_neighbors, n_components=n_components) Y = ml_model.fit_transform(X) t1 = time() # 2d projection fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(5,10)) ax[0].scatter(Y[:,0], Y[:,1], c=color, label='scikit') ax[0].set_title('Sklearn-LE: {t:.2g}'.format(t=t1-t0)) # Jakes LPP Algorithm t0 = time() ml_model = LocalityPreservingProjection(n_components=n_components) ml_model.fit(X) Y = ml_model.transform(X) t1 = time() ax[1].scatter(Y[:,0], Y[:,1], c=color, label='Jakes Algorithm') ax[1].set_title('Jakes LPP: {t:.2g}'.format(t=t1-t0)) # my SSSE algorith, t0 = time() ml_model = LocalityPreservingProjections(weight='angle', n_components=n_components, n_neighbors=n_neighbors, sparse=True, eig_solver='dense') ml_model.fit(X) Y = ml_model.transform(X) t1 = time() ax[2].scatter(Y[:,0], Y[:,1], c=color, label='My LPP Algorithm') ax[2].set_title('My LPP: {t:.2g}'.format(t=t1-t0)) plt.show()
def test(g=dataset_generator.Generator(), mode='show'): alpha_channel = 0.7 spectre1 = np.array([ np.array([1, 0, 0, alpha_channel]), np.array([1, 1, 0, alpha_channel]) ]) spectre2 = np.array([ np.array([0, 0, 1, alpha_channel]), np.array([0, 1, 0, alpha_channel]) ]) spectre3 = np.array([ np.array([1, 0, 1, alpha_channel]), np.array([0, 1, 1, alpha_channel]) ]) n_neighbors = 10 n_components = 2 fig = plt.figure(figsize=(15, 8)) se = SpectralEmbedding(n_components=n_components, n_neighbors=n_neighbors) '''--------------------------------------------------------------------------------------------------------------''' dataset, dataset_colors = g.generate_manifold(f, color_data=spectre1) ax = fig.add_subplot(241, projection='3d') ax.scatter(dataset[:, 0], dataset[:, 1], dataset[:, 2], c=dataset_colors, marker='s', edgecolors='none') dataset_embedding = se.fit_transform(dataset) ax = fig.add_subplot(245) ax.scatter(dataset_embedding[:, 0], dataset_embedding[:, 1], c=dataset_colors, cmap=plt.cm.Spectral, marker='s', edgecolors='none') '''--------------------------------------------------------------------------------------------------------------''' new_points, new_colors = g.generate_manifold(f, spectre2) new_points_embedding = np.array( list(se.transform(point.reshape(1, -1))[0] for point in new_points)) ax = fig.add_subplot(242, projection='3d') ax.scatter(dataset[:, 0], dataset[:, 1], dataset[:, 2], c=dataset_colors, marker='s', edgecolors='none') ax.scatter(new_points[:, 0], new_points[:, 1], new_points[:, 2], c=new_colors, marker='s', edgecolors='none') ax = fig.add_subplot(246) ax.scatter(dataset_embedding[:, 0], dataset_embedding[:, 1], c=dataset_colors, marker='s', edgecolor='none') ax.scatter(new_points_embedding[:, 0], new_points_embedding[:, 1], c=new_colors, marker='s', edgecolors='none') '''--------------------------------------------------------------------------------------------------------------''' re_colors = dataset_generator.Generator.generate_colors( None, dataset.shape[0], spectre3) re_embedding = np.array( list(se.transform(point.reshape(1, -1))[0] for point in dataset)) ax = fig.add_subplot(243, projection='3d') ax.scatter(dataset[:, 0], dataset[:, 1], dataset[:, 2], c=dataset_colors, marker='s', edgecolors='none') ax.scatter(dataset[:, 0], dataset[:, 1], dataset[:, 2], c=re_colors, marker='s', edgecolors='none') ax = fig.add_subplot(247) ax.scatter(dataset_embedding[:, 0], dataset_embedding[:, 1], c=dataset_colors, marker='s', edgecolor='none') ax.scatter(re_embedding[:, 0], re_embedding[:, 1], c=re_colors, marker='s', edgecolors='none') '''--------------------------------------------------------------------------------------------------------------''' dataset_reconstruction = np.array( list( se.inverse_transform(point.reshape(1, -1))[0] for point in dataset_embedding)) ax = fig.add_subplot(244) ax.scatter(dataset_embedding[:, 0], dataset_embedding[:, 1], c=dataset_colors, marker='s', edgecolor='none') ax = fig.add_subplot(248, projection='3d') ax.scatter(dataset_reconstruction[:, 0], dataset_reconstruction[:, 1], dataset_reconstruction[:, 2], c=dataset_colors, marker='s', edgecolors='none') ax.scatter(dataset[:, 0], dataset[:, 1], dataset[:, 2], c=re_colors, marker='s', edgecolors='none') '''--------------------------------------------------------------------------------------------------------------''' plt.axis('tight') if mode == 'show': plt.show() if f == 1000 or input('save? y/N') != 'y': pass else: save(fig, dataset, dataset_embedding, new_points, new_points_embedding) else: save(fig, dataset, dataset_embedding, new_points, new_points_embedding)