Visualization
Explore high-dimensional observations in two or three coordinates.
Scikit-Learn · Class
MDS embeds pairwise dissimilarities in a lower-dimensional space using SMACOF. It supports metric and nonmetric variants and can use Euclidean or precomputed dissimilarities.
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.
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')| Name | Type | Description |
|---|---|---|
n_components | int, default=2 | Number of dimensions in which to immerse the dissimilarities. |
metric_mds | bool, default=True | Whether to perform metric MDS; when false, perform nonmetric MDS. |
n_init | int, default=1 | Number of SMACOF runs with different initializations. |
max_iter | int, default=300 | Maximum iterations for one SMACOF run. |
verbose | int, default=0 | Verbosity level. |
eps | float, default=1e-6 | Relative stress tolerance for convergence. |
n_jobs | int or None, default=None | Number of parallel jobs used for multiple initializations. |
random_state | int, RandomState instance or None, default=None | Controls random number generation. |
dissimilarity | {'euclidean', 'precomputed'} | Dissimilarity measure: Euclidean or precomputed. |
metric_params | dict or None, default=None | Metric configuration passed to distance computation. |
normalized_stress | {'auto', True, False}, default='auto' | Stress normalization behavior. |
metric | str, default='euclidean' | Deprecated compatibility parameter shown by the stable signature. |
The page documents fitted attributes and transformation methods; fit_transform returns the transformed embedding array.
n_components to choose how many coordinates each observation receives in the embedding.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.metric_mds to select quantitative-distance preservation or rank-based nonmetric embedding.n_jobs can control parallel execution of multiple initialization runs.stress_ value according to the selected mode: metric MDS defaults to raw stress, while nonmetric MDS defaults to normalized stress.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.
Explore high-dimensional observations in two or three coordinates.
Represent custom pairwise distances that are not ordinary feature-space Euclidean distances.
Recover a low-dimensional arrangement from similarity or distance data.
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.
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.
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.fitMDS.fit_transformMDS.get_paramsMDS.set_paramsClassicalMDS