Skip to content

Instantly share code, notes, and snippets.

@nlgranger
Created July 27, 2018 15:49
Show Gist options
  • Save nlgranger/8989c74012b0f32fcda21c7d42307d75 to your computer and use it in GitHub Desktop.
Save nlgranger/8989c74012b0f32fcda21c7d42307d75 to your computer and use it in GitHub Desktop.
sort pairwise distance matrix
# like https://gmarti.gitlab.io/ml/2017/09/07/how-to-sort-distance-matrix.html
# but using just 4 lines of code thanks to 'optimal_ordering' argument
# added to scipy.cluster.hierarchy.linkage
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import linkage
# distance matrix
m = np.zeros((500, 500))
m[:100, :100] = 1
m[100:150, 100:150] = 1
m[180:350, 180:350] = 1
m[400:490, 400:490] = 1
m[490:, 490:] = 1
m = 1 - m
m[np.arange(500), np.arange(500)] = 0
plt.figure()
plt.imshow(m)
# shuffle
perm = np.random.permutation(len(m))
m = m[perm][:, perm]
# reorder
y = m[np.triu_indices(len(m), k=1)]
Z = linkage(y, method='single', optimal_ordering=True)
perm = np.ravel(Z[:, :2]).astype(np.int32)
perm = perm[perm < len(m)]
m = m[perm][:, perm]
plt.figure()
plt.imshow(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment