Decomposition & Manifold
MDS
Metric and non-metric Multidimensional Scaling via SMACOF majorization to preserve pairwise dissimilarities.
MDS (Multidimensional Scaling) computes a low-dimensional coordinate embedding that preserves pairwise dissimilarities between samples using the SMACOF (Scaling by MAjorizing a COmplicated Function) algorithm. It supports both metric MDS (metric=True) and non-metric MDS (metric=False, using Kruskal isotonic regression), as well as Euclidean or precomputed dissimilarity matrices. (^▽^)
Backend Support
- Backend primitives:
N3(pairwise Euclidean distance matrix),N5(spectral initialization / helpers). - Supported devices:
"auto","cpu","cuda:0","cuda:all","wgpu","tpu:0". Falls back cleanly to multi-threaded Rayon CPU (f64) when an accelerator is unavailable.
Import & Signature
from ochreml.manifold import MDS
# Or via top-level lazy import:
from ochreml import MDSMDS(
n_components: int = 2,
metric: bool = True,
n_init: int = 4,
max_iter: int = 300,
eps: float = 1e-3,
dissimilarity: str = "euclidean",
random_state: Optional[int] = None,
device: Optional[str] = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
n_components | int | 2 | Number of dimensions in which to immerse the dissimilarities (must be >= 1). |
metric | bool | True | If True, perform metric MDS; if False, perform non-metric MDS using Kruskal isotonic regression. |
n_init | int | 4 | Number of independent SMACOF restarts; the run with the lowest final stress is kept. |
max_iter | int | 300 | Maximum number of SMACOF iterations per initialization. |
eps | float | 1e-3 | Relative tolerance with respect to stress for declaring convergence. |
dissimilarity | {"euclidean", "precomputed"} | "euclidean" | Dissimilarity measure: compute pairwise Euclidean distances from X, or treat X directly as an (n_samples, n_samples) dissimilarity matrix. |
random_state | int or None | None | Random seed used to initialize SMACOF runs reproducibly. |
device | str or None | None | Target compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type / Shape | Description |
|---|---|---|
embedding_ | ndarray of shape (n_samples, n_components) | Final embedded coordinates corresponding to the lowest stress run. |
stress_ | float | Final stress value (sum of squared differences between disparities and embedded distances). |
dissimilarity_matrix_ | ndarray of shape (n_samples, n_samples) | Pairwise dissimilarity matrix used during optimization. |
n_iter_ | int | Number of iterations executed in the best run. |
n_features_in_ | int | Number of features seen during fit. |
device_ | str | Active resolved compute device string. |
Methods
MDS is a non-parametric embedding estimator and therefore provides fit and fit_transform (rather than out-of-sample transform).
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y=None) -> MDS | Compute the embedding coordinates for X and populate embedding_. |
fit_transform | fit_transform(X, y=None) -> ndarray | Fit MDS on X and return embedding_. |
to | to(device) -> MDS | Move execution target to a new hardware device. |
save | save(path) -> None | Save the fitted model and learned embedding_ to a binary .dit file. |
load | MDS.load(path, device=None) -> MDS | Load a saved model from a .dit file (also supported via ochreml.load). |
Example
import numpy as np
import ochreml
from ochreml.manifold import MDS
rng = np.random.default_rng(42)
X = np.vstack([
rng.normal(loc=[0.0, 0.0, 0.0, 0.0], scale=0.4, size=(30, 4)),
rng.normal(loc=[3.0, 2.0, -1.0, 0.5], scale=0.4, size=(30, 4)),
])
mds = MDS(
n_components=2,
metric=True,
n_init=4,
max_iter=300,
eps=1e-3,
dissimilarity="euclidean",
random_state=42,
)
embedding = mds.fit_transform(X)
print("Embedding shape:", embedding.shape)
print("Final stress:", round(mds.stress_, 4))
print("Dissimilarity matrix shape:", mds.dissimilarity_matrix_.shape)
mds.save("mds_model.dit")
loaded = ochreml.load("mds_model.dit", device="cpu")
assert np.allclose(embedding, loaded.embedding_)