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

Viewed 7038

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?

3 Answers

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

Related