简体   繁体   中英

Most efficient way to calculate every L2 distance between vectors of vector array A and vectors of vector array B?

I need to implement an algorithm. But it takes a lot of time to compute and I need to make it as fast as possible.

Right now I have two numpy arrays:

Array A -> 2000 vectors of 512 elements,

Array B -> 1000 vectors of 512 elements.

I need to calculate every single distance between the vectors from Array A and Array B. Right now, I take 1 vector from array A, and calculate it's distances to all vectors in Array B as follows:

np.sum(np.abs(BA[0])**2,axis=-1)**(0.5)

But using this I have to loop for 2000 cycles and it takes a lot of time.

Any alternatives?

sklearn.metrics.pairwise_distances正好解决了这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM