Scipy Cdist Or Pdist On Arrays Of Complex Numbers
Solution 1:
The euclidean norm of a complex number is defined as the modulus of the number, and then you can define the distance between two complex numbers as the modulus of their difference.
The warning is there because pdist
and cdist
are designed for N-dimensional (scalar) spaces, where such notion of distance does not make any sense. (How do you deal with many dimensions, each of them containing a complex number? for scalars is pretty easy, but for complex you have a few options)
Given two collection of points:
A = numpy.random.uniform(size=(5)) + numpy.random.uniform(size=(5))*1j
B = numpy.random.uniform(size=(5)) + numpy.random.uniform(size=(5))*1j
The distance between each point of A
and each point of B
can be calculated as
MA = tile(A[:,newaxis],A.size)
MB = tile(B[:,newaxis],B.size)
dist = abs(MA-MB.T)
and you'll have for example in dist[2][3]
the distance between the third point of the collection A
and fourth point of the collection B
.
This is very efficient, even more so if done in one step as @ali_m suggests in the comments,
dist = np.abs(A[:, None] - B[None, :])
If you just want the pairwise distance matrix of a single collection A
, you can substitute B
with A
in the code above. The matrix dist
will be symmetric and will be zero on the diagonal. So you'd be making about twice the number of operations you'd do in a loop, and you'd occupy about twice the memory required. Likely it would still be faster than a solution with a loop (also because with the loop you'd loop over pairs of numbers)
Post a Comment for "Scipy Cdist Or Pdist On Arrays Of Complex Numbers"