How can I serialize a numpy array while preserving matrix dimensions?

后端 未结 7 850
青春惊慌失措
青春惊慌失措 2020-12-04 13:14

numpy.array.tostring doesn\'t seem to preserve information about matrix dimensions (see this question), requiring the user to issue a call to numpy.array.

7条回答
  •  生来不讨喜
    2020-12-04 13:41

    Msgpack has the best serialization performance: http://www.benfrederickson.com/dont-pickle-your-data/

    Use msgpack-numpy. See https://github.com/lebedov/msgpack-numpy

    Install it:

    pip install msgpack-numpy
    

    Then:

    import msgpack
    import msgpack_numpy as m
    import numpy as np
    
    x = np.random.rand(5)
    x_enc = msgpack.packb(x, default=m.encode)
    x_rec = msgpack.unpackb(x_enc, object_hook=m.decode)
    

提交回复
热议问题