"""
=========================
Multi-dimensional scaling
=========================

An illustration of the metric and non-metric MDS on generated noisy data.

The reconstructed points using the metric MDS and non metric MDS are slightly
shifted to avoid overlapping.
"""

# Author: Nelle Varoquaux <nelle.varoquaux@gmail.com>
# Licence: BSD

print __doc__
import numpy as np

from matplotlib import pyplot as plt
from matplotlib.collections import LineCollection

from sklearn import manifold
from sklearn.metrics import euclidean_distances
from sklearn.decomposition import PCA

n_samples = 20
seed = np.random.RandomState(seed=3)
X_true = seed.randint(0, 20, 2 * n_samples).astype(np.float)
X_true = X_true.reshape((n_samples, 2))
# Center the data
X_true -= X_true.mean()

similarities = euclidean_distances(X_true)

# Add noise to the similarities
noise = np.random.rand(n_samples, n_samples)
noise = noise + noise.T
noise[np.arange(noise.shape[0]), np.arange(noise.shape[0])] = 0
similarities += noise

mds = manifold.MDS(n_components=2, max_iter=3000,
                   eps=1e-9, random_state=seed,
                   n_jobs=1)
pos = mds.fit(similarities).embedding_

nmds = manifold.MDS(n_components=2, metric=False,
                    max_iter=3000,
                    eps=1e-9, random_state=seed, n_jobs=1)
npos = mds.fit_transform(similarities)

# Rotate the data
clf = PCA(n_components=2)
X_true = clf.fit_transform(X_true)

pos = clf.fit_transform(pos)

npos = clf.fit_transform(pos)

fig = plt.figure(1)
ax = plt.axes([0., 0., 1., 1.])

plt.scatter(X_true[:, 0], X_true[:, 1], c='r', s=20)
plt.scatter(pos[:, 0] + 0.2, pos[:, 1] + 0.2, s=20, c='g')
plt.scatter(npos[:, 0] - 0.2, npos[:, 1] - 0.2, s=20, c='b')
plt.legend(('True position', 'MDS', 'NMDS'), loc='best')

similarities = similarities.max() / similarities * 100
similarities[np.isinf(similarities)] = 0

# Plot the edges
start_idx, end_idx = np.where(pos)
#a sequence of (*line0*, *line1*, *line2*), where::
#            linen = (x0, y0), (x1, y1), ... (xm, ym)
segments = [[pos[i, :], pos[j, :]]
            for i in range(len(pos)) for j in range(len(pos))]
values = np.abs(similarities)
lc = LineCollection(segments,
                    zorder=0, cmap=plt.cm.hot_r,
                    norm=plt.Normalize(0, values.max()))
lc.set_array(similarities.flatten())
lc.set_linewidths(0.5 * np.ones(len(segments)))
ax.add_collection(lc)

plt.show()

@@ -1271,33 +1271,34 @@
          eps=1e-
-9
+12
 , random_state=s
@@ -1315,24 +1315,25 @@
 s=1)%0Anpos = 
+n
 mds.fit_tran
@@ -1350,16 +1350,176 @@
 larities
+, init=pos)%0A%0A# Rescale the data%0Apos *= np.sqrt((X_true ** 2).sum()) / np.sqrt((pos ** 2).sum())%0Anpos *= np.sqrt((X_true ** 2).sum()) / np.sqrt((npos ** 2).sum()
 )%0A%0A# Rot
@@ -1644,24 +1644,25 @@
 t_transform(
+n
 pos)%0A%0Afig = 
@@ -1782,22 +1782,16 @@
 os%5B:, 0%5D
- + 0.2
 , pos%5B:,
@@ -1797,14 +1797,8 @@
 , 1%5D
- + 0.2
 , s=
@@ -1830,22 +1830,16 @@
 os%5B:, 0%5D
- - 0.2
 , npos%5B:
@@ -1846,14 +1846,8 @@
 , 1%5D
- - 0.2
 , s=
@@ -2187,19 +2187,22 @@
 = %5B%5B
-pos
+X_true
 %5Bi, :%5D, 
 pos%5B
@@ -2197,19 +2197,22 @@
 %5Bi, :%5D, 
-pos
+X_true
 %5Bj, :%5D%5D%0A

FIX the mds example did not plot the NMDS