scipy大稀疏矩阵

时间:2012-03-12 16:40:59

标签: python scipy

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

n = 10 ** 5
x = scipy.sparse.rand(n, n, .001)

获取

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

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

1 个答案:

答案 0 :(得分: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))