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 that can't be performed on sparse matrices.
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() and .todense() methods which are available on scipy sparse matrices do not seem to take an optional dense array argument, but maybe there is some other way to do this.
(I've also started a stackoverflow version of this question here.)
Thanks,
Conrad lee
>
>
> On Sun, Feb 5, 2012 at 9:05 AM, Conrad Lee <conr...@gmail.com> wrote:
> 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 that can't be performed on sparse matrices.
>
> 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() and .todense() methods which are available on scipy sparse matrices do not seem to take an optional dense array argument, but maybe there is some other way to do this.
>
> (I've also started a stackoverflow version of this question here.)
>
> Thanks,
>
> Conrad lee
>
>
>
> If your sparse matrix is in coo format, you can use fancy indexing to assign the values to the existing array.
Although, unless your sparsity pattern doesn't change (which it may not), you'll need to zero the entire dense array before reassigning, which will also take "a non-negligible amount of time".
_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user
Although, unless your sparsity pattern doesn't change (which it may not), you'll need to zero the entire dense array before reassigning, which will also take "a non-negligible amount of time".
> I did a quick and dirty benchmark, and zeroing takes a small fraction of the time of allocating.
Good to know.
Warren, thanks for the suggestion with the COO matrix. In general I'm storing sparse matrices in the CSR format for quick multiplication, so your approach would mean that I have to convert to a COO matrix every time, but that conversion is pretty quick.