In [1]:
import numpy as np
from numpy import linalg
import numpy.random as npr
import pylab as pl
%matplotlib inline

Goal:

  • Search through space of efficient spectral algorithms for solutions to difficult problems, e.g.
    • Dimensionality reduction:
    • Subspace dynamics: given a high-dimensional

Building blocks:

  • Covariance
  • Spectra
  • Decompositions
  • Derivatives
    • Jacobian
    • Hessian
    • Etc.
  • Convolutions
  • Slicing
  • Copying

In [ ]:
# example of what should come out: PCA

In [2]:
import sklearn

In [3]:
from sklearn import datasets

In [9]:
X = datasets.make_swiss_roll(1000,0.05)[0]

In [10]:
X.shape


Out[10]:
(1000, 3)

In [11]:
from sklearn.decomposition import PCA

In [26]:
pca = PCA(2)

In [27]:
y = pca.fit_transform(X)

In [28]:
X2 = pca.inverse_transform(y)

In [29]:
np.sum(np.abs(X2-X))


Out[29]:
7537.6674630939006

In [30]:
np.mean(X,0)


Out[30]:
array([  2.19610597,  10.42289769,   0.13858269])

In [34]:
cov = np.cov(X,rowvar=0)
cov.shape


Out[34]:
(3, 3)

In [39]:
W = linalg.eig(cov)[1]
W


Out[39]:
array([[-0.33264407, -0.89430932,  0.2992637 ],
       [ 0.17698896, -0.37089915, -0.91165165],
       [-0.92629522,  0.25028915, -0.28166028]])

In [47]:
y = W.dot(X.T)

In [79]:
def transform(X,dim=2):
    #mean = np.mean(X,0)
    cov = np.cov(X,rowvar=0)
    evals,evecs = linalg.eig(cov)
    W = evecs[:dim]
    return W.dot(X.T).T

In [107]:
X = npr.randn(1000,2)
X[:,0] += npr.rand(1000)*20-10
X[:,1] += X[:,0] + 0.05*X[:,0]**2
pl.scatter(X[:,0],X[:,1])


Out[107]:
<matplotlib.collections.PathCollection at 0x1166bbbd0>

In [108]:
y = transform(X)

In [109]:
y.shape


Out[109]:
(1000, 2)

In [110]:
pl.scatter(y[:,0],y[:,1])


Out[110]:
<matplotlib.collections.PathCollection at 0x11691e890>

In [111]:
dim=2
evecs[:dim].shape


Out[111]:
(2, 2)

In [112]:
cov = np.cov(X,rowvar=0)
evals,evecs = linalg.eig(cov)
W = evecs[:dim]
y = W.dot(X.T).T

In [124]:
X2 = W[:1].dot(y.T).T

In [125]:
pl.scatter(X2[::-1,0],-X2[::-1,1])


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-125-5c603559aab3> in <module>()
----> 1 pl.scatter(X2[::-1,0],-X2[::-1,1])

IndexError: index 1 is out of bounds for axis 1 with size 1

In [126]:
X2.shape


Out[126]:
(1000, 1)

In [127]:
pl.scatter(range(len(X2)),X2)


Out[127]:
<matplotlib.collections.PathCollection at 0x1173fff90>

In [ ]: