简体   繁体   English

计算向量数组 A 的向量和向量数组 B 的向量之间的每个 L2 距离的最有效方法?

[英]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.

相关问题 如何计算不同2D向量的行之间的L2或欧式距离 - how would I calculate the L2 or euclidean distance between rows of different 2D vectors tfidf 向量和 tfidf 向量数组之间的 Sklearn cosine_similarity - Sklearn cosine_similarity between a tfidf vector and an array of tfidf vectors 寻找最有效的方法来计算3D点集之间的所有距离矢量 - Looking for most efficient way to calc all distance vectors between set of points in 3D 排序列表/数组的最有效方法(计算 3D 空间中对象之间的距离) - Most efficient way to sort list/array (calculate distance between objects in 3D space) 计算稀疏向量之间的距离 - Calculate distance between sparse vectors 有没有办法找到数组中n个最远的向量? - Is there a way to find the n most distant vectors in an array? 生成n个二进制向量,其中每个向量与每个其他向量的汉明距离为d - Generating n binary vectors where each vector has a Hamming distance of d from every other vector 从向量集中找到最相关的向量 - Find most relevant Vector from set of vectors Tfidvectorizer - L2归一化向量 - Tfidvectorizer - L2 normalized vector 计算具有聚类中心点的向量之间的欧几里得距离 - Calculate euclidean distance between vectors with cluster medoids
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM