本文介绍了如何在gensim中使用cosssim的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是关于cossim的使用.
My questioon is about cossim usage.
我有一个很大的功能片段:
I have this fragment of a very big fuction:
for elem in lList:
temp = []
try:
x = dict(np.ndenumerate(np.asarray(model[elem])))
except:
if x not in embedDict.keys():
x = np.random.uniform(low=0.0, high=1.0, size=300)
embedDict[elem] = x
else:
x = dict(np.ndenumerate(np.asarray(embedDict[elem])))
for w in ListWords:
try:
y = dict(np.ndenumerate(np.asarray(model[w])))
except:
if y not in embedDict.keys():
y = np.random.uniform(low=0.0, high=1.0, size=300)
embedDict[w] = y
else:
y = dict(np.ndenumerate(np.asarray(embedDict[w])))
temp.append(gensim.matutils.cossim(x,y))
我收到以下异常:
File "./match.py", line 129, in getEmbedding
test.append(gensim.matutils.cossim(x,y))
File "./Python_directory/ENV2.7_new/lib/python2.7/site-packages/gensim/matutils.py", line 746, in cossim
vec1, vec2 = dict(vec1), dict(vec2)
TypeError: cannot convert dictionary update sequence element #0 to a sequence
能否请您帮我解释一下此异常的含义?
Can you please help me and explain to me what this exception means?
推荐答案
gensim.matutils的参数.cossim 的类型应为(int,浮点数)的列表
,但是您使用的是字典.
The arguments of gensim.matutils.cossim are expected to be of type list of (int, float)
but you are using dictionaries.
该异常发生在 cossim
函数中,并带有以下 cossim实现:
The exception happens in the cossim
function with the following cossim implementation:
vec1, vec2 = dict(vec1), dict(vec2)
使用正确的类型, dict(vec)
可以工作:
With the correct type, dict(vec)
works:
dict([(1, 2.), (3, 4.), (5, 6.)])
但是,如果您没有提供正确的类型,则会引发异常,例如:
But if you do not provide the correct type, it throws the exception, for instance with:
dict([1, 2, 3])
这篇关于如何在gensim中使用cosssim的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!