使用来自两个不同字典的值进行计算并返回键

我正在尝试计算dict1的所有值和dict2的所有值之间的余弦相似度。 完成后,我想返回相似度很高的字典的键。为此,我想将余弦相似度的结果保存在相似度字典中。 这是我的尝试:

dict_of_sims = {}
for offer in all_offers_with_vectors:
    for request in all_req_vec:
        for o_key, o_val in offer.items():
            for r_key, r_val in request.items():
                cos_sim =  numpy.dot(o_val, r_val)/(norm(o_val,)*norm(r_val))
                dict_of_sims[[o_key, r_key]] = cos_sim 

这给了我以下错误:

ValueError: shapes (1,300) and (1,300) not aligned: 300 (dim 1) != 1 (dim 0)

有人可以帮忙1.解释错误并2.引导我朝正确的方向前进吗? 先感谢您!

评论
  • 好菇凉
    好菇凉 回复

    It looks like you're trying to calculate a dot product of two 1x300 matrices. The error simply states that this cannot work, since you can only multiply an m x n matrix with an n x p matrix (i.e. the 'inner dimensions' need to be the same).

    另外,如果您没有提供最小的工作示例,那么很难说如何改善代码。