Skip to content

Instantly share code, notes, and snippets.

@Pseudomanifold
Created July 3, 2017 12:30
Show Gist options
  • Save Pseudomanifold/2b1490f9447c1435d890f9960782a7fe to your computer and use it in GitHub Desktop.
Save Pseudomanifold/2b1490f9447c1435d890f9960782a7fe to your computer and use it in GitHub Desktop.
A simple example script that embeds a matrix of distances into 2D using MDS
#!/usr/bin/env python3
#
# Embeds a distance matrix into 2D using MDS. Distances are normalized
# to [0,1] in order to facilitate comparing embedded matrices.
import numpy
import sys
from sklearn.manifold import MDS
M = numpy.loadtxt(sys.argv[1])
M = ( M - M.min() ) / ( M.max() - M.min() )
mds = MDS(dissimilarity="precomputed", n_init=10, eps=1e-9, max_iter=3000)
points = mds.fit_transform(M)
for point in points:
x,y = point
print(x,y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment