Distance and graph embeddings#

Distance methods preserve pairwise dissimilarities; kernel methods diagonalize a centered similarity; graph methods first define local adjacency and then solve a geodesic, reconstruction, Laplacian, diffusion, or alignment problem [Belkin and Niyogi, 2003, Coifman and Lafon, 2006, Faloutsos and Lin, 1995, Roweis and Saul, 2000, Schölkopf et al., 1998, Tenenbaum et al., 2000, Torgerson, 1952, Zhang and Zha, 2004].

The following deterministic helix keeps the exact-neighbor graphs connected.

import numpy as np

from believe14.nonlinear import (
    ClassicalMDS,
    DiffusionMap,
    FastMap,
    Isomap,
    KernelPCA,
    LaplacianEigenmaps,
    LocallyLinearEmbedding,
    LocalTangentSpaceAlignment,
)

parameter = np.linspace(0.0, 3.0 * np.pi, 48)
X = np.column_stack(
    (np.cos(parameter), np.sin(parameter), parameter / (3.0 * np.pi))
)

models = [
    ClassicalMDS(2),
    FastMap(2),
    KernelPCA(2, gamma=2.0),
    Isomap(2, n_neighbors=6),
    LocallyLinearEmbedding(2, n_neighbors=7),
    LaplacianEigenmaps(2, n_neighbors=6, gamma=2.0),
    DiffusionMap(2, gamma=2.0),
    LocalTangentSpaceAlignment(2, n_neighbors=7),
]

for model in models:
    embedding = model.fit_transform(X)
    assert embedding.shape == (48, 2)
    assert np.isfinite(embedding).all()
    print(
        f"{type(model).__name__:30s}",
        f"out_of_sample={hasattr(model, 'transform')}",
    )
ClassicalMDS                   out_of_sample=False
FastMap                        out_of_sample=True
KernelPCA                      out_of_sample=True
Isomap                         out_of_sample=False
LocallyLinearEmbedding         out_of_sample=False
LaplacianEigenmaps             out_of_sample=False
DiffusionMap                   out_of_sample=True
LocalTangentSpaceAlignment     out_of_sample=False

The missing transform attributes above are intentional. Classical MDS, Isomap, LLE, Laplacian Eigenmaps, and LTSA are transductive. FastMap uses its pivots, Kernel PCA uses Nyström kernel centering, and Diffusion Map uses its cited Nyström transition rule. Supplying precomputed distances or a kernel does not create a new-data rule by itself.

Read the cards for Classical MDS, FastMap, Kernel PCA, Isomap, LLE, Laplacian Eigenmaps, Diffusion Map, and LTSA. Metric and nonmetric stress objectives are treated in the next guide.

Fitting a coordinate system on landmarks#

Landmark MDS scales distances among a subset of training points. Landmark Isomap uses shortest paths to those landmarks on the fitted neighbor graph. Both can embed a new point in the fitted coordinates. Representative landmarks matter because their geometry determines the retained directions [Bengio et al., 2003, de Silva and Tenenbaum, 2002, de Silva and Tenenbaum, 2004].

from believe14.neighbors import build_neighbor_graph
from believe14.nonlinear import LandmarkIsomap, LandmarkMDS

landmarks = np.linspace(0, len(X) - 1, 12, dtype=int)
graph = build_neighbor_graph(X, n_neighbors=6)
direct = LandmarkMDS(2, landmark_indices=landmarks).fit(X)
geodesic = LandmarkIsomap(
    2, n_neighbors=6, landmark_indices=landmarks
).fit(X, neighbors=graph)

query = (X[13:14] + X[14:15]) / 2
print(direct.transform(query), geodesic.transform(query))
assert geodesic.landmark_distances_.shape == (len(X), len(landmarks))
[[ 0.83250106 -0.3719785 ]] [[ 1.24798457 -0.45824069]]

Specify landmark_indices to use a designed sample. Alternatively, n_landmarks selects that many evenly spaced row indices. With neither supplied, the default count is the ceiling of the square root of the training size, raised to at least one more than the requested dimension. This deterministic selection depends on row order. For Landmark Isomap each query attaches to training points independently, so embedding a batch preserves the coordinates obtained for each query alone.