初始化高维稀疏矩阵
Initialize high dimensional sparse matrix
我想使用 sklearn
初始化 300,000 x 300,0000
稀疏矩阵,但它需要内存,就好像它不是稀疏矩阵一样:
>>> from scipy import sparse
>>> sparse.rand(300000,300000,.1)
它给出了错误:
MemoryError: Unable to allocate 671. GiB for an array with shape (300000, 300000) and data type float64
这与我使用 numpy
:
初始化时的错误相同
np.random.normal(size=[300000, 300000])
即使我进入非常低的密度,它也会重现错误:
>>> from scipy import sparse
>>> from scipy import sparse
>>> sparse.rand(300000,300000,.000000000001)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../python3.8/site-packages/scipy/sparse/construct.py", line 842, in rand
return random(m, n, density, format, dtype, random_state)
File ".../lib/python3.8/site-packages/scipy/sparse/construct.py", line 788, in random
ind = random_state.choice(mn, size=k, replace=False)
File "mtrand.pyx", line 980, in numpy.random.mtrand.RandomState.choice
File "mtrand.pyx", line 4528, in numpy.random.mtrand.RandomState.permutation
MemoryError: Unable to allocate 671. GiB for an array with shape (90000000000,) and data type int64
是否有更节省内存的方法来创建这样的稀疏矩阵?
尝试传递一个合理的 density
参数,如文档中所示...如果您有 10 万亿个细胞,可能像 0.00000001 或其他...
https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.rand.html#scipy.sparse.rand
@hpaulj 的评论是正确的。报错信息中也有线索。
MemoryError:无法为形状为 (90000000000,) 且数据类型为 int64 的数组分配 671.GiB
引用了 int64 而不是 float64 和一个大小为 300,000 X 300,000 的线性数组。这里指的是在创建稀疏矩阵的过程中随机抽样的一个中间步骤,反正占用内存很大。
请注意,在创建任何稀疏矩阵(无论格式如何)时,您都必须为非零值和表示值在矩阵中的位置考虑内存。
只生成你需要的。
from scipy import sparse
import numpy as np
n, m = 300000, 300000
density = 0.00000001
size = int(n * m * density)
rows = np.random.randint(0, n, size=size)
cols = np.random.randint(0, m, size=size)
data = np.random.rand(size)
arr = sparse.csr_matrix((data, (rows, cols)), shape=(n, m))
只要稀疏到足以装入内存,您就可以构建巨大的稀疏数组。
>>> arr
<300000x300000 sparse matrix of type '<class 'numpy.float64'>'
with 900 stored elements in Compressed Sparse Row format>
这可能就是 sparse.rand 构造函数无论如何应该工作的方式。如果任何行、col 对发生碰撞,它会将数据值加在一起,这可能适用于我能想到的所有应用程序。
我想使用 sklearn
初始化 300,000 x 300,0000
稀疏矩阵,但它需要内存,就好像它不是稀疏矩阵一样:
>>> from scipy import sparse
>>> sparse.rand(300000,300000,.1)
它给出了错误:
MemoryError: Unable to allocate 671. GiB for an array with shape (300000, 300000) and data type float64
这与我使用 numpy
:
np.random.normal(size=[300000, 300000])
即使我进入非常低的密度,它也会重现错误:
>>> from scipy import sparse
>>> from scipy import sparse
>>> sparse.rand(300000,300000,.000000000001)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../python3.8/site-packages/scipy/sparse/construct.py", line 842, in rand
return random(m, n, density, format, dtype, random_state)
File ".../lib/python3.8/site-packages/scipy/sparse/construct.py", line 788, in random
ind = random_state.choice(mn, size=k, replace=False)
File "mtrand.pyx", line 980, in numpy.random.mtrand.RandomState.choice
File "mtrand.pyx", line 4528, in numpy.random.mtrand.RandomState.permutation
MemoryError: Unable to allocate 671. GiB for an array with shape (90000000000,) and data type int64
是否有更节省内存的方法来创建这样的稀疏矩阵?
尝试传递一个合理的 density
参数,如文档中所示...如果您有 10 万亿个细胞,可能像 0.00000001 或其他...
https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.rand.html#scipy.sparse.rand
@hpaulj 的评论是正确的。报错信息中也有线索。
MemoryError:无法为形状为 (90000000000,) 且数据类型为 int64 的数组分配 671.GiB
引用了 int64 而不是 float64 和一个大小为 300,000 X 300,000 的线性数组。这里指的是在创建稀疏矩阵的过程中随机抽样的一个中间步骤,反正占用内存很大。
请注意,在创建任何稀疏矩阵(无论格式如何)时,您都必须为非零值和表示值在矩阵中的位置考虑内存。
只生成你需要的。
from scipy import sparse
import numpy as np
n, m = 300000, 300000
density = 0.00000001
size = int(n * m * density)
rows = np.random.randint(0, n, size=size)
cols = np.random.randint(0, m, size=size)
data = np.random.rand(size)
arr = sparse.csr_matrix((data, (rows, cols)), shape=(n, m))
只要稀疏到足以装入内存,您就可以构建巨大的稀疏数组。
>>> arr
<300000x300000 sparse matrix of type '<class 'numpy.float64'>'
with 900 stored elements in Compressed Sparse Row format>
这可能就是 sparse.rand 构造函数无论如何应该工作的方式。如果任何行、col 对发生碰撞,它会将数据值加在一起,这可能适用于我能想到的所有应用程序。