Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CMCDragonkai/d663840fc151fca01e2bee242e792a3d to your computer and use it in GitHub Desktop.
Save CMCDragonkai/d663840fc151fca01e2bee242e792a3d to your computer and use it in GitHub Desktop.
Condensed distance matrix and Pairwise index #python #numpy
# sometimes you want to get the distance matrix
# and once you have that distance matrix
# you want to be able to know what pairs contributed to that distance
# instead of converting it to the squareform
# which has redundant data
# we can do a little calculation
# note that there's a parallel version of pdist https://stackoverflow.com/a/29639465/582917
# however it doesn't return the condensed matrix
from scipy.spatial.distance import pdist, squareform
arr = np.array([1,2,3])
dist = pdist(arr)
square = squareform(dist)
def pair_ind_to_dist_ind(d, i, j):
index = d*(d-1)/2 - (d-i)*(d-i-1)/2 + j - i - 1
return index
def dist_ind_to_pair_ind(d, i):
b = 1 - 2 * d
x = np.floor((-b - np.sqrt(b**2 - 8*i))/2).astype(int)
y = (i + x * (b + x + 2) / 2 + 1).astype(int)
return (x,y)
@janjagusch
Copy link

What are d, i and j?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment