回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我有两本这样的字典:</p>
<pre><code>dict_of_items = tf_idf_by_doc {1: [('dog', 3), ('bird', 0)], 2: [('egret', 2), ('cat', 3), ('bird', 0), ('aardvark', 1)], 3: [('fish', 6), ('bird', 0), ('dog', 1), ('aardvark', 5)], 4: [('fish', 6), ('bird', 0), ('dog', 1), ('aardvark', 2)], 5: [('egret', 4), ('bird', 0)], 6: [('bird', 0)], 7: [('dog', 5), ('bird', 0)], 8: [('bird', 0), ('aardvark', 1)]}
dict_of_search = {1: [('bird', 0), ('dog', 1), ('cat', 3)]}
</code></pre>
<p>我需要计算<code>dict_of_search</code>和{<cd2>}中每个键的<code>dict_of_search</code>和<em>之间的点积,然后存储得到的点积值并按键跟踪。我的意思是。。。在</p>
<p>在<code>dict_of_items</code>中,1和<code>dict_of_search</code>中的项有一个向量:</p>
^{pr2}$
<p>所以我的点积是:<code>3</code></p>
<p><strong>所需的结果将是按点积降序排列的单词词典,其中包括单词dict_of_items及其各自的点积(与搜索结果中的dict_相比)(这将永远是一个项目)。</strong></p>
<p>但是,我不确定如何将字典的形状转换为两个数组来执行向量计算,尤其是当其中一个术语没有出现时(例如,在上面的例子中,<code>cat</code>没有出现在<code>dict_of_items_1</code>中的键<code>1</code>中)时。在</p>
<p>我用<code>numpy</code>尝试过类似的方法。。。在</p>
<pre><code>import numpy as numpy
def main():
test_arr_1 = [1,2,3]
test_arr_2 = [3,2,6]
first_dot_product = numpy.dot(test_arr_1, test_arr_2)
print("First Example: ", first_dot_product)
test_arr_3 = [3,0,1]
test_arr_4 = [2,10]
second_dot_product = numpy.dot(test_arr_3, test_arr_4)
print("Second Example Missing Value: ", second_dot_product)
main()
</code></pre>
<p>但这失败了,因为向量的大小和形状不一样。在</p>
<pre><code>ValueError: shapes (3,) and (2,) not aligned: 3 (dim 0) != 2 (dim 0)
</code></pre>
<p>我还尝试将字典值重新调整为列表:</p>
<pre><code>def main():
dict_of_items = {'1': [('bird', 0), ('dog', 3), ('egret', 2), ('bird', 0), ('aardvark', 1), ('cat', 3), ('dog', 1), ('bird', 0), ('fish', 6), ('aardvark', 5), ('dog', 1), ('bird', 0), ('fish', 6), ('aardvark', 2), ('egret', 4), ('bird', 0), ('bird', 0), ('bird', 0), ('dog', 5), ('bird', 0), ('aardvark', 1)]}
test_list_of_lists = []
for k, v in dict_of_items.items():
curr_list = []
for aTuple in v:
curr_list.append(aTuple[1])
test_list_of_lists.append(curr_list)
print(test_list_of_lists)
main()
</code></pre>
<p>但这只是错误地将所有内容合并到一个列表中:<code>[[0, 3, 2, 0, 1, 3, 1, 0, 6, 5, 1, 0, 6, 2, 4, 0, 0, 0, 5, 0, 1]]</code></p>
<p>我还查看了<a href="https://stackoverflow.com/questions/33079472/dot-product-with-dictionaries">this post</a>,但该字典的格式要简单得多。在</p>