Skip to main content

mdscuda

This is a CUDA implementation of Multidimensional Scaling (https://en.wikipedia.org/wiki/Multidimensional_scaling) using the SMACOF algorithm. Currently only metric MDS is supported.

Installation

pip install mdscuda

Latest version: 0.1.3

Example

import numpy as np
from mdscuda import MDS, mds_fit, minkowski_pairs

N_SAMPLES = 1000
N_FEATURES = 100
X = np.random.normal(size = (N_SAMPLES, N_FEATURES))
DELTA = minkowski_pairs(X, sqform = False)  # this returns a matrix of pairwise distances in longform

# method 1: use an sklearn-style class

mds = MDS(n_dims = 3, verbosity = 2)  # defines sklearn-style class
x = mds.fit(DELTA, calc_r2=True)  # fits and returns embedding
print("mds r2: {}".format(mds.r2))  # prints R-squared value to assess quality of fit

# method 2: you can fit directly without using a class

x = mds_fit(DELTA, n_dims = 3, verbosity = 1)

Documentation

class mdscuda.MDS methods:

mdscuda.MDS.init(self, n_dims = 2, max_iter = 300, n_init = 4, x_init = None, verbosity = 0)

  • n_dims: int; number of dimensions in embedding space
  • max_iter: int; maximum iterations of SMACOF algorithm to perform
  • n_init: int; number of times to initialize SMACOF algorithm with random uniform(0, 100) initialization
  • x_init: array or None; initial embedding. If not None, n_init is set to 1 and n_dims is set to x_init.shape[1]
  • verbosity: int; if >= 1, print num iterations and final sigma values. if >= 2, print sigma value each iteration (note: verbosity >= 2 slows performance by a factor of approximately 2)

mdscuda.MDS.fit(self, delta, sqform = False, calc_r2 = False)

  • delta: array; matrix of pairwise distances, longform by default, squareform if sqform == True
  • sqform: bool; if True, delta is interpreted in squareformn, if False, delta is interpreted in longform
  • calc_r2: bool; if True, calculates R-squared value and records it as the attribute MDS.r2

class mdscuda.MDS attributes:

  • MDS.x: array or None; embedding
  • MDS.r2: float or None; R-squared value

mdscuda.mds_fit(delta, n_dims = 2, max_iter = 300, n_init = 4, x_init = None, verbosity = 0, sqform = False)

  • delta: array; matrix of pairwise distances, longform by default, squareform if sqform == True
  • n_dims: int; number of dimensions in embedding space
  • max_iter: int; maximum iterations of SMACOF algorithm to perform
  • n_init: int; number of times to initialize SMACOF algorithm with random uniform(0, 100) initialization
  • x_init: array or None; initial embedding. If not None, n_init is set to 1 and n_dims is set to x_init.shape[1]
  • verbosity: int; if >= 1, print num iterations and final sigma values. if >= 2, print sigma value each iteration (note: verbosity >= 2 slows performance by a factor of approximately 2)
  • sqform: bool; if True, delta is interpreted in squareformn, if False, delta is interpreted in longform

mdscuda.minkowski_pairs(X, p = 2, w = None, sqform = True)

  • X: array of shape (n_samples, n_features); matrix of samples
  • p: float; p for minkowski distance
  • w: array of shape (n_features, ) or None; weights for minkowski distance
  • sqform: bool; if True, squareform matrix of pairwise distances is returned, otherwise, longform is returned

Benchmarks

Run on AMD Ryzen 5 2600 CPU and Nvidia RTX 2080 Ti GPU.
All times are in seconds.

Test1

Dataset: np.random.normal
Dataset shape: (10000, 1000)
Paramters: n_components = 3, max_iter = 50, n_init = 1

Results:
mdscuda final sigma: 11014284288.0
sklearn final sigma: 11022683577.672052

mdscuda time: 1.5030748590070289
sklearn time: 125.06202016805764

Test2

Dataset: Iris
Dataset shape: (150, 4)
Parameters: n_components = 2, max_iter = 100, n_init = 100

Results:
mdscuda final sigma: 120.9668197631836
sklearn final sigma: 112.45789790236945

mdscuda time: 3.961419030005345
sklearn time: 3.922074425005121

Test3

Dataset: Digits
Dataset shape: (1797, 64)
Parameters: n_components = 3, max_iter = 600, n_init = 3

Results:
mdscuda final sigma: 199908928.0
sklearn final sigma: 199902115.6507256

mdscuda time:  2.1541129870165605
sklearn time:  90.4121356800024

Release files for mdscuda 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mdscuda 0.1.3
File Size Uploaded
mdscuda-0.1.3.tar.gz 6.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mdscuda 0.1.3
File Interpreter ABI Platform
mdscuda-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / mdscuda-0.1.3.tar.gz

Download URL mdscuda-0.1.3.tar.gz
Size 6.3 kB
Tags Source
SHA-256 checksum
How to use checksums
23cd3c7be8b82fa11de091f3cf7cd30c6b551c2f587be11212eb4e357cfb67fd
BLAKE2b-256 checksum
How to use checksums
3676b90dbd79aec43598059e59039597d80824ce933c58ef6fa8f702bf6bf6f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.3

Release files / mdscuda-0.1.3-py3-none-any.whl

Download URL mdscuda-0.1.3-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
26aba222d66fcb2952b68838f9b57e69e3f79554c4f26f1d90b04909149076cb
BLAKE2b-256 checksum
How to use checksums
636a7b039b86420c7fe87f983aeae00912699a5ad6f7b1636b36167e8cebd74a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.3

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page