简体   繁体   English

同一向量之间的余弦距离不等于0

[英]Cosine distance between same vectors not equal 0

I'm trying to retrieve the nearest neighbors of a vector from a list of vectors, using : 我正在尝试使用向量从向量列表中检索向量的最近邻居:

neigh = NearestNeighbors(metric='cosine') neigh = NearestNeighbors(metric ='cosine')

neigh.fit(list) neigh.fit(清单)

From what I've read and witnessed if vector1 and vector2 have the same exact value across all dimension, the distance retrieved from these two vectors will be equal to 0. I'm using the kneighbors method to find the distance. 根据我的阅读和见证,如果vector1vector2在所有维度上都具有相同的精确值,则从这两个向量中检索到的距离将等于0。我正在使用kneighbors方法来查找距离。

neigh.kneighbors(vector_input) neigh.kneighbors(vector_input)

However, in some cases (not all cases) even if both vectors are equal, the distance retrieved is not equal to 0 but some tiny numbers like 2.34e-16. 但是,在某些情况下(并非所有情况下),即使两个向量相等,检索到的距离也不等于0,而是一些微小的数字,例如2.34e-16。


len([i for i, j in zip(vector_from_list,vector_input) if i == j]) returns the dimension of the list meaning that each i-index element is equal to the i-index element of the other vector. len([[i for i,zip(如果我== j,则为vector_from_list,vector_input)中的j])返回列表的尺寸,这意味着每个i-index元素都等于另一个向量的i-index元素。 Therefore, the vectors, if I'm not wrong, are totally equal. 因此,如果我没记错的话,向量是完全相等的。

The dtype for all vectors is np.float64 所有向量的dtype为np.float64


Is the method to find the distance not consistent ? 查找距离的方法不一致吗? Or did I overlook something (a parameter, for example) in scikit method ? 还是我在scikit方法中忽略了某些东西(例如参数)?

I think that's an expected behavior. 我认为这是预期的行为。

If you want to use a condition if distance is equal to zero consider using numpy.isclose . 如果要使用距离等于零的条件,请考虑使用numpy.isclose For example, 例如,

import numpy as np

a = 2.34e-16
b = 1.7e-14 # both tiny values, almost zero
print(a==b) # prints False
print(np.isclose(a,b)) # prints True

You can set how close you want the value to be by setting other parameters of the function. 您可以通过设置函数的其他参数来设置所需值的接近程度。 See documentation for more. 有关更多信息 ,请参见文档

Alternatively, you can also use python's built-in function math.isclose . 另外,您也可以使用python的内置函数math.isclose See documentation . 请参阅文档 Example, 例,

import math

a = 2.34e-16
b = 1.7e-14 # both tiny values, almost zero
print(math.isclose(a,b, abs_tol=1e-10)) # True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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