[英]python ValueError: too many values to unpack (expected …)
I have defined the following function that takes an input string, and compares it to a large list of strings (both vectorized using tfidf): 我定义了以下函数,该函数接受输入字符串,并将其与大量字符串进行比较(均使用tfidf进行向量化):
def find_new_similar(tfidf_matrix2, index, tfidf_matrix, top_n = 5):
cosine_similarities = linear_kernel(tfidf_matrix2[index:index+1], tfidf_matrix).flatten()
related_docs_indices = [i for i in cosine_similarities.argsort()[::-1] if i != index]
return [(i, cosine_similarities[i]) for i in related_docs_indices][0:top_n], index
when I call this function my output is: 当我调用此函数时,我的输出是:
find_new_similar(tfidf_matrix2, 1, tfidf_matrix)
Out[15]:
([(923576, 0.51192576542407131),
(558563, 0.51192576542407131),
(1554977, 0.51192576542407131),
(1604772, 0.51192576542407131),
(514529, 0.50251903670563314)],
1)
where the first element of each tuple (ie. 923576, 558563) is an index for a large file of terms. 其中每个元组的第一个元素(即923576、558563)是大型术语文件的索引。 I would like to use these indexes and return the value at the index. 我想使用这些索引并在索引处返回值。
I have tried:
for i, score in find_new_similar(tfidf_matrix2, 0, tfidf_matrix):
print (score, corpus[i], i)
Traceback (most recent call last):
File "<ipython-input-18-792db65f6fd0>", line 1, in <module>
for i, score in find_new_similar(tfidf_matrix2, 0, tfidf_matrix):
ValueError: too many values to unpack (expected 2)
can anyone help? 有人可以帮忙吗? thanks? 谢谢?
You function returns a list and index
您的函数返回一个列表和index
return [(i, cosine_similarities[i]) for i in related_docs_indices][0:top_n], index
change your code to 将您的代码更改为
for i, score in find_new_similar(tfidf_matrix2, 0, tfidf_matrix)[0]:
print (score, corpus[i], i)
to get the list and iterate over it. 获取列表并对其进行迭代。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.