我有一个在Python2.7中用多处理批量执行的进程,并生成大致如下大小的numpy float64
数组:
现在,它们在每一批中进行处理,并进行一些统计分析。这是可管理的(12×2000×89×8字节/float=大约17 my ),但是我想对我的整个数据集做一些分析。
我需要以某种方式将沿维B的批次连接到至少1000000个大小,这意味着8.5GB,然后将它们拆分到维度C中,分别分析每个AxB二维矩阵。(对于维度C中的每个元素,12x1000000“仅”为96 is,这是更易于管理的。)但是我的系统上没有那么多内存,很可能我需要用到2000000或4000000。
有没有一种方法可以在磁盘上进行级联和切片,这样我就不需要在内存中包含整个矩阵了吗?
发布于 2017-05-16 00:00:33
Pytables EArray看起来很有帮助;我创建了一个概念证明:
import tables
import numpy as np
fh = tables.open_file('bigarray.h5', mode='w')
atom = tables.Float64Atom()
filters = tables.Filters(complevel=5, complib='zlib')
bigarray = fh.create_earray(fh.root, 'bigarray', atom, shape=(0,89,12), filters=filters)
chunksize = 2000
nchunks = 25
for k in xrange(nchunks):
if k%10 == 0:
print k
x0 = np.arange(89,dtype=np.float64)
y0 = np.arange(12,dtype=np.float64)
z0 = np.arange(chunksize,dtype=np.float64)+k*chunksize
x,z,y=np.meshgrid(x0,z0,y0)
bigarray.append(x*y*z)
print "shape:", bigarray.shape
print bigarray[10000:10100,1,:]
fh.close()
打印出来的
0
10
20
shape: (50000, 89, 12)
[[ 0. 10000. 20000. ..., 90000. 100000. 110000.]
[ 0. 10001. 20002. ..., 90009. 100010. 110011.]
[ 0. 10002. 20004. ..., 90018. 100020. 110022.]
...,
[ 0. 10097. 20194. ..., 90873. 100970. 111067.]
[ 0. 10098. 20196. ..., 90882. 100980. 111078.]
[ 0. 10099. 20198. ..., 90891. 100990. 111089.]]
并在磁盘上占用62.4MB用于50000*89*12元素(使用压缩的每个元素1.17字节)。
https://stackoverflow.com/questions/43990174
复制相似问题