问题
I have two arrays:
array1 = numpy.array([ 7.26741212e-01, -9.80825232e-17])
array2 = numpy.array([-3.82390578e-01, -1.48157964e-17],
[-3.82390578e-01, 7.87310307e-01],
[ 7.26741212e-01, -9.80825232e-17],
[ 7.26741212e-01, -9.80825232e-17],
[-3.82390578e-01, -2.06286905e-01],
[ 7.26741212e-01, -9.80825232e-17],
[-2.16887107e-01, 6.84509305e-17],
[-3.82390578e-01, -5.81023402e-01],
[-2.16887107e-01, 6.84509305e-17],
[-2.16887107e-01, 6.84509305e-17])
How do I get the cosine distance of each row in array2 to array1 in a list?
回答1:
import numpy as np
def cosine_similarity(x, y):
return np.dot(x, y) / (np.sqrt(np.dot(x, x)) * np.sqrt(np.dot(y, y)))
a = np.array([7.26741212e-01, -9.80825232e-17])
b = np.array(([-3.82390578e-01, -1.48157964e-17],
[-3.82390578e-01, 7.87310307e-01],
[7.26741212e-01, -9.80825232e-17],
[7.26741212e-01, -9.80825232e-17],
[-3.82390578e-01, -2.06286905e-01],
[7.26741212e-01, -9.80825232e-17],
[-2.16887107e-01, 6.84509305e-17],
[-3.82390578e-01, -5.81023402e-01],
[-2.16887107e-01, 6.84509305e-17],
[-2.16887107e-01, 6.84509305e-17]))
output = [cosine_similarity(a, y) for y in b]
来源:https://stackoverflow.com/questions/62739643/how-is-the-cosine-distance-calculated-for-two-arrays-with-different-shapes-in-py