Last active
February 16, 2021 14:57
-
-
Save CMCDragonkai/d663840fc151fca01e2bee242e792a3d to your computer and use it in GitHub Desktop.
Condensed distance matrix and Pairwise index #python #numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What are d, i and j?