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

3

8

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?

Rubidium answered 23/9, 2017 at 12:49 Comment(0)
D
12

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

Davon answered 21/11, 2018 at 7:42 Comment(1)
similarly, i set mds='tsne', which also solved the problem.Yacketyyak
B
4

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

Bigoted answered 16/4, 2018 at 2:19 Comment(0)
I
0

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

Idolatrous answered 9/12, 2021 at 10:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.