Scipy cdist maximum distance

Question:

New to scipy. I am trying to use the cdist function to pick the greatest distance between vectors. My attempt is

dm = cdist(XA, XB, lambda u, v: np.max(np.sqrt(((u-v)**2).sum())))

but it doesn’t seem to produce the correct result. Any suggestions?

Answers:

The cdist function returns a NxM matrix containing all distances between the N vectors of XA and M vectors of XB. If you want the max distance, regardless of the vectors that originate it, you need to ravel() the 2D array into a 1D array and then look for the max() value:

dm = cdist(XA, XB,metric='euclidean').ravel().max()
Answered By: imburningbabe
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.