Skip to Content
OchreML
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 MDS
MDS(
    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

ParameterTypeDefaultDescription
n_componentsint2Number of dimensions in which to immerse the dissimilarities (must be >= 1).
metricboolTrueIf True, perform metric MDS; if False, perform non-metric MDS using Kruskal isotonic regression.
n_initint4Number of independent SMACOF restarts; the run with the lowest final stress is kept.
max_iterint300Maximum number of SMACOF iterations per initialization.
epsfloat1e-3Relative 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_stateint or NoneNoneRandom seed used to initialize SMACOF runs reproducibly.
devicestr or NoneNoneTarget compute device ("auto", "cpu", "cuda:0", "cuda:all", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device().

Attributes

AttributeType / ShapeDescription
embedding_ndarray of shape (n_samples, n_components)Final embedded coordinates corresponding to the lowest stress run.
stress_floatFinal 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_intNumber of iterations executed in the best run.
n_features_in_intNumber of features seen during fit.
device_strActive 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).

MethodSignatureDescription
fitfit(X, y=None) -> MDSCompute the embedding coordinates for X and populate embedding_.
fit_transformfit_transform(X, y=None) -> ndarrayFit MDS on X and return embedding_.
toto(device) -> MDSMove execution target to a new hardware device.
savesave(path) -> NoneSave the fitted model and learned embedding_ to a binary .dit file.
loadMDS.load(path, device=None) -> MDSLoad 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_)

On this page