We could make use of np.argpartition
on flattened distances off cdist
result -
dists = np.triu(cdist(x,x, 'seuclidean'),1)
s = dists.shape
idx = np.vstack(np.unravel_index(np.argpartition(dists.ravel(),-m)[-m:],s)).T
idx
would be m
pairs of indexes that are farthest, i.e. each row of idx
would represent indexes of one pair from x
.
Sample run -
# with m = 1
In [144]: idx
Out[144]: array([[2, 3]])
# with m = 2
In [147]: idx
Out[147]:
array([[1, 2],
[2, 3]])
# with m = 3
In [150]: idx
Out[150]:
array([[0, 3],
[1, 2],
[2, 3]])
Sample run on 2D
array -
In [44]: x
Out[44]:
array([[1.25, 1.25],
[1.25, 1.25],
[1.87, 1.87],
[0.62, 0.62],
[0.62, 0.62],
[1.25, 1.25],
[0. , 0. ],
[0.62, 0.62]])
In [45]: m = 2
In [46]: dists
Out[46]:
array([[0. , 0. , 1.58, 1.58, 1.58, 0. , 3.16, 1.58],
[0. , 0. , 1.58, 1.58, 1.58, 0. , 3.16, 1.58],
[0. , 0. , 0. , 3.16, 3.16, 1.58, 4.74, 3.16],
[0. , 0. , 0. , 0. , 0. , 1.58, 1.58, 0. ],
[0. , 0. , 0. , 0. , 0. , 1.58, 1.58, 0. ],
[0. , 0. , 0. , 0. , 0. , 0. , 3.16, 1.58],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 1.58],
[0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]])
In [47]: idx
Out[47]:
array([[0, 6],
[2, 6]])
Note that because of the way argpartition
works, idx
might not have the indices in their sorted order of distances. To force it that way, we could do -
idx[dists[tuple(idx.T)].argsort()]