【问题标题】:How is the cosine distance calculated for two arrays with different shapes in Python?Python中两个不同形状的数组的余弦距离是如何计算的?
【发布时间】:2020-07-05 10:44:05
【问题描述】:

我有两个数组:

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])

如何获取array2中每一行到列表中array1的余弦距离?

【问题讨论】:

  • 你能给出 2 对的公式吗?然后将其应用于 array2 的每一行
  • 另外你的numpy数组定义无效

标签: python arrays multidimensional-array


【解决方案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]

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-15
  • 1970-01-01
  • 2014-08-21
  • 2010-12-21
  • 2022-01-05
  • 2020-06-06
  • 1970-01-01
相关资源
最近更新 更多