Files
FYS-STK4155/doc/MathFoundationML/PythonCode_MDS.ipynb
T
2024-01-07 17:01:32 +01:00

145 KiB

Multi-Dimensional Scaling

The following experiment extracts pairwise distances from a collection of sample points, then uses MDS to reconstruct them.

This example is taken from https://jakevdp.github.io/PythonDataScienceHandbook/05.10-manifold-learning.html

In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()
import numpy as np



def make_hello(N=1000, rseed=42):
    # Make a plot with "HELLO" text; save as PNG
    fig, ax = plt.subplots(figsize=(4, 1))
    fig.subplots_adjust(left=0, right=1, bottom=0, top=1)
    ax.axis('off')
    ax.text(0.5, 0.4, 'HELLO', va='center', ha='center', weight='bold', size=85)
    fig.savefig('hello.png')
    plt.close(fig)
    
    # Open this PNG and draw random points from it
    from matplotlib.image import imread
    data = imread('hello.png')[::-1, :, 0].T
    rng = np.random.RandomState(rseed)
    X = rng.rand(4 * N, 2)
    i, j = (X * data.shape).astype(int).T
    mask = (data[i, j] < 1)
    X = X[mask]
    X[:, 0] *= (data.shape[0] / data.shape[1])
    X = X[:N]
    return X[np.argsort(X[:, 0])]
In [10]:
X = make_hello(1000)
colorize = dict(c=X[:, 0], cmap=plt.cm.get_cmap('rainbow', 5))
#plt.scatter(X[:, 0], X[:, 1], **colorize)
#plt.axis('equal');


# Rotate the words by 20 degrees
def rotate(X, angle):
    theta = np.deg2rad(angle)
    R = [[np.cos(theta), np.sin(theta)],
         [-np.sin(theta), np.cos(theta)]]
    return np.dot(X, R)
    
X2 = rotate(X, 20)
plt.scatter(X2[:, 0], X2[:, 1], **colorize)
plt.axis('equal');

We extract the pairwise distances between the sample points.

In [12]:
from sklearn.metrics import pairwise_distances
D = pairwise_distances(X)
#D.shape

plt.imshow(D, zorder=2, cmap='Blues', interpolation='nearest')
plt.colorbar();

We apply the MDS algorithm to find features that preserve the pairwise distances.

In [14]:
from sklearn.manifold import MDS


model = MDS(n_components=2, dissimilarity='precomputed', random_state=1)
out = model.fit_transform(D)
plt.scatter(out[:, 0], out[:, 1], **colorize)
plt.axis('equal');
In [ ]: