python - 如何从字节串中恢复二维 numpy.array?

标签 python numpy

numpy.array 有一个方便的 .tostring() 方法,它可以将数组的紧凑表示形式生成为字节串。但是如何从字节串中恢复原始数组呢? numpy.fromstring() 只生成一维数组,没有numpy.array.fromstring()。似乎我应该能够提供一个字符串、一个形状和一个类型,然后就可以了,但是我找不到函数。

最佳答案

>>> x
array([[ 0.   ,  0.125,  0.25 ],
       [ 0.375,  0.5  ,  0.625],
       [ 0.75 ,  0.875,  1.   ]])
>>> s = x.tostring()
>>> numpy.fromstring(s)
array([ 0.   ,  0.125,  0.25 ,  0.375,  0.5  ,  0.625,  0.75 ,  0.875,  1.   ])
>>> y = numpy.fromstring(s).reshape((3, 3))
>>> y
array([[ 0.   ,  0.125,  0.25 ],
       [ 0.375,  0.5  ,  0.625],
       [ 0.75 ,  0.875,  1.   ]])

关于python - 如何从字节串中恢复二维 numpy.array?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7165367/

相关文章:

python - 用偏斜高斯拟合直方图

python - numpy 切片数组而不复制它

python - 单个 Numpy 类型似乎不保留字节顺序

Python分组和拼接: splicing the result returned from itertools. groupby

Python不在for循环中释放内存

python - Cython 中的 ctypedef 与 numpy : what is right convention?

python - 令人尴尬的是,并行助手对于不同的 'n_jobs' 参数没有返回相同的结果

Python 3.7 Authlib UnsupportedAlgorithmError

python - 使用 python 3 将列表添加为树的子级

python - 将 SymPy 矩阵转换为 numpy 数组/矩阵的最佳方法是什么