scipy大稀疏矩阵

eri*_*icf 3 python scipy

我正在尝试使用大型10 ^ 5x10 ^ 5稀疏矩阵,但似乎正在对抗scipy:

n = 10 ** 5
x = scipy.sparse.rand(n, n, .001)
Run Code Online (Sandbox Code Playgroud)

得到

ValueError: Trying to generate a random sparse matrix such as the
    product of dimensions is greater than 2147483647 - this is not
    supported on this machine
Run Code Online (Sandbox Code Playgroud)

有谁知道为什么有限制,如果我可以避免它?(fyi,我正在使用带有4GB内存的macbook air和enthought发行版)

Sve*_*ach 10

这是由scipy.sparse.rand()实施方式产生的限制.您可以滚动自己的随机矩阵生成来规避此限制:

n = 10 ** 5
density = 1e-3
ij = numpy.random.randint(n, size=(2, n * n * density))
data = numpy.random.rand(n * n * density)
matrix = scipy.sparse.coo.coo_matrix((data, ij), (n, n))
Run Code Online (Sandbox Code Playgroud)