Pairwise operations (distance) on two lists in numpy

2024/9/30 23:26:09

I have two lists of coordinates:

l1 = [[x,y,z],[x,y,z],[x,y,z],[x,y,z],[x,y,z]]
l2 = [[x,y,z],[x,y,z],[x,y,z]]

I want to find the shortest pairwise distance between l1 and l2. Distance between two coordinates is simply:

numpy.linalg.norm(l1_element - l2_element)

So how do I use numpy to efficiently apply this operation to each pair of elements?

Answer

Here is a quick performance analysis of the four methods presented so far:

import numpy
import scipy
from itertools import product
from scipy.spatial.distance import cdist
from scipy.spatial import cKDTree as KDTreen = 100
l1 = numpy.random.randint(0, 100, size=(n,3))
l2 = numpy.random.randint(0, 100, size=(n,3))# by @Phillip
def a(l1,l2):return min(numpy.linalg.norm(l1_element - l2_element) for l1_element,l2_element in product(l1,l2))# by @Kasra
def b(l1,l2):return numpy.min(numpy.apply_along_axis(numpy.linalg.norm,2,l1[:, None, :] - l2[None, :, :]))# mine
def c(l1,l2):return numpy.min(scipy.spatial.distance.cdist(l1,l2))# just checking that numpy.min is indeed faster.
def c2(l1,l2):return min(scipy.spatial.distance.cdist(l1,l2).reshape(-1))# by @BrianLarsen
def d(l1,l2):# make KDTrees for both sets of pointst1 = KDTree(l1)t2 = KDTree(l2)# we need a distance to not look beyond, if you have real knowledge use it, otherwise guessmaxD = numpy.linalg.norm(l1[0] - l2[0]) # this could be closest but anyhting further is certainly not# get a sparce matrix of all the distancesans = t1.sparse_distance_matrix(t2, maxD)# get the minimum distance and points involvedminD = min(ans.values())return minDfor x in (a,b,c,c2,d):print("Timing variant", x.__name__, ':', flush=True)print(x(l1,l2), flush=True)%timeit x(l1,l2)print(flush=True)

For n=100

Timing variant a :
2.2360679775
10 loops, best of 3: 90.3 ms per loopTiming variant b :
2.2360679775
10 loops, best of 3: 151 ms per loopTiming variant c :
2.2360679775
10000 loops, best of 3: 136 µs per loopTiming variant c2 :
2.2360679775
1000 loops, best of 3: 844 µs per loopTiming variant d :
2.2360679775
100 loops, best of 3: 3.62 ms per loop

For n=1000

Timing variant a :
0.0
1 loops, best of 3: 9.16 s per loopTiming variant b :
0.0
1 loops, best of 3: 14.9 s per loopTiming variant c :
0.0
100 loops, best of 3: 11 ms per loopTiming variant c2 :
0.0
10 loops, best of 3: 80.3 ms per loopTiming variant d :
0.0
1 loops, best of 3: 933 ms per loop
https://en.xdnf.cn/q/71028.html

Related Q&A

Bad results when undistorting points using OpenCV in Python

Im having trouble undistorting points on an image taken with a calibrated camera using the Python bindings for OpenCV. The undistorted points have entirely different coordinates than the original point…

Pandas - Groupby and create new DataFrame?

This is my situation - In[1]: data Out[1]: Item Type 0 Orange Edible, Fruit 1 Banana Edible, Fruit 2 Tomato Edible, Vegetable 3 Laptop Non Edible, Elec…

Can pytest fixtures and confest.py modules be shared across packages?

Suppose I have packageA which provides a class usefulClass, pytest fixtures in a test_stuff.py module, and test configurations in a conftest.py module. Moreover, suppose that I have packageBand package…

Sort a list by presence of items in another list

Suppose I have two lists:a = [30, 10, 90, 1111, 17] b = [60, 1201, 30, 17, 900]How would you sort this most efficiently, such that:list b is sorted with respect to a. Unique elements in b should be pla…

Sample from a multivariate t distribution python

I am wondering if there is a function for sampling from a multivariate student t-distribution in Python. I have the mean vector with 14 elements, the 14x14 covariance matrix and the degrees of freedom …

Why is this Jinja nl2br filter escaping brs but not ps?

I am attempting to implement this Jinja nl2br filter. It is working correctly except that the <br>s it adds are being escaped. This is weird to me because the <p>s are not being escaped and…

select certain monitor for going fullscreen with gtk

I intend to change the monitor where I show a fullscreen window. This is especially interesting when having a projector hooked up.Ive tried to use fullscreen_on_monitor but that doesnt produce any visi…

Load Excel add-in using win32com from Python

Ive seen from various questions on here that if an instance of Excel is opened from Python using:xl = win32com.client.gencache.EnsureDispatch(Excel.Application) xl.Visible = True wb = xl.Workbooks.Open…

iterating through a list removing items, some items are not removed

Im trying to transfer the contents of one list to another, but its not working and I dont know why not. My code looks like this:list1 = [1, 2, 3, 4, 5, 6] list2 = []for item in list1:list2.append(item)…

Apply function to create string with multiple columns as argument

I have a dataframe like this:name . size . type . av_size_type 0 John . 23 . Qapra . 22 1 Dan . 21 . nukneH . 12 2 Monica . 12 . kahless . 15I wa…