5

我有一个相当大的稀疏矩阵,我估计它在加载到内存时会占用 1Gb。

我不需要随时访问整个矩阵,所以某种内存映射会起作用;但是,似乎不可能使用 numpy 或辣(我熟悉的工具)来内存映射稀疏矩阵。

它可以很容易地放入内存中,但是如果我每次运行程序时都必须加载它会很痛苦。也许某种方式可以在运行之间将其保存在内存中?

那么,您有什么建议: 1. 找到一种方法来记忆映射稀疏矩阵;2. 每次只需将整个想法加载到内存中 3. ?

4

2 回答 2

7

以下可能是一个一般概念,但您将不得不弄清楚很多细节......您应该首先熟悉CSR 格式,其中一个数组的所有信息都存储在 3 个数组中,两个长度为非零条目的数量,长度之一是行数加一:

>>> import scipy.sparse as sps
>>> a = sps.rand(10, 10, density=0.05, format='csr')
>>> a.toarray()
array([[ 0.        ,  0.46531486,  0.03849468,  0.51743202,  0.        ],
       [ 0.        ,  0.67028033,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.9967058 ],
       [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ]])
>>> a.data
array([ 0.46531486,  0.03849468,  0.51743202,  0.67028033,  0.9967058 ])
>>> a.indices
array([1, 2, 3, 1, 4])
>>> a.indptr
array([0, 3, 4, 4, 5, 5])

非零条目也是如此a.data,按行主要顺序,a.indices具有非零条目的相应列索引,并且a.indptr具有其他两个数组的起始索引,其中每行的数据开始,例如a.indptr[3] = 4a.indptr[3+1] = 5,所以非零条目第四行是a.data[4:5],以及它们的列索引a.indices[4:5]

因此,您可以将这三个数组存储在磁盘中,并将它们作为 memmap 访问,然后您可以按如下方式检索 m 到 n 行:

ip = indptr[m:n+1].copy()
d = data[ip[0]:ip[-1]]
i = indices[ip[0]:ip[-1]]
ip -= ip[0]
rows = sps.csr_matrix((d, i, ip))

作为概念的一般证明:

>>> c = sps.rand(1000, 10, density=0.5, format='csr')
>>> ip = c.indptr[20:25+1].copy()
>>> d = c.data[ip[0]:ip[-1]]
>>> i = c.indices[ip[0]:ip[-1]]
>>> ip -= ip[0]
>>> rows = sps.csr_matrix((d, i, ip))
>>> rows.toarray()
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.55683501,
         0.61426248,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.67789204,  0.        ,  0.71821363,
         0.01409666,  0.        ,  0.        ,  0.58965142,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.1575835 ,  0.08172986,
         0.41741147,  0.72044269,  0.        ,  0.72148343,  0.        ],
       [ 0.        ,  0.73040998,  0.81507086,  0.13405909,  0.        ,
         0.        ,  0.82930945,  0.71799358,  0.8813616 ,  0.51874795],
       [ 0.43353831,  0.00658204,  0.        ,  0.        ,  0.        ,
         0.10863725,  0.        ,  0.        ,  0.        ,  0.57231074]])
>>> c[20:25].toarray()
array([[ 0.        ,  0.        ,  0.        ,  0.        ,  0.55683501,
         0.61426248,  0.        ,  0.        ,  0.        ,  0.        ],
       [ 0.        ,  0.        ,  0.67789204,  0.        ,  0.71821363,
         0.01409666,  0.        ,  0.        ,  0.58965142,  0.        ],
       [ 0.        ,  0.        ,  0.        ,  0.1575835 ,  0.08172986,
         0.41741147,  0.72044269,  0.        ,  0.72148343,  0.        ],
       [ 0.        ,  0.73040998,  0.81507086,  0.13405909,  0.        ,
         0.        ,  0.82930945,  0.71799358,  0.8813616 ,  0.51874795],
       [ 0.43353831,  0.00658204,  0.        ,  0.        ,  0.        ,
         0.10863725,  0.        ,  0.        ,  0.        ,  0.57231074]])
于 2013-04-16T16:12:43.680 回答
2

Scipy 支持不同种类的稀疏矩阵。但是您必须编写一个例程才能将其读入内存。你应该使用哪种类型取决于你想用它做什么。

如果您的矩阵非常稀疏,您可以使用struct模块将(row, column, value)元组作为二进制数据保存到磁盘。假设可移植性不是问题,这将使磁盘上的数据更小并且更容易加载。

然后,您可以像这样读取数据:

import struct
from functools import partial

fmt = 'IId'
size = struct.calcsize(fmt)

with open('sparse.dat', 'rb') as infile:
    f = partial(infile.read, size)
    for chunk in iter(f, ''):
        row, col, value = struct.unpack(fmt, chunk)
        # put it in your matrix here
于 2013-04-16T14:48:36.083 回答