Skip to content

Instantly share code, notes, and snippets.

@tpeng
Created March 25, 2013 08:29
Show Gist options
  • Save tpeng/5235674 to your computer and use it in GitHub Desktop.
Save tpeng/5235674 to your computer and use it in GitHub Desktop.
MDS example
# MDS example from http://www.analytictech.com/networks/mds.htm
import numpy as np
from sklearn import manifold
import matplotlib.pyplot as plt
cities = ["BOST", "NY", "DC", "MIAM", "CHIC", "SEAT", "SF", "LA", "DENV"]
dis = np.array([[0, 206, 429, 1504, 963, 2976, 3095, 2979, 1949],
[206, 0, 233, 1308, 802, 2815, 2934, 2786, 1771],
[429, 233, 0, 1075, 671, 2684, 2799, 2631, 1616],
[1504, 1308, 1075, 0, 1329, 3273, 3053, 2687, 2037],
[963, 802, 671, 1329, 0, 2013, 2142, 2054, 996],
[2976, 2815, 2684, 3273, 2013, 0, 808, 1131, 1307],
[3095, 2934, 2799, 3053, 2142, 808, 0, 379, 1235],
[2979, 2786, 2631, 2687, 2054, 1131, 379, 0, 1059],
[1949, 1771, 1616, 2037, 996, 1307, 1235, 1059, 0]])
mds = manifold.MDS(dissimilarity='precomputed')
Y = mds.fit_transform(dis)
xs1 = np.array(Y[:, 0])
xs2 = np.array(Y[:, 1])
plt.plot(xs1, xs2, '.')
for i in range(9):
plt.text(xs1[i], xs2[i], cities[i])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment