将值分配给大型密集平方矩阵的切片列表(Python)

问题描述 投票:0回答:1

我正在处理大小为NxN ~(100k x 100k)的大型密集正方形矩阵,这些矩阵太大而无法放入内存。

经过一些研究,我发现大多数人通过使用numpy的memappytables包来处理大型矩阵。但是,我发现这些软件包似乎有很多限制。他们似乎都没有提供支持ASSIGN值来沿多个维度列出磁盘上矩阵的切片。

我想寻找一种有效的方法,将值分配给大型密集方阵M,方法如下:

M[0, [1,2,3], [8,15,30]] = np.zeros((3, 3))

  • 使用memmap,表达式M[0, [1,2,3], [8,15,30]]总是将切片复制到RAM中,因此分配似乎无效。
  • With pytables,不支持沿1个以上维度进行列表切片。目前,我只是沿1个维度进行切片,然后再按另一个维度(即M[0, [1,2,3]][:, [8,15,30]])进行切片。该解决方案的RAM使用率将与N成比例,这比处理整个数组(N ^ 2)更好,但仍不理想。

  • 另外,看来[[pytables并不是处理具有很多行的矩阵的最有效方法。 (或者是否有一种方法可以指定chunksize来摆脱此消息?)我收到以下警告消息:

The Leaf ``/M`` is exceeding the maximum recommended rowsize (104857600 bytes); be ready to see PyTables asking for *lots* of memory and possibly slow I/O. You may want to reduce the rowsize by trimming the value of dimensions that are orthogonal (and preferably close) to the *main* dimension of this leave. Alternatively, in case you have specified a very small/large chunksize, you may want to increase/decrease it.
我只是想知道是否有更好的解决方案将值分配给大型矩阵的任意2d切片?
python matrix pytables numpy-memmap
1个回答
0
投票
首先,请注意,在numpy中(不确定pytables),M[0, [1,2,3], [8,15,30]]将返回与元素(3,)M[0,1,8]M[0,2,15]相对应的形状M[0,3,30]的数组,因此将np.zeros((3,3))分配给该数组将引发错误。

现在,以下对我有用:

np.save('M.npy', np.random.randn(5,5,5)) # create some dummy matrix M = np.load('M.npy', mmap_mode='r+') # load such matrix as a memmap M[[0,1,2],[1,2,3],[2,3,4]] = 0 M.flush() # make sure thing is updated on disk del M M = np.load('M.npy', mmap_mode='r+') # re-load matrix print(M[[0,1,2],[1,2,3],[2,3,4]]) # should show array([0., 0., 0.])

© www.soinside.com 2019 - 2024. All rights reserved.