evomap.mapping._mds#

Stress-Based Multidimensional Scaling.

Attributes#

Classes#

MDS

Functions#

_normalized_stress_function(positions, disparities[, ...])

Compute normalized stress and its gradient.

_normalized_stress_gradient(positions, distances, ...)

Calculate the gradient of the normalized stress function for MDS.

Module Contents#

evomap.mapping._mds.EPSILON = 1e-10#
class evomap.mapping._mds.MDS(n_dims=2, mds_type=None, n_iter=2000, n_iter_check=50, init=None, verbose=0, input_type='distance', max_halves=5, tol=0.001, n_inits=1, step_size=1)[source]#
n_dims#
mds_type#
n_iter#
n_iter_check#
init#
verbose#
input_type#
max_halves#
tol#
n_inits#
step_size#
method_str = 'MDS'#
__str__()[source]#

Create a string representation of the MDS instance. Displays the key attributes and all parameters modified by the user.

Returns:

A summary of the key attributes of this MDS object, including modified parameters.

Return type:

str

fit(X)[source]#

Fit the MDS model to the input data, without returning the transformed positions.

Parameters:

X (np.array of shape (n_samples, n_features) or (n_samples, n_samples)) – The input data. If input_type is ‘vector’, X should be the feature vectors of the samples. If input_type is ‘distance’, X should be a pairwise distance matrix.

Returns:

self – The instance of the MDS class, after fitting the model to the input data.

Return type:

object

fit_transform(X)[source]#

Fit the MDS model to the input data and return transformed positions.

Dependning on ‘input_type’, the input data is either interpreted as a distance matrix or feature vectors. The method uses gradient descent to optimize the lower-dimensional positions such that a Stress function, measuring the discrepancy between the input distances and resulting configuration, is minimized.

Parameters:

X (np.array of shape (n_samples, n_features) or (n_samples, n_samples)) – The input data. If input_type is ‘vector’, X should be the feature vectors of the samples. If input_type is ‘distance’, X should be a pairwise distance matrix.

Returns:

The transformed positions in the lower-dimensional space.

Return type:

np.array of shape (n_samples, n_dims)

Raises:

ValueError – If input_type is neither ‘distance’ nor ‘vector’, a ValueError is raised.

evomap.mapping._mds._normalized_stress_function(positions, disparities, mds_type=None, compute_error=True, compute_grad=True)[source]#

Compute normalized stress and its gradient.

The stress function quantifies the goodness-of-fit between the input disparities (or distances) and the Euclidean distances in the low-dimensional space, with options for different MDS types: absolute, ratio, interval, and ordinal scaling. The input distances are transformed to disparities according to the mds type. Optionally, the function also computes the gradient to be used in optimization.

Parameters:
  • positions (np.array of shape (n_samples, n_dims)) – The estimated positions in the low-dimensional space.

  • disparities (np.array of shape (n_samples, n_samples)) – The input distances or disparities matrix, depending on the MDS type.

  • mds_type (str, optional) – The type of MDS scaling to use: ‘absolute’, ‘ratio’, ‘interval’, or ‘ordinal’. If None, ‘absolute’ scaling is used by default.

  • compute_error (bool, optional) – Whether to compute the normalized stress value, by default True.

  • compute_grad (bool, optional) – Whether to compute the gradient of the stress function, by default True.

Returns:

  • float or None – The computed stress value, or None if compute_error is False.

  • np.array of shape (n_samples, n_dims) or None – The computed gradient of the stress function, or None if compute_grad is False.

Raises:

ValueError – If an invalid mds_type is provided, or if mds_type is not recognized.

evomap.mapping._mds._normalized_stress_gradient(positions, distances, disparities)[source]#

Calculate the gradient of the normalized stress function for MDS.

Parameters:
  • positions (np.array of shape (n_samples, n_dims)) – The estimated positions in the low-dimensional space.

  • distances (np.array of shape (n_samples, n_samples)) – The Euclidean distances among the estimated positions.

  • disparities (np.array of shape (n_samples, n_samples)) – The input disparities (or distance) matrix.

Returns:

The gradient of the stress function with respect to the positions.

Return type:

np.array of shape (n_samples, n_dims)