def _cosine_distance(a, b, data_is_normalized=False):
"""Compute pair-wise cosine distance between points in a and b.
Parameters
----------
a : array_like
An NxM matrix of N samples of dimensionality M.
b : array_like
An LxM matrix of L samples of dimensionality M.
data_is_normalized : Optional[bool]
If True, assumes rows in a and b are unit length vectors.
Otherwise, a and b are explicitly normalized to lenght 1.
Returns
-------
ndarray
Returns a matrix of size len(a), len(b) such that eleement (i, j)
contains the squared distance between `a[i]` and `b[j]`.
"""
if not data_is_normalized:
a = np.asarray(a) / np.linalg.norm(a, axis=1, keepdims=True)
b = np.asarray(b) / np.linalg.norm(b, axis=1, keepdims=True)
return 1. - np.dot(a, b.T)
When calculating the cosine similarity of feature according to the above function, the range of feature a and feature b after matrix operation is between [-1,1], and finally returns between [0,2], is this true? However, when using the test in the project, we found no similarity of more than 1 in any case, why is this?
def _cosine_distance(a, b, data_is_normalized=False): """Compute pair-wise cosine distance between points in
a
andb
.When calculating the cosine similarity of feature according to the above function, the range of feature a and feature b after matrix operation is between [-1,1], and finally returns between [0,2], is this true? However, when using the test in the project, we found no similarity of more than 1 in any case, why is this?