问题描述
我有一个大小为(3、3、19、19)的数组,我应用了flatten
来得到大小为3249的数组.
I have array of size (3, 3, 19, 19), which I applied flatten
to get array of size 3249.
我不得不将这些值与其他一些数据一起写入文件,因此我做了以下操作以将数组转换为字符串.
I had to write these values to file along with some other data, so I did following to get the array in string.
np.array2string(arr.flatten(), separator=', ', suppress_small=False)
但是,当我写完文件后检查文件的内容时,我注意到我在数组中间有,... ,
,如下所示:
However when I checked the content of the files after write,I noticed that I have ,... ,
in the middle of the array as following
[ 0.09720755, -0.1221265 , 0.08671697, ..., 0.01460444, 0.02018792, 0.11455765]
[ 0.09720755, -0.1221265 , 0.08671697, ..., 0.01460444, 0.02018792, 0.11455765]
如何获取包含所有元素的数组字符串,以便潜在地将所有数据保存到文件中?
How can I get string of array with all the elements, so I can potentially get all data to a file?
推荐答案
据我了解array2string
,它只是用于返回数组的"nice"字符串表示形式.
As far as I understand array2string
, it's just for returning a "nice" string representation of the array.
numpy.ndarray.tofile
对于您的目的可能是更好的选择- https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tofile.html .它应该将数组的全部内容写入给定文件.
numpy.ndarray.tofile
might be a better option for your purposes - https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.tofile.html. It should write the full contents of the array to the given file.
with open("test.bin", "wb") as f:
arr.flatten().tofile(f)
您当然可以使用numpy.fromfile
读回它- https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html .
And you can of course read it back with numpy.fromfile
- https://docs.scipy.org/doc/numpy/reference/generated/numpy.fromfile.html.
with open("test.bin", "rb") as f:
arr = numpy.fromfile(f)
这篇关于numpy array2string应用于巨大的数组,跳过中心值,(...在中间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!