mojolearn.MDS#

class mojolearn.MDS(n_components=2, *, metric_mds=True, n_init=1, init='random', max_iter=300, verbose=0, eps=1e-06, n_jobs=None, random_state=None, metric='euclidean', metric_params=None, normalized_stress='auto', numeric_mode=None)#

Multidimensional scaling by SMACOF.

Follows scikit-learn’s MDS: minimizes stress with the Guttman transform from n_init starts and keeps the start with the lowest stress. metric_mds=False runs Kruskal’s non-metric SMACOF. Random starts come from mojolearn’s own generator.

Parameters:
  • n_components (int, default 2) – Embedding dimension.

  • metric_mds (bool, default True) – Metric (True) or non-metric (False) MDS.

  • n_init (int, default 1) – Number of random starts (with init="random").

  • init ({“random”, “classical_mds”} or array-like, default “random”) – Starting configuration: random, the ClassicalMDS embedding, or an array of shape (n_samples, n_components).

  • max_iter (int, default 300) – Maximum SMACOF iterations per start.

  • verbose (ignored)

  • eps (float, default 1e-6) – Convergence tolerance on the stress.

  • n_jobs (ignored)

  • random_state (int or None, default None) – Seed for random starts; None means 0.

  • metric (str, default “euclidean”) – "precomputed" (X is a dissimilarity matrix) or a distance metric: "euclidean", "l2", "minkowski" (p from metric_params), "manhattan" ("cityblock", "l1"), "chebyshev" ("infinity") or "cosine"; a callable or any other name raises NotImplementedError.

  • metric_params (dict or None, default None) – Only {"p": ...} is supported.

  • normalized_stress (bool or “auto”, default “auto”) – Report normalized stress (Kruskal’s stress-1). "auto" is True for non-metric MDS and False otherwise.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

Variables:
  • embedding_ (ndarray of shape (n_samples, n_components))

  • stress_ (float)

  • n_iter_ (int)

  • dissimilarity_matrix_ (ndarray of shape (n_samples, n_samples))

  • n_features_in_ (int)

Raises:

ValueError – For an unknown init, or with metric_mds=False when n_samples**2 exceeds 2**31 - 1.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((300, 8), dtype=np.float32)
>>> Y = mojolearn.MDS(n_components=2, random_state=0).fit_transform(X)

Methods

fit(X[, y, init])

Compute the embedding.

fit_transform(X[, y, init])

Compute the embedding and return it.

get_params([deep])

Return this estimator's constructor parameters.

set_params(**params)

Set constructor parameters by keyword.

fit_transform(X, y=None, init=None)#

Compute the embedding and return it.

Parameters:
  • X (array-like of shape (n_samples, n_features) or (n_samples, n_samples)) – Data, or dissimilarities with metric="precomputed".

  • y (ignored)

  • init (array-like of shape (n_samples, n_components) or None, default None) – Starting configuration; overrides the init parameter.

Returns:

ndarray of shape (n_samples, n_components), float32

fit(X, y=None, init=None)#

Compute the embedding.

Parameters:
  • X (array-like of shape (n_samples, n_features) or (n_samples, n_samples))

  • y (ignored)

  • init (array-like of shape (n_samples, n_components) or None, default None) – Starting configuration; overrides the init parameter.

Returns:

self

get_params(deep=True)#

Return this estimator’s constructor parameters.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility; it has no effect.

Returns:

dict – Parameter name to current value.

set_params(**params)#

Set constructor parameters by keyword.

Raises:

ValueError – If a keyword is not a parameter of this estimator.

Returns:

self