将稀疏 scipy 矩阵加载到现有的 numpy 稠密矩阵中
假设我有一个巨大的 numpy 矩阵 A 占用数十 GB。分配此内存需要花费不可忽略的时间。
假设我还有一组与 numpy 矩阵具有相同维度的 scipy 稀疏矩阵。有时我想将这些稀疏矩阵之一转换为稠密矩阵以执行一些矢量化操作。
我可以将这些稀疏矩阵之一加载到 A 中,而不是每次想要将稀疏矩阵转换为稠密矩阵时重新分配空间吗? scipy 稀疏矩阵上可用的 .toarray() 方法似乎没有采用可选的密集数组参数,但也许还有其他方法可以做到这一点。
Say I have a huge numpy matrix A taking up tens of gigabytes. It takes a non-negligible amount of time to allocate this memory.
Let's say I also have a collection of scipy sparse matrices with the same dimensions as the numpy matrix. Sometimes I want to convert one of these sparse matrices into a dense matrix to perform some vectorized operations.
Can I load one of these sparse matrices into A rather than re-allocate space each time I want to convert a sparse matrix into a dense matrix? The .toarray() method which is available on scipy sparse matrices does not seem to take an optional dense array argument, but maybe there is some other way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果稀疏矩阵是 COO 格式:
如果是 CSR 格式:
为了安全起见,您可能需要将以下行添加到上述每个函数的开头:
If the sparse matrix is in the COO format:
If it is in the CSR format:
To be safe, you might want to add the following lines to the beginning of each of the functions above:
似乎确实应该有更好的方法来做到这一点(而且我还没有浏览文档),但是您始终可以循环遍历稀疏数组的元素并分配给密集数组(可能首先将密集数组清零) )。如果这最终太慢,那么这似乎是一个很容易编写的 C 扩展......
It does seem like there should be a better way to do this (and I haven't scoured the documentation), but you could always loop over the elements of the sparse array and assign to the dense array (probably zeroing out the dense array first). If this ends up too slow, that seems like an easy C extension to write....