Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object of type 'complex' is not JSON serializable while using pyLDAvis.display() function

I have a document Term matrix with nine documents:

enter image description here

I am running the code as below:

import pyLDAvis.gensim
topicData = pyLDAvis.gensim.prepare(ldamodel, docTermMatrix, dictionary)
pyLDAvis.display(topicData)  

I am getting the below error when executing pyLDAvis.display function:

TypeError: Object of type 'complex' is not JSON serializable

Can someone guide here? What could be the reason?

like image 887
Gaurav Pandey Avatar asked Sep 23 '17 12:09

Gaurav Pandey


2 Answers

I had the same problem. Following the GH issue referenced by user3411846 I found a different, simpler solution.

The complex number had come from coordinate calculation and specifying the "mds" worked.

https://github.com/bmabey/pyLDAvis/issues/69#issuecomment-311337191

So your code would be

topicData = pyLDAvis.gensim.prepare(ldamodel, docTermMatrix, dictionary, mds='mmds')   

Other options for mds are here: https://pyldavis.readthedocs.io/en/latest/modules/API.html#pyLDAvis.prepare

like image 169
birdsarah Avatar answered Sep 20 '22 18:09

birdsarah


Add this line of code to your pyLDAvis pyLDAvis/utils.py

    if np.iscomplexobj(obj):
        return abs(obj)

This error has been reported in GitHub GitHub Issue

like image 24
user3411846 Avatar answered Sep 19 '22 18:09

user3411846