Skip to content

Instantly share code, notes, and snippets.

@nicoguaro
Created January 16, 2018 15:14
Show Gist options
  • Save nicoguaro/9bf6fb94f555c9cedeec6200bcd365a2 to your computer and use it in GitHub Desktop.
Save nicoguaro/9bf6fb94f555c9cedeec6200bcd365a2 to your computer and use it in GitHub Desktop.
Dimensional reduction using non-linear methods present in scikit-learn
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Dimensional reduction using non-linear methods present in scikit-learn
Based on an example by Jake Vanderplas
"""
from time import time
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.ticker import NullFormatter
import cmocean
from sklearn import manifold
# Next line to silence pyflakes. This import is needed.
Axes3D
def toroidal_helix(n_points=100, rad_out=2, rad_in=0.5, n_turns=10,
random_state=None, noise=0.0):
from numpy import linspace, zeros, sin, cos, pi, random
random.seed(seed=random_state)
t = linspace(-1, 1, n_points)
X = zeros((n_points, 3))
x = (rad_out + rad_in*cos(n_turns*pi*t)) * cos(pi*t)
y = (rad_out + rad_in*cos(n_turns*pi*t)) * sin(pi*t)
z = rad_in*sin(n_turns*pi*t)
X[:, 0] = x + rad_in*random.normal(size=n_points, scale=noise)
X[:, 1] = y + rad_in*random.normal(size=n_points, scale=noise)
X[:, 2] = z + rad_in*random.normal(size=n_points, scale=noise)
return X, t
n_points = 500
cmap = cmocean.cm.phase
#cmap = plt.cm.Spectral
X, color = toroidal_helix(n_points, random_state=0, noise=0.1)
n_neighbors = 6
n_components = 2
point_size = 5
fig = plt.figure(figsize=(15, 6))
plt.suptitle("Manifold Learning with %i points, %i neighbors"
% (1000, n_neighbors), fontsize=14)
ax = fig.add_subplot(151, projection='3d')
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=color, cmap=cmap, s=5)
ax.view_init(30, -60)
ax.set_zlim(-2, 2)
methods = ['standard', 'ltsa', 'hessian', 'modified']
labels = ['LLE', 'LTSA', 'Hessian LLE', 'Modified LLE']
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=color, cmap=cmap, s=5)
plt.title("%s (%.2g sec)" % (labels[i], t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis('tight')
t0 = time()
Y = manifold.Isomap(n_neighbors, n_components).fit_transform(X)
t1 = time()
print("Isomap: %.2g sec" % (t1 - t0))
ax = fig.add_subplot(257)
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=cmap, s=5)
plt.title("Isomap (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis('tight')
t0 = time()
mds = manifold.MDS(n_components, max_iter=100, n_init=1)
Y = mds.fit_transform(X)
t1 = time()
print("MDS: %.2g sec" % (t1 - t0))
ax = fig.add_subplot(258)
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=cmap, s=5)
plt.title("MDS (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis('tight')
t0 = time()
se = manifold.SpectralEmbedding(n_components=n_components,
n_neighbors=n_neighbors)
Y = se.fit_transform(X)
t1 = time()
print("SpectralEmbedding: %.2g sec" % (t1 - t0))
ax = fig.add_subplot(259)
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=cmap, s=5)
plt.title("SpectralEmbedding (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis('tight')
t0 = time()
tsne = manifold.TSNE(n_components=n_components, init='pca', random_state=0)
Y = tsne.fit_transform(X)
t1 = time()
print("t-SNE: %.2g sec" % (t1 - t0))
ax = fig.add_subplot(2, 5, 10)
plt.scatter(Y[:, 0], Y[:, 1], c=color, cmap=cmap, s=5)
plt.title("t-SNE (%.2g sec)" % (t1 - t0))
ax.xaxis.set_major_formatter(NullFormatter())
ax.yaxis.set_major_formatter(NullFormatter())
plt.axis('tight')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment