Giter Club home page Giter Club logo

Comments (4)

ahsan3803 avatar ahsan3803 commented on June 5, 2024 2

@athn-nik With this method you can't write JSON file and you will get following error.
TypeError: Object of type ndarray is not JSON serializable
because JSON format is quite different and built-in json module can only handle primitives types that have a direct JSON equivalent (lists, dictionary, strings etc.) check following screenshot.

python_serialize_numpy_array_into_json_explained

To get JSON file we need to serialize or encode NumPy array into JSON format.
As you know that json module has a JSONEncoder class, we can use this class to encode our NumPy array data into JSON format.
Here is example that how you can solve it.

class NumpyArrayEncode(JSONEncoder):
    def default(self, o):
        if isinstance(o, numpy.ndarray):
            return o.tolist()
        return JSONEncoder.default(self, o)

numpyArray = numpy.array(vibe_results)

#then to write data in JSON
json.dump(numpyArray, fp, cls=NumpyArrayEncode)

Note: Don't forget to import JSONEncoder from json module
from json import JSONEncoder
@lan786 try it. I hope it will solve your problem.

from vibe.

athn-nik avatar athn-nik commented on June 5, 2024

It is pretty easy do it yourself and the open a pull request and we can merge it.
Something like:

with open('vibe_results.json', 'w') as fp:
    json.dump(vibe_results, fp)

should be enough. However json is not so memory efficient. Also ujson package is more efficient than json in my experience.

from vibe.

lisa676 avatar lisa676 commented on June 5, 2024

@ahsan3803 Thanks so much for this solution. I faced same problem as you mention and now I have solved it with your provided example.
@athn-nik As I read code and data and it seems that ndarray is different in pkl. In ujson I'm unable to find that how we can encode numpy array as suggested in above solution. I'll submit a new issue in ujson repository.

from vibe.

athn-nik avatar athn-nik commented on June 5, 2024

Ok, I don't think a pull request is needed for this. If you want you can open one. I am closing this for now. Thanks for the solution @ahsan3803 . Happy to help with more issues @lan786

from vibe.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.