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

后端 未结 7 801
青春惊慌失措
青春惊慌失措 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 14:02

    pickle.dumps or numpy.save encode all the information needed to reconstruct an arbitrary NumPy array, even in the presence of endianness issues, non-contiguous arrays, or weird tuple dtypes. Endianness issues are probably the most important; you don't want array([1]) to suddenly become array([16777216]) because you loaded your array on a big-endian machine. pickle is probably the more convenient option, though save has its own benefits, given in the npy format rationale.

    The pickle option:

    import pickle
    a = # some NumPy array
    serialized = pickle.dumps(a, protocol=0) # protocol 0 is printable ASCII
    deserialized_a = pickle.loads(serialized)
    

    numpy.save uses a binary format, and it needs to write to a file, but you can get around that with io.BytesIO:

    a = # any NumPy array
    memfile = io.BytesIO()
    numpy.save(memfile, a)
    memfile.seek(0)
    serialized = json.dumps(memfile.read().decode('latin-1'))
    # latin-1 maps byte n to unicode code point n
    

    And to deserialize:

    memfile = io.BytesIO()
    memfile.write(json.loads(serialized).encode('latin-1'))
    memfile.seek(0)
    a = numpy.load(memfile)
    
    0 讨论(0)
提交回复
热议问题