Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

MDS

MDS embeds pairwise dissimilarities in a lower-dimensional space using SMACOF. It supports metric and nonmetric variants and can use Euclidean or precomputed dissimilarities.

How it works

MDS uses the SMACOF algorithm to place observations in a lower-dimensional space while preserving their pairwise dissimilarities as well as possible. Metric mode preserves quantitative distances, whereas nonmetric mode uses the rank order of dissimilarities; repeated initializations can improve the selected solution.

Syntax

class sklearn.manifold.MDS(n_components=2, *, metric_mds=True, n_init=1, init='warn', max_iter=300, verbose=0, eps=1e-6, n_jobs=None, random_state=None, dissimilarity='deprecated', metric='euclidean', metric_params=None, normalized_stress='auto')

Parameters

NameTypeDescription
n_componentsint, default=2Number of dimensions in which to immerse the dissimilarities.
metric_mdsbool, default=TrueWhether to perform metric MDS; when false, perform nonmetric MDS.
n_initint, default=1Number of SMACOF runs with different initializations.
max_iterint, default=300Maximum iterations for one SMACOF run.
verboseint, default=0Verbosity level.
epsfloat, default=1e-6Relative stress tolerance for convergence.
n_jobsint or None, default=NoneNumber of parallel jobs used for multiple initializations.
random_stateint, RandomState instance or None, default=NoneControls random number generation.
dissimilarity{'euclidean', 'precomputed'}Dissimilarity measure: Euclidean or precomputed.
metric_paramsdict or None, default=NoneMetric configuration passed to distance computation.
normalized_stress{'auto', True, False}, default='auto'Stress normalization behavior.
metricstr, default='euclidean'Deprecated compatibility parameter shown by the stable signature.

Returns

The page documents fitted attributes and transformation methods; fit_transform returns the transformed embedding array.

Key points

  • Use n_components to choose how many coordinates each observation receives in the embedding.
  • Increase n_init to try multiple starting configurations and reduce dependence on one initialization.
  • max_iter bounds the optimization work performed by each individual SMACOF run.
  • eps determines when an optimization run is considered converged based on relative stress improvement.
  • Set metric_mds to select quantitative-distance preservation or rank-based nonmetric embedding.
  • n_jobs can control parallel execution of multiple initialization runs.

Common mistakes

  • In nonmetric mode, zero dissimilarities are treated as missing values, so zero entries should not be used to represent genuine equal distances.
  • Interpret the stress_ value according to the selected mode: metric MDS defaults to raw stress, while nonmetric MDS defaults to normalized stress.

Example: Embed Iris data with MDS

from sklearn.manifold import MDS
from sklearn.datasets import load_iris

X, _ = load_iris(return_X_y=True)
embedding = MDS(n_components=2, random_state=0)
X_2d = embedding.fit_transform(X)
print(X_2d.shape)

This creates a two-dimensional embedding of the Iris samples using Euclidean metric MDS.

When to use it

Visualization

Explore high-dimensional observations in two or three coordinates.

Precomputed dissimilarities

Represent custom pairwise distances that are not ordinary feature-space Euclidean distances.

Distance-based representation

Recover a low-dimensional arrangement from similarity or distance data.

Interview questions

QWhat problem does MDS solve?

MDS is a manifold-learning estimator for embedding dissimilarities into a chosen number of dimensions. It supports metric and nonmetric modes and optimizes the embedding with SMACOF.

Follow-up: How would you supply distances rather than feature vectors? Set dissimilarity='precomputed' and pass the square dissimilarity matrix to fit or fit_transform.

QWhy might you increase n_init?

n_init controls how many SMACOF runs use different initializations. The estimator can run those initializations in parallel when appropriate, and the resulting solution is selected using stress.

Follow-up: How can reproducibility be improved? Provide a fixed random_state, while remembering that changing initialization can change the optimization result.

QWhat do n_components and dissimilarity control?

n_components specifies the dimensionality of the embedding, while dissimilarity selects either pairwise Euclidean distances computed from the input or distances supplied directly. The default dissimilarity is Euclidean.

Follow-up: When should precomputed distances be used? Use them when the pairwise relationship is not adequately represented by Euclidean distances in the original feature space.

  • MDS.fit
  • MDS.fit_transform
  • MDS.get_params
  • MDS.set_params
  • ClassicalMDS

Source: MDS — scikit-learn 1.9.1 documentation. Checked 2026-10-15.