3 回答

TA贡献1785条经验 获得超4个赞
使用numpy.memmap您创建直接映射到文件的数组:
import numpy
a = numpy.memmap('test.mymemmap', dtype='float32', mode='w+', shape=(200000,1000))
# here you will see a 762MB file created in your working directory
您可以将其视为常规数组:+ = 1000。
甚至可以将更多数组分配给同一文件,并根据需要从相互来源控制它。但是我在这里遇到了一些棘手的事情。要打开完整的数组,您必须先使用del以下命令“关闭”上一个数组:
del a
b = numpy.memmap('test.mymemmap', dtype='float32', mode='r+', shape=(200000,1000))
但是只打开数组的某些部分可以实现同时控制:
b = numpy.memmap('test.mymemmap', dtype='float32', mode='r+', shape=(2,1000))
b[1,5] = 123456.
print a[1,5]
#123456.0
大!a与一起更改b。更改已经写入磁盘。
另一个值得一提的重要事情是offset。假设您不想采用中的前2行b,而是要采用150000和150001行。
b = numpy.memmap('test.mymemmap', dtype='float32', mode='r+', shape=(2,1000),
offset=150000*1000*32/8)
b[1,2] = 999999.
print a[150001,2]
#999999.0
现在,您可以在同时操作中访问和更新阵列的任何部分。注意偏移量计算中的字节大小。因此,对于“ float64”,此示例为150000 * 1000 * 64/8。

TA贡献1828条经验 获得超3个赞
32位进程仅限于2Gb RAM(虚拟或其他)。这是因为32位仅允许寻址4Gb,而OS保留2Gb。可以将其调整为3Gb / 1Gb,但这是您的限制。解决此问题的唯一其他方法是使用多进程模块将程序拆分为单独的进程,每个进程限制为2Gb
添加回答
举报