bib
bib

Reputation: 1040

how to use cosssim in gensim

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

I get the following exception:

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?

Upvotes: 0

Views: 603

Answers (1)

Stanislas Morbieu
Stanislas Morbieu

Reputation: 1827

The arguments of gensim.matutils.cossim are expected to be of type list of (int, float) but you are using dictionaries.

The exception happens in the cossim function with the following cossim implementation:

vec1, vec2 = dict(vec1), dict(vec2)

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

Upvotes: 2

Related Questions