Reputation: 91
I have a document Term matrix with nine documents:
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?
Upvotes: 9
Views: 11000
Reputation: 11
add this to your pyldavis/utils.py (NumPyEncoder)
if np.iscomplexobj(obj):
return abs(obj)
class NumPyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.int64) or isinstance(obj, np.int32):
return int(obj)
if isinstance(obj, np.float64) or isinstance(obj, np.float32):
return float(obj)
if np.iscomplexobj(obj):
return abs(obj)
return json.JSONEncoder.default(self, obj)
#resolved my issue
Upvotes: 1
Reputation: 1165
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
Upvotes: 13
Reputation: 248
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
Upvotes: 4