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()
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?
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()