[英]How to compare each row of vectors of numpy array to itself and every element
I have a numpy array which contains vectorised data.我有一个包含矢量化数据的 numpy 数组。 I need to compare each of these vectors (a row in the array) euclidean distances to itself and every other row.
我需要比较这些向量中的每一个(数组中的一行)欧几里得距离自身和每隔一行。
The vectors are of the form向量的形式为
[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]
I know I need two loops, here is what I have so far我知道我需要两个循环,这是我目前所拥有的
def euclidean_distance_loop(termdoc):
i = 0
j = 0
matrix = np.array([])
while( j < (len(termdoc-1))):
matrix = np.append(matrix,[euclidean_distance(termdoc[i],termdoc[j])])
j = j + 1
return np.array([matrix])
euclidean_distance_loop(termdoc)
I know this is an index problem and I need another index or an incremented index in another loop but not sure how to construct it我知道这是一个索引问题,我需要另一个索引或另一个循环中的递增索引,但不知道如何构造它
You don't need loops.你不需要循环。
def self_distance(x):
return np.linalg.norm(x[:,np.newaxis] - x, axis=-1)
See also:也可以看看:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.