Skip to content

Instantly share code, notes, and snippets.

@kevin-keraudren
Created July 25, 2015 19:00
Show Gist options
  • Save kevin-keraudren/4449c45e246888f96368 to your computer and use it in GitHub Desktop.
Save kevin-keraudren/4449c45e246888f96368 to your computer and use it in GitHub Desktop.
import numpy as np
def dr_toolbox(D,n_components=None):
"""
Landmark isomap dimensionality reduction method
This is the version implemented in
http://homepage.tudelft.nl/19j49/Matlab_Toolbox_for_Dimensionality_Reduction.html
Parameters
----------
C : matrix of geodesic distances to the landmarks
Returns
-------
embedded_coords : embedded coordinates of the original points
"""
(n, nl) = map(float, D.shape)
subB = -0.5 * (
D - np.array(
[D.mean(axis=1)]*int(nl)
).transpose() - np.array(
[D.mean(axis=0)]*int(n)
) + np.ones(D.shape)*D.sum() / (n * nl))
subB2 = np.dot(subB.T, subB)
beta,alpha = np.linalg.eig(subB2)
val = map(np.sqrt, np.asarray(beta,dtype=complex))
invVal = np.linalg.inv(np.diag(val))
vec = np.dot( np.dot(subB, alpha), invVal)
# Computing final embedding
val = np.array(map(np.real,val))
ind = val.argsort()
ind = ind[::-1] # we want decreasing order
val = val.take(ind)
vec = vec.take(ind, axis=1)
return vec*[map(sqrt, val)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment