Implementation:Scikit learn Scikit learn MDS
| Knowledge Sources | |
|---|---|
| Domains | Machine Learning, Dimensionality Reduction |
| Last Updated | 2026-02-08 15:00 GMT |
Overview
Concrete tool for multidimensional scaling (MDS) dimensionality reduction provided by scikit-learn.
Description
This module implements Multidimensional Scaling (MDS), which finds a low-dimensional representation of data in which distances between points approximate the given dissimilarity matrix. It supports both metric MDS (preserves actual distances) and non-metric MDS (preserves only the rank order of distances). The module uses the SMACOF (Scaling by MAjorizing a COmplicated Function) algorithm for iterative optimization and supports parallel execution of multiple random initializations.
Usage
Use MDS when you have a distance or dissimilarity matrix and want to visualize or embed the data in a low-dimensional space. Non-metric MDS is useful when only the ordinal relationships between distances matter.
Code Reference
Source Location
- Repository: scikit-learn
- File: sklearn/manifold/_mds.py
Signature
class MDS(BaseEstimator):
"""Multidimensional scaling."""
def __init__(
self,
n_components=2,
*,
metric=True,
n_init=4,
max_iter=300,
verbose=0,
eps=1e-6,
n_jobs=None,
random_state=None,
dissimilarity="euclidean",
normalized_stress="auto",
):
...
def _smacof_single(
dissimilarities,
metric=True,
n_components=2,
init=None,
max_iter=300,
verbose=0,
eps=1e-6,
random_state=None,
normalized_stress=False,
):
...
Import
from sklearn.manifold import MDS
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| X | array-like of shape (n_samples, n_features) or (n_samples, n_samples) | Yes | Data matrix or precomputed dissimilarity matrix |
| n_components | int | No | Target dimensionality (default: 2) |
| metric | bool | No | Use metric MDS if True, non-metric if False (default: True) |
| n_init | int | No | Number of random initializations (default: 4) |
| max_iter | int | No | Maximum SMACOF iterations (default: 300) |
| dissimilarity | str | No | Input type: 'euclidean' or 'precomputed' (default: 'euclidean') |
| normalized_stress | str or bool | No | Normalize stress measure (default: 'auto') |
Outputs
| Name | Type | Description |
|---|---|---|
| embedding_ | ndarray of shape (n_samples, n_components) | Low-dimensional embedding coordinates |
| stress_ | float | Final stress value (goodness of fit) |
| dissimilarity_matrix_ | ndarray of shape (n_samples, n_samples) | Pairwise dissimilarity matrix used |
| n_iter_ | int | Number of iterations for best result |
Usage Examples
Basic Usage
from sklearn.manifold import MDS
from sklearn.metrics import euclidean_distances
import numpy as np
# Generate sample data
rng = np.random.RandomState(42)
X = rng.randn(100, 10)
# Apply metric MDS
mds = MDS(n_components=2, random_state=42)
X_embedded = mds.fit_transform(X)
print("Embedded shape:", X_embedded.shape)
print("Stress:", mds.stress_)