numpy's tostring/fromstring — what do I need to specify to restore the array

寵の児 提交于 2019-12-12 10:51:14

问题


Given a raw binary representation of a numpy array, what is the complete set of metadata needed to unambiguously restore the array?

For example,

>>> np.fromstring( np.array([42]).tostring())
array([  2.07507571e-322])

which is to be expected (with a hindsight, at least): here the I haven't told fromstring to expect ints, so it goes with the default float.

But it seems to me that just specifying the dtype=np.float64 or similar may or may not be sufficient. For example,

>>> a = np.array([42.])
>>> a.dtype
dtype('float64')
>>> a.dtype.byteorder
'='

which the docs tell me means 'native order'. Meaning, it's going to be interpreted differently on a big-endian and little-endian machines --- or am I missing something simple?


回答1:


sys.byteorder gives the endianness of the machine.


However, as @J.F.Sebastain, @seberg and @jorgeca have suggested, np.savez is a better way to go. The help docstring shows

import io
content = io.BytesIO()
np.savez(content, x=x, y=y)
content.seek(0)

which means you could save the string content to an sqlite database.

Then, when you SELECT this string from the database, it can be re-converted into numpy arrays with

data = np.load(content)


来源:https://stackoverflow.com/questions/13672597/numpys-tostring-fromstring-what-do-i-need-to-specify-to-restore-the-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!