0

For the following code:

document_vectors = dict()
for label in d2v_model.docvecs.doctags:
    vector = [ v for v in d2v_model.docvecs[label]]
    document_vectors[label] = list(vector)

document_vectors_file ="../results/amazon_hierarchical_document_vectors.json"

with open(document_vectors_file, "w") as outfile:
    print "Writing document vectors .."
    json.dumps(document_vectors, outfile)

I am getting:

TypeError: 0.031942371 is not JSON serializable

Debugging it shows me that I'm indeed having here a dict() of list() (I also tried to use just a simple array) but it's not working. Why?

Stefan Falk
  • 18,764
  • 34
  • 144
  • 286

1 Answers1

0

Interestingly this did the trick:

vector = [1.0*v for v in d2v_model.docvecs[label]]

The debugger says that d2v_model.docvecs[label] returns a float32, however, json is not happy with it. Multiplying by 1.0 converted it into a float64 which works all of a sudden.

Might be related to Convert numpy type to python

Community
  • 1
  • 1
Stefan Falk
  • 18,764
  • 34
  • 144
  • 286