[英]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:现在我有两个 numpy 数组:
Array A -> 2000 vectors of 512 elements,数组 A -> 2000 个包含 512 个元素的向量,
Array B -> 1000 vectors of 512 elements.数组 B -> 512 个元素的 1000 个向量。
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:我需要计算数组 A 和数组 B 中向量之间的每一个距离。现在,我从数组 A 中取出 1 个向量,并计算它与数组 B 中所有向量的距离,如下所示:
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.但是使用这个我必须循环 2000 个周期,这需要很多时间。
Any alternatives?任何替代方案?
sklearn.metrics.pairwise_distances
正好解决了这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.