API Documentation

Spectral Embedding

class megaman.embedding.spectral_embedding.SpectralEmbedding(n_components=2, radius=None, geom=None, eigen_solver='auto', random_state=None, eigen_tol=1e-12, drop_first=True, diffusion_maps=False)[source]

Spectral embedding for non-linear dimensionality reduction.

Forms an affinity matrix given by the specified function and applies spectral decomposition to the corresponding graph laplacian. The resulting transformation is given by the value of the eigenvectors for each data point.

Parameters:

n_components : integer

number of coordinates for the manifold.

radius : float (optional)

radius for adjacency and affinity calculations. Will be overridden if either is set in geom

geom : dict or megaman.geometry.Geometry object

specification of geometry parameters: keys are [“adjacency_method”, “adjacency_kwds”, “affinity_method”,

“affinity_kwds”, “laplacian_method”, “laplacian_kwds”]

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. Uses a dense data array, and thus should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

random_state : numpy.RandomState or int, optional

The generator or seed used to determine the starting vector for arpack iterations. Defaults to numpy.random.RandomState

eigen_tol : float, optional, default=0.0

Stopping criterion for eigendecomposition of the Laplacian matrix when using arpack eigen_solver.

drop_first : bool, optional, default=True

Whether to drop the first eigenvector. For spectral embedding, this should be True as the first eigenvector should be constant vector for connected graph, but for spectral clustering, this should be kept as False to retain the first eigenvector.

diffusion_map : boolean, optional. Whether to return the diffusion map

version by re-scaling the embedding by the eigenvalues.

References

[R1]A Tutorial on Spectral Clustering, 2007 Ulrike von Luxburg http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.165.9323
[R2]On Spectral Clustering: Analysis and an algorithm, 2011 Andrew Y. Ng, Michael I. Jordan, Yair Weiss http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.19.8100
[R3]Normalized cuts and image segmentation, 2000 Jianbo Shi, Jitendra Malik http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.160.2324
fit(X, y=None, input_type='data')[source]

Fit the model from data in X.

Parameters:

input_type : string, one of: ‘data’, ‘distance’ or ‘affinity’.

The values of input data X. (default = ‘data’)

X : array-like, shape (n_samples, n_features)

Training vector, where n_samples in the number of samples and n_features is the number of features.

If self.input_type is distance, or affinity:

X : array-like, shape (n_samples, n_samples),

Interpret X as precomputed distance or adjacency graph computed from samples.

Returns:

self : object

Returns the instance itself.

megaman.embedding.spectral_embedding.spectral_embedding(geom, n_components=8, eigen_solver='auto', random_state=None, eigen_tol=0.0, drop_first=True, diffusion_maps=False)[source]

Project the sample on the first eigen vectors of the graph Laplacian.

The adjacency matrix is used to compute a normalized graph Laplacian whose spectrum (especially the eigen vectors associated to the smallest eigen values) has an interpretation in terms of minimal number of cuts necessary to split the graph into comparably sized components.

This embedding can also ‘work’ even if the adjacency variable is not strictly the adjacency matrix of a graph but more generally an affinity or similarity matrix between samples (for instance the heat kernel of a euclidean distance matrix or a k-NN matrix).

However care must taken to always make the affinity matrix symmetric so that the eigen vector decomposition works as expected.

Parameters:

geom : a Geometry object from megaman.embedding.geometry

n_components : integer, optional

The dimension of the projection subspace.

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. For this method, M must be an array or matrix type. This method should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

random_state : int seed, RandomState instance, or None (default)

A pseudo random number generator used for the initialization of the lobpcg eigen vectors decomposition when eigen_solver == ‘amg’. By default, arpack is used.

eigen_tol : float, optional, default=0.0

Stopping criterion for eigendecomposition of the Laplacian matrix when using arpack eigen_solver.

drop_first : bool, optional, default=True

Whether to drop the first eigenvector. For spectral embedding, this should be True as the first eigenvector should be constant vector for connected graph, but for spectral clustering, this should be kept as False to retain the first eigenvector.

diffusion_map : boolean, optional. Whether to return the diffusion map

version by re-scaling the embedding by the eigenvalues.

Returns:

embedding : array, shape=(n_samples, n_components)

The reduced samples.

Notes

Spectral embedding is most useful when the graph has one connected component. If there graph has many components, the first few eigenvectors will simply uncover the connected components of the graph.

References

ISOMAP

class megaman.embedding.isomap.Isomap(n_components=2, radius=None, geom=None, eigen_solver='auto', random_state=None, eigen_tol=1e-12, path_method='auto')[source]

Isomap Embedding

Non-linear dimensionality reduction through Isometric Mapping

Parameters:

n_components : integer

number of coordinates for the manifold.

radius : float (optional)

radius for adjacency and affinity calculations. Will be overridden if either is set in geom

geom : dict or megaman.geometry.Geometry object

specification of geometry parameters: keys are [“adjacency_method”, “adjacency_kwds”, “affinity_method”,

“affinity_kwds”, “laplacian_method”, “laplacian_kwds”]

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. Uses a dense data array, and thus should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

random_state : numpy.RandomState or int, optional

The generator or seed used to determine the starting vector for arpack iterations. Defaults to numpy.random.RandomState

eigen_tol : float, optional. Tolerance for ‘arpack’ solver.

path_method : string, optionl. method for computing graph shortest path.

One of [‘auto’, ‘D’, ‘FW’, ‘BF’, ‘J’]. See scipy.sparse.csgraph.shortest_path for more information.

References

[R4]Tenenbaum, J.B.; De Silva, V.; & Langford, J.C. A global geometric framework for nonlinear dimensionality reduction. Science 290 (5500)

Attributes

embedding_ (array, shape = (n_samples, n_components)) Spectral embedding of the training matrix.
fit(X, y=None, input_type='data')[source]

Fit the model from data in X.

Parameters:

input_type : string, one of: ‘data’, ‘distance’.

The values of input data X. (default = ‘data’)

X : array-like, shape (n_samples, n_features)

Training vector, where n_samples in the number of samples and n_features is the number of features.

If self.input_type is ‘distance’:

X : array-like, shape (n_samples, n_samples),

Interpret X as precomputed distance or adjacency graph computed from samples.

eigen_solver : {None, ‘arpack’, ‘lobpcg’, or ‘amg’}

The eigenvalue decomposition strategy to use. AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

Returns:

self : object

Returns the instance itself.

megaman.embedding.isomap.isomap(geom, n_components=8, eigen_solver='auto', random_state=None, eigen_tol=1e-12, path_method='auto', distance_matrix=None, graph_distance_matrix=None, centered_matrix=None)[source]
Parameters:

geom : a Geometry object from megaman.geometry.geometry

n_components : integer, optional

The dimension of the projection subspace.

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. For this method, M must be an array or matrix type. This method should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

random_state : int seed, RandomState instance, or None (default)

A pseudo random number generator used for the initialization of the lobpcg eigen vectors decomposition when eigen_solver == ‘amg’. By default, arpack is used.

eigen_tol : float, optional, default=0.0

Stopping criterion for eigendecomposition of the Laplacian matrix when using arpack eigen_solver.

path_method : string, method for computing graph shortest path. One of :

‘auto’, ‘D’, ‘FW’, ‘BF’, ‘J’. See scipy.sparse.csgraph.shortest_path for more information.

distance_matrix : sparse Ndarray (n_obs, n_obs), optional. Pairwise distance matrix

sparse zeros considered ‘infinite’.

graph_distance_matrix : Ndarray (n_obs, n_obs), optional. Pairwise graph distance

matrix. Output of graph_shortest_path.

centered_matrix : Ndarray (n_obs, n_obs), optional. Centered version of

graph_distance_matrix

Returns:

embedding : array, shape=(n_samples, n_components)

The reduced samples.

Locally Linear Embedding

class megaman.embedding.locally_linear.LocallyLinearEmbedding(n_components=2, radius=None, geom=None, eigen_solver='auto', random_state=None, tol=1e-06, max_iter=100, reg=1000.0)[source]

Locally Linear Embedding

Parameters:

n_components : integer

number of coordinates for the manifold.

radius : float (optional)

radius for adjacency and affinity calculations. Will be overridden if either is set in geom

geom : dict or megaman.geometry.Geometry object

specification of geometry parameters: keys are [“adjacency_method”, “adjacency_kwds”, “affinity_method”,

“affinity_kwds”, “laplacian_method”, “laplacian_kwds”]

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. Uses a dense data array, and thus should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

random_state : numpy.RandomState or int, optional

The generator or seed used to determine the starting vector for arpack iterations. Defaults to numpy.random.RandomState

tol : float, optional

Tolerance for ‘arpack’ method Not used if eigen_solver==’dense’.

max_iter : integer, optional

maximum number of iterations for the arpack solver.

reg : float, optional

regularization constant, multiplies the trace of the local covariance matrix of the distances.

References

[R5]Roweis, S. & Saul, L. Nonlinear dimensionality reduction by locally linear embedding. Science 290:2323 (2000).
fit(X, y=None, input_type='data')[source]

Fit the model from data in X.

Parameters:

input_type : string, one of: ‘data’, ‘distance’.

The values of input data X. (default = ‘data’)

X : array-like, shape (n_samples, n_features)

Training vector, where n_samples in the number of samples and n_features is the number of features.

If self.input_type is ‘distance’:

X : array-like, shape (n_samples, n_samples),

Interpret X as precomputed distance or adjacency graph computed from samples.

Returns:

self : object

Returns the instance itself.

megaman.embedding.locally_linear.barycenter_graph(distance_matrix, X, reg=0.001)[source]

Computes the barycenter weighted graph for points in X

Parameters:

distance_matrix: sparse Ndarray, (N_obs, N_obs) pairwise distance matrix.

X : Ndarray (N_obs, N_dim) observed data matrix.

reg : float, optional

