[SciPy-user] combine two sparse matrices

Nathan Bell wnbell@gmail....
Sat May 3 10:24:12 CDT 2008

On Sat, May 3, 2008 at 10:16 AM, Robin <robince@gmail.com> wrote:
>  >
>  >  I was wondering what the most (memory) efficient way of combining two
>  >  sparse matrices would be.
>  >
>  I'd still appreciate some advice as to the best way to do this. I
>  thought of directly appending to the lists in the lil_matrix, but I
>  would then have to sort them again and I wasn't sure if the object
>  array could take resizing like this.

If you're worried about speed or memory consumption, then you should
avoid lil_matrix and dok_matrix.

The fastest and most memory efficient approach uses coo_matrix:

row = array([2,3,1,4])
col = array([4,2,3,1])
data = array([5,5,5,5])
A = coo_matrix( (data,(row,col)), shape=(5,5)).tocsr()

The conversion to CSR forces a copy, however CSR and COO are so much
more memory efficient than LIL/DOK that it shouldn't matter.

Nathan Bell wnbell@gmail.com

More information about the SciPy-user mailing list