ClassicalMDS#

class sklearn.manifold.ClassicalMDS(n_components=2, *, metric='euclidean', metric_params=None)[source]#

Classical multidimensional scaling (MDS).

This is also known as principal coordinates analysis (PCoA) or Torgerson’s scaling. It is a version of MDS that has exact solution in terms of eigendecomposition. If the input dissimilarity matrix consists of the pairwise Euclidean distances between some vectors, then classical MDS is equivalent to PCA applied to this set of vectors.

Read more in the User Guide.

Parameters:
n_componentsint, default=2

Number of embedding dimensions.

metricstr or callable, default=’euclidean’

Metric to use for dissimilarity computation. Default is “euclidean”.

If metric is a string, it must be one of the options allowed by scipy.spatial.distance.pdist for its metric parameter, or a metric listed in sklearn.metrics.pairwise.distance_metrics

If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit.

If metric is a callable function, it takes two arrays representing 1D vectors as inputs and must return one value indicating the distance between those vectors. This works for Scipy’s metrics, but is less efficient than passing the metric name as a string.

metric_paramsdict, default=None

Additional keyword arguments for the dissimilarity computation.

Attributes:
embedding_ndarray of shape (n_samples, n_components)

Stores the position of the dataset in the embedding space.

dissimilarity_matrix_ndarray of shape (n_samples, n_samples)

Pairwise dissimilarities between the points.

eigenvalues_ndarray of shape (n_components,)

Eigenvalues of the double-centered dissimilarity matrix, corresponding to each of the selected components. They are equal to the squared 2-norms of the n_components variables in the embedding space.

n_features_in_int

Number of features seen during fit.

feature_names_in_ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when X has feature names that are all strings.

See also

sklearn.decomposition.PCA

Principal component analysis.

MDS

Metric and non-metric MDS.

References

[1]

“Modern Multidimensional Scaling - Theory and Applications” Borg, I.; Groenen P. Springer Series in Statistics (1997)

Examples

>>> from sklearn.datasets import load_digits
>>> from sklearn.manifold import ClassicalMDS
>>> X, _ = load_digits(return_X_y=True)
>>> X.shape
(1797, 64)
>>> cmds = ClassicalMDS(n_components=2)
>>> X_emb = cmds.fit_transform(X[:100])
>>> X_emb.shape
(100, 2)
fit(X, y=None)[source]#

Compute the embedding positions.

Parameters:
Xarray-like of shape (n_samples, n_features) or (n_samples, n_samples)

Input data. If metric=='precomputed', the input should be the dissimilarity matrix.

yIgnored

Not used, present for API consistency by convention.

Returns:
selfobject

Fitted estimator.

fit_transform(X, y=None)[source]#

Compute and return the embedding positions.

Parameters:
Xarray-like of shape (n_samples, n_features) or (n_samples, n_samples)

Input data. If metric=='precomputed', the input should be the dissimilarity matrix.

yIgnored

Not used, present for API consistency by convention.

Returns:
X_newndarray of shape (n_samples, n_components)

The embedding coordinates.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:
routingMetadataRequest

A MetadataRequest encapsulating routing information.

get_params(deep=True)[source]#

Get parameters for this estimator.

Parameters:
deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.

Returns:
paramsdict

Parameter names mapped to their values.

set_params(**params)[source]#

Set the parameters of this estimator.

The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Parameters:
**paramsdict

Estimator parameters.

Returns:
selfestimator instance

Estimator instance.