Amount of regularization when solving the least-squares problem. Only relevant if mode=’barycenter’. If None, use the default.

Returns:

W : sparse matrix in CSR format, shape = [n_samples, n_samples]

W[i, j] is assigned the weight of edge that connects i to j.

megaman.embedding.locally_linear.locally_linear_embedding(geom, n_components, reg=0.001, max_iter=100, eigen_solver='auto', tol=1e-06, random_state=None)[source]

Perform a Locally Linear Embedding analysis on the data.

Parameters:

geom : a Geometry object from megaman.geometry.geometry

n_components : integer

number of coordinates for the manifold.

reg : float

regularization constant, multiplies the trace of the local covariance matrix of the distances.

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. For this method, M must be an array or matrix type. This method should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

tol : float, optional

Tolerance for ‘arpack’ method Not used if eigen_solver==’dense’.

max_iter : integer

maximum number of iterations for the arpack solver.

random_state : numpy.RandomState or int, optional

The generator or seed used to determine the starting vector for arpack iterations. Defaults to numpy.random.

Returns:

Y : array-like, shape [n_samples, n_components]

Embedding vectors.

squared_error : float

Reconstruction error for the embedding vectors. Equivalent to norm(Y - W Y, 'fro')**2, where W are the reconstruction weights.

References

[R6]Roweis, S. & Saul, L. Nonlinear dimensionality reduction by locally linear embedding. Science 290:2323 (2000).

Local Tangent Space Alignment

class megaman.embedding.ltsa.LTSA(n_components=2, radius=None, geom=None, eigen_solver='auto', random_state=None, tol=1e-06, max_iter=100)[source]

Local Tangent Space Alignment

Parameters:

n_components : integer

number of coordinates for the manifold.

radius : float (optional)

radius for adjacency and affinity calculations. Will be overridden if either is set in geom

geom : dict or megaman.geometry.Geometry object

specification of geometry parameters: keys are [“adjacency_method”, “adjacency_kwds”, “affinity_method”,

“affinity_kwds”, “laplacian_method”, “laplacian_kwds”]

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. Uses a dense data array, and thus should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

random_state : numpy.RandomState or int, optional

The generator or seed used to determine the starting vector for arpack iterations. Defaults to numpy.random.RandomState

tol : float, optional

Tolerance for ‘arpack’ method Not used if eigen_solver==’dense’.

max_iter : integer

maximum number of iterations for the arpack solver.

References

[R7]Zhang, Z. & Zha, H. Principal manifolds and nonlinear dimensionality reduction via tangent space alignment. Journal of Shanghai Univ. 8:406 (2004)
fit(X, y=None, input_type='data')[source]

Fit the model from data in X.

Parameters:

input_type : string, one of: ‘data’, ‘distance’.

The values of input data X. (default = ‘data’)

X : array-like, shape (n_samples, n_features)

Training vector, where n_samples in the number of samples and n_features is the number of features.

If self.input_type is ‘distance’, or ‘affinity’:

X : array-like, shape (n_samples, n_samples),

Interpret X as precomputed distance or adjacency graph computed from samples.

Returns:

self : object

Returns the instance itself.

megaman.embedding.ltsa.ltsa(geom, n_components, eigen_solver='auto', tol=1e-06, max_iter=100, random_state=None)[source]

Perform a Local Tangent Space Alignment analysis on the data.

Parameters:

geom : a Geometry object from megaman.geometry.geometry

n_components : integer

number of coordinates for the manifold.

eigen_solver : {‘auto’, ‘dense’, ‘arpack’, ‘lobpcg’, or ‘amg’}

‘auto’ :

algorithm will attempt to choose the best method for input data

‘dense’ :

use standard dense matrix operations for the eigenvalue decomposition. For this method, M must be an array or matrix type. This method should be avoided for large problems.

‘arpack’ :

use arnoldi iteration in shift-invert mode. For this method, M may be a dense matrix, sparse matrix, or general linear operator. Warning: ARPACK can be unstable for some problems. It is best to try several random seeds in order to check results.

‘lobpcg’ :

Locally Optimal Block Preconditioned Conjugate Gradient Method. A preconditioned eigensolver for large symmetric positive definite (SPD) generalized eigenproblems.

‘amg’ :

AMG requires pyamg to be installed. It can be faster on very large, sparse problems, but may also lead to instabilities.

tol : float, optional

Tolerance for ‘arpack’ method Not used if eigen_solver==’dense’.

max_iter : integer

maximum number of iterations for the arpack solver.

random_state : numpy.RandomState or int, optional

The generator or seed used to determine the starting vector for arpack iterations. Defaults to numpy.random.

Returns:

embedding : array-like, shape [n_samples, n_components]

Embedding vectors.

squared_error : float

Reconstruction error for the embedding vectors. Equivalent to norm(Y - W Y, 'fro')**2, where W are the reconstruction weights.

References

  • Zhang, Z. & Zha, H. Principal manifolds and nonlinear dimensionality reduction via tangent space alignment. Journal of Shanghai Univ. 8:406 (2004)