rsatoolbox.util.vis_utils module¶
Collection of helper methods for vis module
Weighted_MDS: an MDS class that incorporates weighting
weight_to_matrices: batch squareform() to weight matrices
@author: baihan
Notice:
The functions of MDS in this module are modified from the Python package scikit-learn, originally written by Nelle Varoquaux <nelle.varoquaux@gmail.com> under BSD licence <https://en.wikipedia.org/wiki/BSD_licenses>. We modified the MDS function to include an additional functionality of having an important matrix as an input.
- class rsatoolbox.util.vis_utils.Weighted_MDS(n_components=2, *, metric=True, n_init=4, max_iter=300, verbose=0, eps=0.001, n_jobs=None, random_state=None, dissimilarity='euclidean', normalized_stress='auto')[source]¶
Bases:
BaseEstimator
Multidimensional scaling with weighting options.
- Parameters
n_components (int, default=2) – Number of dimensions in which to immerse the dissimilarities.
metric (bool, default=True) – If
True
, perform metric MDS; otherwise, perform nonmetric MDS.n_init (int, default=4) – Number of times the SMACOF algorithm will be run with different initializations. The final results will be the best output of the runs, determined by the run with the smallest final stress.
max_iter (int, default=300) – Maximum number of iterations of the SMACOF algorithm for a single run.
verbose (int, default=0) – Level of verbosity.
eps (float, default=1e-3) – Relative tolerance with respect to stress at which to declare convergence.
n_jobs (int, default=None) –
The number of jobs to use for the computation. If multiple initializations are used (
n_init
), each run of the algorithm is computed in parallel.None
means 1 unless in ajoblib.parallel_backend
context.-1
means using all processors.random_state (int, RandomState instance or None, default=None) – Determines the random number generator used to initialize the centers. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
dissimilarity ({'euclidean', 'precomputed'}, default='euclidean') –
Dissimilarity measure to use:
- ’euclidean’:
Pairwise Euclidean distances between points in the dataset.
- ’precomputed’:
Pre-computed dissimilarities are passed directly to
fit
andfit_transform
.
- Variables
embedding (ndarray of shape (n_samples, n_components)) – Stores the position of the dataset in the embedding space.
stress (float) – The final value of the stress (sum of squared distance of the disparities and the distances for all constrained points).
dissimilarity_matrix (ndarray of shape (n_samples, n_samples)) –
Pairwise dissimilarities between the points. Symmetric matrix that:
either uses a custom dissimilarity matrix by setting dissimilarity to ‘precomputed’;
or constructs a dissimilarity matrix from data using Euclidean distances.
n_iter (int) – The number of iterations corresponding to the best stress.
Examples
>>> from sklearn.datasets import load_digits >>> from sklearn.manifold import MDS >>> X, _ = load_digits(return_X_y=True) >>> X.shape (1797, 64) >>> embedding = MDS(n_components=2) >>> X_transformed = embedding.fit_transform(X[:100]) >>> X_transformed.shape (100, 2)
References
“Modern Multidimensional Scaling - Theory and Applications” Borg, I.; Groenen P. Springer Series in Statistics (1997)
“Nonmetric multidimensional scaling: a numerical method” Kruskal, J. Psychometrika, 29 (1964)
“Multidimensional scaling by optimizing goodness of fit to a nonmetric hypothesis” Kruskal, J. Psychometrika, 29, (1964)
- fit(X, y=None, init=None, weight=None)[source]¶
Computes the position of the points in the embedding space.
- Parameters
X (array-like of shape (n_samples, n_features) or (n_samples, n_samples)) – Input data. If
dissimilarity=='precomputed'
, the input should be the dissimilarity matrix.y (Ignored) –
init (ndarray of shape (n_samples,), default=None) – Starting configuration of the embedding to initialize the SMACOF algorithm. By default, the algorithm is initialized with a randomly chosen array.
weight (ndarray of shape (n_samples, n_samples), default=None) – symmetric weighting matrix of similarities. In default, all weights are 1.
- fit_transform(X, y=None, init=None, weight=None)[source]¶
Fit the data from X, and returns the embedded coordinates.
- Parameters
X (array-like of shape (n_samples, n_features) or (n_samples, n_samples)) – Input data. If
dissimilarity=='precomputed'
, the input should be the dissimilarity matrix.y (Ignored) –
init (ndarray of shape (n_samples,), default=None) – Starting configuration of the embedding to initialize the SMACOF algorithm. By default, the algorithm is initialized with a randomly chosen array.
weight (ndarray of shape (n_samples, n_samples), default=None) – symmetric weighting matrix of similarities. In default, all weights are 1.
- rsatoolbox.util.vis_utils.smacof(dissimilarities, *, metric=True, n_components=2, init=None, n_init=8, n_jobs=None, max_iter=300, verbose=0, eps=0.001, random_state=None, return_n_iter=False, weight=None)[source]¶
Computes multidimensional scaling using the SMACOF algorithm.
The SMACOF (Scaling by MAjorizing a COmplicated Function) algorithm is a multidimensional scaling algorithm which minimizes an objective function (the stress) using a majorization technique. Stress majorization, also known as the Guttman Transform, guarantees a monotone convergence of stress, and is more powerful than traditional techniques such as gradient descent.
The SMACOF algorithm for metric MDS can summarized by the following steps:
Set an initial start configuration, randomly or not.
Compute the stress
Compute the Guttman Transform
Iterate 2 and 3 until convergence.
The nonmetric algorithm adds a monotonic regression step before computing the stress.
- Parameters
dissimilarities (ndarray of shape (n_samples, n_samples)) – Pairwise dissimilarities between the points. Must be symmetric.
metric (bool, default=True) – Compute metric or nonmetric SMACOF algorithm.
n_components (int, default=2) – Number of dimensions in which to immerse the dissimilarities. If an
init
array is provided, this option is overridden and the shape ofinit
is used to determine the dimensionality of the embedding space.init (ndarray of shape (n_samples, n_components), default=None) – Starting configuration of the embedding to initialize the algorithm. By default, the algorithm is initialized with a randomly chosen array.
n_init (int, default=8) – Number of times the SMACOF algorithm will be run with different initializations. The final results will be the best output of the runs, determined by the run with the smallest final stress. If
init
is provided, this option is overridden and a single run is performed.n_jobs (int, default=None) –
The number of jobs to use for the computation. If multiple initializations are used (
n_init
), each run of the algorithm is computed in parallel.None
means 1 unless in ajoblib.parallel_backend
context.-1
means using all processors.max_iter (int, default=300) – Maximum number of iterations of the SMACOF algorithm for a single run.
verbose (int, default=0) – Level of verbosity.
eps (float, default=1e-3) – Relative tolerance with respect to stress at which to declare convergence.
random_state (int, RandomState instance or None, default=None) – Determines the random number generator used to initialize the centers. Pass an int for reproducible results across multiple function calls. See :term: Glossary <random_state>.
return_n_iter (bool, default=False) – Whether or not to return the number of iterations.
weight (ndarray of shape (n_samples, n_samples), default=None) – symmetric weighting matrix of similarities. In default, all weights are 1.
- Returns
X (ndarray of shape (n_samples, n_components)) – Coordinates of the points in a
n_components
-space.stress (float) – The final value of the stress (sum of squared distance of the disparities and the distances for all constrained points).
n_iter (int) – The number of iterations corresponding to the best stress. Returned only if
return_n_iter
is set toTrue
.
Notes
“Modern Multidimensional Scaling - Theory and Applications” Borg, I.; Groenen P. Springer Series in Statistics (1997)
“Nonmetric multidimensional scaling: a numerical method” Kruskal, J. Psychometrika, 29 (1964)
“Multidimensional scaling by optimizing goodness of fit to a nonmetric hypothesis” Kruskal, J. Psychometrika, 29, (1964)