How is the cosine distance calculated for two arrays with different shapes in Python?

痴心易碎 提交于 2021-02-11 15:53:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!