当我试图保存一个非常大的数组(20000 x 20000个元素),结果返回全是零:
In [2]: shape = (2e4,)*2
In [3]: r = np.random.randint(0, 10, shape)
In [4]: r.tofile('r.data')
In [5]: ls -lh r.data
-rw-r--r-- 1 whg staff 3.0G 23 Jul 16:18 r.data
In [6]: r[:6,:6]
Out[6]:
array([[6, 9, 8, 7, 4, 4],
[5, 9, 5, 0, 9, 4],
[6, 0, 9, 5, 7, 6],
[4, 0, 8, 8, 4, 7],
[8, 3, 3, 8, 7, 9],
[5, 6, 1, 3, 1, 4]])
In [7]: r = np.fromfile('r.data', dtype=np.int64)
In [8]: r = r.reshape(shape)
In [9]: r[:6,:6]
Out[9]:
array([[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
np.save()执行类似奇怪的操作。
在搜索网络后,我发现在OSX中存在已知的错误:
https://github.com/numpy/numpy/issues/2806
当我尝试使用Python的read()从文件中读取tostring()数据时,会出现内存错误。
有更好的方法吗?有人可以推荐解决此问题的实用解决方法吗?